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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! 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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.

Top Solution Authors