Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
RolandPlanet
Frequent Visitor

How do I get the Top X value across columns in each row?

Hello, 

I'm trying to add a column where I get the first most occurring value across columns in a row along with the second most occurring value, and 3rd most occuring value

 

IDColumn1Column2Column3Column4Column5
1234AppleAppleOrangeAppleOrange
1235OrangeOrangePearOrangeOrange

 

In other words, I'd like a column that would show "Apple" as the first most occurring value and "Orange" as the second most occuring value in the first row.  How can I do that? 

1 ACCEPTED SOLUTION

Hi, @RolandPlanet 

 

I have made a few changes on the measure. Please try the following measure to see if it works.

 

Result = 
var _id = SELECTEDVALUE('Table'[ID])
var tab = 
SUMMARIZE(
    'Table',
    'Table'[ID],
    'Table'[Value],
    "Count",
    COUNTROWS(
        FILTER(
            'Table',
            'Table'[Value] = EARLIER('Table'[Value])
        )
    )
)
var newtab = 
ADDCOLUMNS(
    tab,
    "Rank",
    RANKX(
        FILTER(
            tab,
            [ID] = _id
        ),
        [Count]
    )
)
return
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 1
    ),
    [Value],
    "-"
)&" "&
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 2
    ),
    [Value],
    "-"
)&" "&
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 3
    ),
    [Value],
    "-"
)

 

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-alq-msft
Community Support
Community Support

Hi, @RolandPlanet 

 

You may go to Query Editor, go to 'Transform' ribbon, make 'ID' selected and click 'unpivot other columns'.  And then you need to click 'Close and Apply'.

I1.png

 

You may create a measure as below.

 

Result = 
var _id = SELECTEDVALUE('Table'[ID])
var tab = 
SUMMARIZE(
    'Table',
    'Table'[ID],
    'Table'[Value],
    "Count",
    COUNTROWS(
        FILTER(
            'Table',
            'Table'[Value] = EARLIER('Table'[Value])
        )
    )
)
var newtab = 
ADDCOLUMNS(
    tab,
    "Rank",
    RANKX(
        FILTER(
            tab,
            [ID] = _id
        ),
        [Count]
    )
)
return
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 1
    ),
    [Value]
)&" "&
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 2
    ),
    [Value]
)&" "&
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 3
    ),
    [Value]
)

 

 

Result:

I2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

This is great! However, if two values have the same number of occurences, they get concatenated together as "AppleOrange" for example. Is there a way to prevent this? 

Hi, @RolandPlanet 

 

I have made a few changes on the measure. Please try the following measure to see if it works.

 

Result = 
var _id = SELECTEDVALUE('Table'[ID])
var tab = 
SUMMARIZE(
    'Table',
    'Table'[ID],
    'Table'[Value],
    "Count",
    COUNTROWS(
        FILTER(
            'Table',
            'Table'[Value] = EARLIER('Table'[Value])
        )
    )
)
var newtab = 
ADDCOLUMNS(
    tab,
    "Rank",
    RANKX(
        FILTER(
            tab,
            [ID] = _id
        ),
        [Count]
    )
)
return
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 1
    ),
    [Value],
    "-"
)&" "&
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 2
    ),
    [Value],
    "-"
)&" "&
CONCATENATEX(
    FILTER(
        newtab,
        [Rank] = 3
    ),
    [Value],
    "-"
)

 

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@RolandPlanet 

Better you unpivot data

https://radacad.com/pivot-and-unpivot-with-power-bi

 

Then you can use all expect at ID level to get this answer

https://www.sqlbi.com/articles/managing-all-functions-in-dax-all-allselected-allnoblankrow-allexcept...

https://community.powerbi.com/t5/Desktop/Percentage-of-subtotal/td-p/95390

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.