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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
itskool
Advocate II
Advocate II

Sort stacked bar chart - add secondary criteria in case total values are the same

Hi All,

 

I've run into a weird problem: when I create stacked bar chart and sort it by total value, when then total values are the same, the visual gives preference to the bar where there are more yellow values than red. The business requirement is to make it vice versa.
I've added a sort column to the column which is in the legend bucket, tried it both ways (red 1 yellow 2, red 2 yellow 1), it is still the same. 

Funnily enough, if I sort it ascending , it works as I expect it (5 is below 3), so it makes me think that this order is random.
Any ideas? Is there a way to add this secondary sorting criteria?

itskool_0-1726055281867.png

 

1 ACCEPTED SOLUTION
dk_dk
Super User
Super User

Hi @itskool 

I have a solution, it is a bit wonky but seems to work nonetheless.

Assuming you have data that looks something like this:

dk_dk_0-1726057595166.png


And you create a chart based on this table, where Item is the Y-axis, Sum of Value is the X-axis, and Color is the Legend.


 

  1.  Create a helper table (called Sorting) that contains only the unique  Items from your main table. Many ways to do this, I did it in Power Query, by referencing the main table, removing all other columns except Item and removing duplicate rows.
  2. add a calculated column to the Sorting table:


    SortOrderHelper =
    VAR curritem = Sorting[Item]
    VAR totVal = CALCULATE(SUM('Table'[Value]),'Table'[Item]=curritem)
    VAR redVal = CALCULATE(SUM('Table'[Value]),'Table'[Item]=curritem, 'Table'[Color]="Red")

    RETURN
    totVal+(redVal/100)​


    This will calculate a custom sort order, primarily based on the total value, but in case of tiebreaks it will give a higher rank to the item with more red.
  3. Create a calculated column in your original table that looks up this value:

    SortOrder = LOOKUPVALUE(Sorting[SortOrderHelper],Sorting[Item],'Table'[Item])​

  4. Add the SortOrder column to your visual, into the tooltips section. Set aggregation to Max (though Min, Sum, or Avg should also work)
  5. Sort axis by Max of SortOrder, and it should get you the correct sorting with red being prioritised for tiebreaks.

    dk_dk_1-1726057980378.png

Note: Depending on your data, the division by 100 might not be sufficient  (if you have 100+ red values for an item, it might mess up the order completely). You can change it to a higher number, or create a VAR that calculates the largest red value dynamically, add +1 to that and divide by that amount (this is so you never have a redVal fraction that is > 1).
Also make sure that the calculated columns you create are formatted as decimal numbers, with enough decimals to show the difference in the tiebreaks.


I hope this helps, let me know if you have any questions. 







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

Proud to be a Super User!





View solution in original post

3 REPLIES 3
ribisht17
Super User
Super User

 

"➨Please mark the relevant answer it it helped you so that it can help others too

  ∎  Help the community back!

dk_dk
Super User
Super User

Hi @itskool 

I have a solution, it is a bit wonky but seems to work nonetheless.

Assuming you have data that looks something like this:

dk_dk_0-1726057595166.png


And you create a chart based on this table, where Item is the Y-axis, Sum of Value is the X-axis, and Color is the Legend.


 

  1.  Create a helper table (called Sorting) that contains only the unique  Items from your main table. Many ways to do this, I did it in Power Query, by referencing the main table, removing all other columns except Item and removing duplicate rows.
  2. add a calculated column to the Sorting table:


    SortOrderHelper =
    VAR curritem = Sorting[Item]
    VAR totVal = CALCULATE(SUM('Table'[Value]),'Table'[Item]=curritem)
    VAR redVal = CALCULATE(SUM('Table'[Value]),'Table'[Item]=curritem, 'Table'[Color]="Red")

    RETURN
    totVal+(redVal/100)​


    This will calculate a custom sort order, primarily based on the total value, but in case of tiebreaks it will give a higher rank to the item with more red.
  3. Create a calculated column in your original table that looks up this value:

    SortOrder = LOOKUPVALUE(Sorting[SortOrderHelper],Sorting[Item],'Table'[Item])​

  4. Add the SortOrder column to your visual, into the tooltips section. Set aggregation to Max (though Min, Sum, or Avg should also work)
  5. Sort axis by Max of SortOrder, and it should get you the correct sorting with red being prioritised for tiebreaks.

    dk_dk_1-1726057980378.png

Note: Depending on your data, the division by 100 might not be sufficient  (if you have 100+ red values for an item, it might mess up the order completely). You can change it to a higher number, or create a VAR that calculates the largest red value dynamically, add +1 to that and divide by that amount (this is so you never have a redVal fraction that is > 1).
Also make sure that the calculated columns you create are formatted as decimal numbers, with enough decimals to show the difference in the tiebreaks.


I hope this helps, let me know if you have any questions. 







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

Proud to be a Super User!





ribisht17
Super User
Super User

hi @itskool 

The game here is to reverse the alphabetical order of the Legend 

 

 

"f" comes first, hence it is on the LHS

ribisht17_1-1726058437364.png

 

 

I wrote a calculation and bring the same to legend and I was able to reverse it

 

ribisht17_0-1726058373856.png

 

 

Regards,

Ritesh

"➨Please mark the answer it it helped you so that it can help others too

  ∎  My You tube Channel Dance-Sing with Data Projects - YouTube"

 

 

 

 

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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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