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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.