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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Get Top N of a category by a value using DAX function RANKX() and a tie breaker.

I thought I was following the pattern provided in the post:  Breaking Ties in Rankings with RANKX Using Multipl... - Microsoft Power BI Community when I wrote the DAX below.  It doesn't error, and its closer to what I want, but it's not what I want. 

 

I read documentation (I think) on RANKX and I see that it "Returns the rank of an expression evaluated in the current context in the list of values for the expression evaluated for each row in the specified table.", which is a riddle if there ever was one, and the syntax is:

RANKX(<Table>, <Expression> [, <Value>] [, <Order>] [, <Ties>]).

The issue is the confusion caused by the fact that this is a RANKX with a nested RANKX() + DIVIDE() as the expression, so I'm lost trying to get a top 5, sorting by A descending, then B ascending. 

 

RankedByMoney(CareGapsToPotential) =
RANKX(ALL('HN_CHIP'),
RANKX (ALL('HN_CHIP'), 'HN_CHIP'[MoneyLeftOnTable], , ASC)
+ DIVIDE(
RANKX(ALL('HN_CHIP'), 'HN_CHIP'[cgToMaxPotential]),
(COUNTROWS(ALL('HN_CHIP')) + 1)
)
)

 

I got:

mohassan99_0-1652070726934.png

I was trying to get Descending by "Money Left on the Table", then Ascending by "Care Gaps to Potential". How would I do that?

 

Can you help me put that 'ASC' in the right place @amitchandak?

 

Thank you,

Mo

 

2 ACCEPTED SOLUTIONS
v-yalanwu-msft
Community Support
Community Support

Hi, @Anonymous ;

Try it.

RankedByMoney(CareGapsToPotential) =
RANKX (
    ALL ( HN_CHIP ),
    RANKX ( ALL ( HN_CHIP ), 'HN_CHIP'[Money Left on the Table],, ASC )
        * ( COUNTROWS ( 'HN_CHIP' ) + 1 )
        + RANKX ( ALL ( HN_CHIP ), 'HN_CHIP'[Care Gaps to Potential],, DESC )
)

The final output is shown below:

vyalanwumsft_0-1652334643839.png


Best Regards,
Community Support Team _ Yalan Wu
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

Anonymous
Not applicable

I created a rank in MS SQL Server using ROW_NUMBER() OVER(PARTITION BY ... ORDER BY...)

View solution in original post

4 REPLIES 4
v-yalanwu-msft
Community Support
Community Support

Hi, @Anonymous ;

Try it.

RankedByMoney(CareGapsToPotential) =
RANKX (
    ALL ( HN_CHIP ),
    RANKX ( ALL ( HN_CHIP ), 'HN_CHIP'[Money Left on the Table],, ASC )
        * ( COUNTROWS ( 'HN_CHIP' ) + 1 )
        + RANKX ( ALL ( HN_CHIP ), 'HN_CHIP'[Care Gaps to Potential],, DESC )
)

The final output is shown below:

vyalanwumsft_0-1652334643839.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

I created a rank in MS SQL Server using ROW_NUMBER() OVER(PARTITION BY ... ORDER BY...)

Anonymous
Not applicable

@amitchandak 

Now I have this:

mohassan99_0-1652076458351.png

 

Could the manual filter over "Measure" be disables so we can be sure it is not interfereing with the DAX calculation. Care Gaps.... should be ascending within one value of Money....

 

Thank you, 

Mo

amitchandak
Super User
Super User

@Anonymous , Check if this can help

RankedByMoney(CareGapsToPotential) =
RANKX(ALL('HN_CHIP'),
RANKX (ALL('HN_CHIP'), 'HN_CHIP'[MoneyLeftOnTable], , ASC)
+ DIVIDE(
RANKX(ALL('HN_CHIP'), 'HN_CHIP'[cgToMaxPotential],,asc),
(COUNTROWS(ALL('HN_CHIP')) + 1)
) ,,asc
)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.