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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Arenek
New Member

Rankx, Ranking by state and by city (multiple level of hierarchy) using Matrix style table

The right below data by State and by City sales needs to be ranked based on sales....the tricky part is that I would like to rank this within 1 MATRIX style table visual (not in 2 separate tables), so I can toggle/switch between both variables to produce the ranking by state and by city as you can see further below (this is called ranking by multiple level of hierarchy)

 

I know that to rank by state (for example),  you need to write something like this: 

RANKX(ALLSELECTED(table [State]), calculate(sum[sales])

 

Does anybody know the DAX to accomplish ranking by state and by city ?

 

StateCitySales
CaliforniaSan diego23
CaliforniaLos Angeles55
CaliforniaSan Francisco35
FloridaMiami32
FloridaOrlando48
FloridaJacksonville44
GeorgiaAtlanta46
GeorgiaAugusta31
GeorgiaSavannah53
   
StateRank 
Georgia1 
Florida2 
California3 
   
CityRank 
Los Angeles1 
Savannah2 
Orlando3 
Atlanta4 
Jacksonville5 
San Francisco6 
Miami7 
Augusta8 
San diego9 
5 REPLIES 5
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1673374046377.png

 

 

Rank measure: =
SWITCH (
    TRUE (),
    ISINSCOPE ( Data[City] ), RANKX ( ALL ( Data[City] ), CALCULATE ( SUM ( Data[Sales] ) ) ),
    ISINSCOPE ( Data[State] ), RANKX ( ALL ( Data[State] ), CALCULATE ( SUM ( Data[Sales] ) ) )
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thank you. 

I was looking to do that within 1 Matrix style table, not in 2 separate tables....I believe it is called "ranking by multiply hierarchy levels" in a table.

 

I believe you have to use VAR in your DAX in combination with something like this: RANKX(ALLSELECTED(table [State]), calculate(sum[sales]).

 

Please let me know if you know the solution.

 

Regards.

 

 

 

 

 

Hi,

Thank you for your feedback.

Please check the below picture and the attached pbix file whether one of two visualizations is what you are looking for.

 

Jihwan_Kim_0-1673375534232.png

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Hi again,

 

The sample you provided works perfectly, but it doesn't work with my data.

Could it be because the location (state and city) are in a separate table I joined with the sales table ?

 

Regards

Hi,

Thank you for your message.

I assume your data model looks something like below.

If my assumption is wrong, please provide your sample pbix file's link.

 

Please check the below picture and the attached pbix file whether it suits your requirement.

 

Jihwan_Kim_0-1673409935360.png

 

Jihwan_Kim_1-1673410255266.png

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.