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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
AdamMetz23
Advocate I
Advocate I

Sorting column chart X axis by two level hierarchies

Dear Power BI Community.

 

Can you please help me with  the following issue if you have seen similar situation before?

Situation snippet: Here are values on a column chart. the measure values (32,29,29,36) should be sorted in descending order. However the trick is that it should consider the region column values (EAPRO, ECARO). It means that the measure value should be 32 for Myanmar, 29 for Papaue New Guinea in EAPRO but after when it comes to ECARO Region the descending order sort should start again and Ukraine should be 36 and then Turkiye should be 29. So the descending order logic should restart for each region.

 

AdamMetz23_0-1720688740904.png

 

Currently If I try to sort by the measure value descending, it disregards the region hiearchy:


AdamMetz23_1-1720689004278.png

 

The region values are not batched into one but only the measure value matters in the sorting.

 

Do you know a way how to create this hiearchy custom sorting by any chance?

Thank you a lot for your help and have a great day,

Adam

 

1 ACCEPTED SOLUTION

@AdamMetz23 ,

It's sometimes hard to find the problem without knowing the data model, but try using this code for a second sort:

RANK (
    SKIP,
    ALLSELECTED ( 'Table' ),
    ORDERBY ( 'Table'[Value], DESC, 'Table'[Group], ASC ),
    LAST,
    PARTITIONBY ( 'Table'[Group] )
)

This code does ranking based on values ​​and partitions by groups.

View solution in original post

9 REPLIES 9
lkalawski
Resident Rockstar
Resident Rockstar

Hi @AdamMetz23 ,

 

To sort values ​​within a group, you need to create an additional measure, which is best added as a tooltip.
This measure will sort the values ​​taking into account both the group and the values ​​that are in it.

1. Create measure:

Ranking =
VAR __CurrentGroup =
    SELECTEDVALUE ( 'Table'[Group] )
VAR __GroupRanking =
    COUNTROWS (
        FILTER ( ALL ( 'Table'[Group] ), 'Table'[Group] <= __CurrentGroup )
    )
VAR __ValueRanking =
    RANKX (
        FILTER ( ALL ( 'Table' ), 'Table'[Group] = MAX ( 'Table'[Group] ) ),
        CALCULATE ( SUM ( 'Table'[Value] ) ),
        ,
        DESC
    )
VAR __Result =
    __GroupRanking + DIVIDE ( __ValueRanking, 1000 )
RETURN
    __Result

This measure, as I wrote above, first sorts all groups alphabetically and then checks the sorting by nominal value. We then add the sum of these and get a result that takes both factors into account.

 

2. Add a measure as a tooltip to the visualization.

lkalawski_0-1720695938513.png

3. Set sorting using Ranking

lkalawski_1-1720695992790.png

The result:

lkalawski_2-1720696042633.png

If you have any further questions, feel free to ask.

 

If I helped, accept the post as a solution and give a kudos. 👍 Thanks! 😁

Hi Ikalawski,

 

Thank you for your great help. I did what you suggested, it does not seem to work yet.

 

AdamMetz23_0-1720710623865.png

 

AdamMetz23_1-1720710699322.png

 

AdamMetz23_2-1720710754952.png

 

Do you think I missed something?

Both columns the Country and Region comes from the same table and the measure is just a relatively easy CALCULATE on the same table to distinctcount the values for the countries and region:

AdamMetz23_3-1720710849293.png

 

 

 

I would be grateful if you could help but thank you already for your effort.

@AdamMetz23 ,

Please check what __GroupRanking returns and attach such a matrix for verification.

lkalawski_0-1720772020524.png

 

@lkalawski , thank you that is a great validation check idea.

 

I see the problem:

AdamMetz23_1-1720773512923.png

 

 

Even if Turkiye sum of value is 29 while Ukraine is 36, when it  comes to ranking both values are ranked as 2 etc. for the others.

 

Do you know by any chance where I made a mistake in the DAX? Every column (Group, Category) and value (Sum of Value measure) is in the same table so I think the issue is with the filter context somehow?

What do you think? 

Thank you a lot if you had time to check it but I understand if you have other things to do as well :).

 

 

@AdamMetz23 ,

The values ​​you currently have are fine because they don't take Value into account.
In this solution we combine two sorts. Please add __ValueRanking to this matrix - it should sort based on values.
And the sum of these two rankings creates the main sorting 🙂

@lkalawski Thank you.


Oh I see so the ranking is not based on the value currently but on the Group/Category.

 

I can see that if I look at the valueranking variable however it looks strange if you consider the ranking 267:

Ranking Test returns _ValueRanking on the print screen below:

AdamMetz23_0-1720776429128.png

 

Ideally for the visualizations the rank should be like this:

AdamMetz23_1-1720776544857.png

 

 

Would it be possible for you to help me modify the DAX Code to represent the print screen with the red indicated ranking structure above:

This is the code I would like to modify to reflect the changes:

AdamMetz23_2-1720776623859.png

 

Do I think correctly that the filter context needs to be changed somehow in the DAX?

Thank you.



@AdamMetz23 ,

It's sometimes hard to find the problem without knowing the data model, but try using this code for a second sort:

RANK (
    SKIP,
    ALLSELECTED ( 'Table' ),
    ORDERBY ( 'Table'[Value], DESC, 'Table'[Group], ASC ),
    LAST,
    PARTITIONBY ( 'Table'[Group] )
)

This code does ranking based on values ​​and partitions by groups.

@lkalawski , Thank you.

 

I have modified your original DAX a bit my adding ALLSELECTED to the _valueranking variable and it works perfectly now.

 

AdamMetz23_0-1720798971324.png

 

Thank you for your help once again and have a great weekend 🙂

amitchandak
Super User
Super User

@AdamMetz23 ,  if you use legend then you have an option to sort by value, within axis sorting . Check if that can help

amitchandak_0-1720694337126.png

 

 

 

 

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

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.