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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Top N from the column

Issue I haveIssue I haveHi Experts,

Test Data file: PowerBi.Pbix 

I have data where the all reasons are in one column and I need to find the TOP 10 along with other columns where I need top 10, SD, mean etc, Reason count by Reason ( is the index created grouped by Reason). 

I tried following adapted from the solution in the Community Forum.  

1. I need help to get TOP 5 reasons in the column and

2. When I do not have to create the index in the query too, if possible so other measures can be done without adding the steps of Index and group by. 

 

 

RankReason = 
IF (
    ISINSCOPE ( 'Data'[ReasonCountByReason]),
    RANKX (
        CALCULATETABLE (
            VALUES ( Data[ReasonCountByReason] ),
            ALLSELECTED ( Data[ReasonCountByReason] )
        ),
        [ReasonCountByReason]
    ),
    IF (
        ISINSCOPE ( 'Data'[ReasonCountByReason] ),
        VAR TotalReasons = Data[TopReasons]
        RETURN
            CALCULATE (
                RANKX (
                    VALUES ( 'Data'[ReasonCountByReason] ),
                    [TopReasons],
                    Data[TopReasons]
                ),
                ALLSELECTED ()
            )
    )
)

-- Did not work

 

Data.PNGTopn.PNG

 

PowerBi.Pbix 

1 ACCEPTED SOLUTION
v-zhangti
Community Support
Community Support

Hi, @Anonymous 

 

Please try the following methods. You can solve it directly with Dax without creating indexes in the power query.

Measure:

Count name =
CALCULATE (
    COUNT ( Data[TotalReasons.Name] ),
    FILTER (
        ALL ( Data ),
        [TotalReasons.Name] = SELECTEDVALUE ( Data[TotalReasons.Name] )
    )
)
Count reason =
CALCULATE (
    COUNT ( Data[Reason] ),
    FILTER ( ALL ( Data ), [Reason] = SELECTEDVALUE ( Data[Reason] ) )
)

vzhangti_0-1653458773616.png

Rank name = RANKX(ALLSELECTED(Data[TotalReasons.Name]),[Count name],,DESC)
Top name = IF([Rank name]<=2,1,0)

Put the measure of Top name into the name's view and set it equal to 1.

vzhangti_1-1653458911553.png

Is this the result you want for Top 2 name?

 

Rank reason = RANKX(ALLSELECTED(Data[Reason]),[Count reason],,DESC)
Top reason = IF([Rank reason]<=5,1,0)

vzhangti_2-1653459465577.png

Is this the result you want for Top 5 reason?

 

Best Regards,

Community Support Team _Charlotte

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

2 REPLIES 2
Anonymous
Not applicable

Spoiler
@v-zhangti  Thanks a lot. Much appreciated. 
v-zhangti
Community Support
Community Support

Hi, @Anonymous 

 

Please try the following methods. You can solve it directly with Dax without creating indexes in the power query.

Measure:

Count name =
CALCULATE (
    COUNT ( Data[TotalReasons.Name] ),
    FILTER (
        ALL ( Data ),
        [TotalReasons.Name] = SELECTEDVALUE ( Data[TotalReasons.Name] )
    )
)
Count reason =
CALCULATE (
    COUNT ( Data[Reason] ),
    FILTER ( ALL ( Data ), [Reason] = SELECTEDVALUE ( Data[Reason] ) )
)

vzhangti_0-1653458773616.png

Rank name = RANKX(ALLSELECTED(Data[TotalReasons.Name]),[Count name],,DESC)
Top name = IF([Rank name]<=2,1,0)

Put the measure of Top name into the name's view and set it equal to 1.

vzhangti_1-1653458911553.png

Is this the result you want for Top 2 name?

 

Rank reason = RANKX(ALLSELECTED(Data[Reason]),[Count reason],,DESC)
Top reason = IF([Rank reason]<=5,1,0)

vzhangti_2-1653459465577.png

Is this the result you want for Top 5 reason?

 

Best Regards,

Community Support Team _Charlotte

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

Helpful resources

Announcements
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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.