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
ebecerra
Microsoft Employee
Microsoft Employee

Using a variable to determine sort order inside Rankx

I'm using a slicer to select a "Sort type" for a visualization I have. Depending on what value is selected, I am planning to use that as the sort option for a RANKX function I have. For some reason when I use a variable to get the selected value (since I cannot see the table from inside the RANKX inner part) I get an error that says that it expects the ORDER argument to be 0/FALSE/DESC etc. The funny thing is that if I pass in a hardcoded value to my variable it works fine. Is there something I'm missing here in order to get it to work? Here is an example of the code I have:

 

VAR TopOrBottom = if(SELECTEDVALUE('Top or Bottom'[Option])=="Top",FALSE,TRUE) // THIS DOES NOT WORK
VAR TopOrBottom = TRUE //THIS MAKES RANKX happy.
...
...
... 
RANKX(
ALLSELECTED(MyTable[pageTitle]),
[Total PageViews],[Total PageViews], TopOrBottom // This is where it complains that it needs a 0/1/TRUE/FALSE/ASC/DESC
)
 
Thanks in advanced for your help.
1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

Another option might be to flip the sign of the measure depending on Top/Bottom.

VariableRank = 
VAR Sgn = IF ( SELECTEDVALUE ( 'Top or Bottom'[Option] ) = "Top", 1, -1 )
RETURN
    RANKX ( ALLSELECTED ( MyTable[pageTitle] ), Sgn * [Total PageViews] )

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

Another option might be to flip the sign of the measure depending on Top/Bottom.

VariableRank = 
VAR Sgn = IF ( SELECTEDVALUE ( 'Top or Bottom'[Option] ) = "Top", 1, -1 )
RETURN
    RANKX ( ALLSELECTED ( MyTable[pageTitle] ), Sgn * [Total PageViews] )

Sweet! I liked this solution as it is simple enough to understand and for my particular scenario. Thanks a lot to everyone else for their soultions!

Anonymous
Not applicable

Hello @ebecerra ,

We can achieve it by a little different method. By adding two rank functions in an if-else clause. The issue is RANKX function couldn't accept variables in the ORDER attribute.

You can find the solution and measure formula below :

NeelPatel_0-1638861138727.png

NeelPatel_1-1638861202677.png

NeelPatel_2-1638861221025.png

 

Thanks,
Neel

 

 

 



amitchandak
Super User
Super User

@ebecerra , better create two rank Var or measure with Asc and desc rank and use that

 

Example - All measures

Rank1 = Rankx(Allselected(Sales), [Sales],,asc,dense)

Rank2 = Rankx(Allselected(Sales), [Sales],,desc,dense)

 

if(selectedvalues(Table[Sort Order]) =1, [Rank1], [Rank2] )

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.