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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
SandeepSathya
New Member

Stacked Column chart - Custom sort X axis based on Count of legend

Hello, 
I need help with sorting the chart based on the value count. 

Sample data:

InstanceCategory
Instance 1Low
Instance 1Medium
Instance 2Medium
Instance 2Critical
Instance 2Critical
Instance 2Critical
Instance 3Low
Instance 3Medium
Instance 3Medium
Instance 3Low
Instance 3Critical
Instance 1Critical

 

Based on the data I have following chart created:

SandeepSathya_4-1694000401678.png

 

As expected, defaut sort is based on Total count (descending). 

My requirement is that: X axis sort order should be based on the logic : 

Instance with highest # of critical should be left most.
Logic: first compare # of critical for the Instance, if same then # of medium, if same then # of low. (not based on the total # under each instance)

So based on my data I want the graph to be sorted in following order:

First vertical should be Instance 2 as it has highest # of critical (value 3)

Second vertical should be Instance 3 (has same # of critical as Instance 1, but # of Medium for Instance 3 is higher than Instance 1)

Third vertical should be Instance 1.

 

Any suggestion on how to sort X axis based on custom rule for the 'Count of legend' would be of great help! Thanks in advance

2 REPLIES 2
SamInogic
Super User
Super User

Hi,

As per our understanding you're trying to sort the X-axis in a stacked column chart based on the count of a specific category (e.g., "Critical"), then break ties using "Medium", and finally "Low". Since Power BI does not allow direct sorting based on legend values, here’s a complete workaround using calculated measures and a helper table.

Steps to Follow

  1. Create Measures

Go to Modeling → New Measure, and add:

CriticalCount = CALCULATE(COUNT('InstanceData'[Instance]), 'InstanceData'[Category] = "Critical")

MediumCount = CALCULATE(COUNT('InstanceData'[Instance]), 'InstanceData'[Category] = "Medium")

LowCount = CALCULATE(COUNT('InstanceData'[Instance]), 'InstanceData'[Category] = "Low")

 

SortOrder = [CriticalCount] * 10000 + [MediumCount] * 100 + [LowCount]

 

  1. Create a Sorting Table

Go to Modeling → New Table:

InstanceSortTable =

SUMMARIZE(

    'InstanceData',

   'InstanceData'[Instance],

    "SortOrder",

        CALCULATE(COUNTROWS('InstanceData'), 'InstanceData'[Category] = "Critical") * 10000 +

        CALCULATE(COUNTROWS('InstanceData'), 'InstanceData'[Category] = "Medium") * 100 +

        CALCULATE(COUNTROWS('InstanceData'), 'InstanceData'[Category] = "Low")

)

Then go to Data view, select InstanceSortTable[Instance], and Sort by Column → SortOrder.

 

  1. Create a Relationship

In Model view, relate:

InstanceSortTable[Instance] → InstanceData[Instance]

 

  1. Create the Chart

Use a Stacked Column Chart:

  • X-axis: InstanceSortTable[Instance]
  • Y-axis: Count of InstanceData[Instance]
  • Legend: InstanceData[Category]

 

This will sort the X-axis based on:

  • Highest Critical count
  • Then Medium
  • Then Low

Please find the attached pbix file for reference.

 

Hope this helps.

 

Thanks!

Inogic

 

Inogic Professional Services: Power Platform/Dynamics 365 CRM
An expert technical extension for your techno-functional business needs
Service: https://www.inogic.com/services/
Tips and Tricks: https://www.inogic.com/blog/
Palak_Agarwal
New Member

Hello @SandeepSathya 
Power BI doesn’t let us sort the X‑axis directly by values in the legend, but I’ve created a handy workaround:
Create 3 measures to get the count of each category using CALCULATE() for eg:
CALCULATE(COUNT('Table'[Instance]),'Table'[Category] = "Critical")
Similarly calculate the count of Medium and Low.

Then I created a measure for sorting in which I multiplied the critical count with a large constant number to make the count large (I have used 10000 but you can use any value based on the highest value in your data). Then I multiplied the count of medium with a constant smaller than before so that the value is smaller than the count of critical and left count of low as it is, then added all three.
This makes the values weighted with critical having the highest weight and low having the lowest. Refer to the image below:
unnamed.png


Then add this measure in the tooltip field and then sort the axis by this sort measure by clicking on the 3 dots in the upper right corner of the chart and select sorting measure in the sort axis.

If this post helps, please accept this as a solution. Appreciate your kudos.

Regards,

Palak

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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