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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
ribs
Helper I
Helper I

Sort the RankX results in asc or desc based on user's input through slicer

I could get the RankX working but I don't want to display the RankX measure in the matrix rather I want to add it to the filter pane of the visual. The problem is I can't tell if the results are sorted in ascending or descending or the Ranks are mixed. How do I get this working through DAX or better ask the user to dynamically select ASC or DSC

Here is my RankX function;

Dynamic Rank =
VAR SelectJahr= SELECTEDVALUE(RankYearSelector[Year])
RETURN
SWITCH(
    TRUE();
    SelectJahr== 'From'[From Value];RANKX(ALLSELECTED(Customer[Name]); [Dynamic Revenue 1];;DESC;Dense);
    SelectJahr== 'To'[To Value 2]; RANKX(ALLSELECTED(Customer[Name]);[Dynamic Revenue 2];;DESC;Dense)
)
 
The matrix has 3 columns: Customers, Revenue1 Year, Revenue2 Year. This compares the revenue between two years that is specified through parameters- 'From'[From Value] and 'To'[To Value 2]
This parameter- RankYearSelector[Year]- user selects based on which Yearof the two above to Rank

Thank you in advance

1 ACCEPTED SOLUTION
BeaBF
Super User
Super User

@ribs Hi!

First, create a new table with this code:

SortDirection =
DATATABLE("SortOrder", STRING, {{"ASC"}, {"DESC"}})

 

Second, update your measure:

Dynamic Rank =
VAR SelectJahr = SELECTEDVALUE(RankYearSelector[Year])
VAR SortOrder = SELECTEDVALUE(SortDirection[SortOrder])
VAR RevenueValue =
SWITCH(
TRUE(),
SelectJahr = 'From'[From Value], [Dynamic Revenue 1],
SelectJahr = 'To'[To Value 2], [Dynamic Revenue 2],
BLANK()
)
RETURN
SWITCH(
TRUE(),
SortOrder = "ASC", RANKX(ALLSELECTED(Customer[Name]), RevenueValue, , ASC, Dense),
SortOrder = "DESC", RANKX(ALLSELECTED(Customer[Name]), RevenueValue, , DESC, Dense),
BLANK()
)

 

Now, add your Dynamic Rank measure to the visual-level filter pane.

Set a condition like Dynamic Rank is less than or equal to 5 or whatever top N filter you want.

 

BBF


💡 Did I answer your question? Mark my post as a solution!

👍 Kudos are appreciated

🔥 Proud to be a Super User!

Community News image 1920X1080.png

View solution in original post

2 REPLIES 2
ribs
Helper I
Helper I

Thank you! That worked😊

BeaBF
Super User
Super User

@ribs Hi!

First, create a new table with this code:

SortDirection =
DATATABLE("SortOrder", STRING, {{"ASC"}, {"DESC"}})

 

Second, update your measure:

Dynamic Rank =
VAR SelectJahr = SELECTEDVALUE(RankYearSelector[Year])
VAR SortOrder = SELECTEDVALUE(SortDirection[SortOrder])
VAR RevenueValue =
SWITCH(
TRUE(),
SelectJahr = 'From'[From Value], [Dynamic Revenue 1],
SelectJahr = 'To'[To Value 2], [Dynamic Revenue 2],
BLANK()
)
RETURN
SWITCH(
TRUE(),
SortOrder = "ASC", RANKX(ALLSELECTED(Customer[Name]), RevenueValue, , ASC, Dense),
SortOrder = "DESC", RANKX(ALLSELECTED(Customer[Name]), RevenueValue, , DESC, Dense),
BLANK()
)

 

Now, add your Dynamic Rank measure to the visual-level filter pane.

Set a condition like Dynamic Rank is less than or equal to 5 or whatever top N filter you want.

 

BBF


💡 Did I answer your question? Mark my post as a solution!

👍 Kudos are appreciated

🔥 Proud to be a Super User!

Community News image 1920X1080.png

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors
Top Kudoed Authors