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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
schoden
Post Partisan
Post Partisan

Sort Legend Dynamically By its Value

Hi Community, 

 

Is it possible to sort the legend dynamically by the value in its column( highest to lowest)  

There is hardcoded way which is not I am after or alphabetically sorting.

 

Sort each bar with highest value as first stacked bar, least value on top.

 

Just like the Feb Month highest value first (Black color), Red color the least value on top of the bar.

 

Legend.JPG 

1 ACCEPTED SOLUTION

@schoden did you check out the solution I put forward? I attached the PBIX in an earlier reply. I believe it is what you were after. Hope it helps!

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

View solution in original post

8 REPLIES 8
TheoC
Super User
Super User

@schoden figured out how to do it. There is very limited stuff out and about but anyway, hopefully the below screenshot is kind of what you were wanting?

 

TheoC_0-1633049912075.png

You will need to create the following measures and two tables:

TheoC_1-1633050089772.png

TheoC_2-1633050227656.png

TheoC_3-1633050235287.png

TheoC_4-1633050256528.png

TheoC_5-1633050281231.png

TheoC_6-1633050294672.png

TheoC_7-1633050307220.png

TheoC_9-1633050326765.png

S_Table2_Cat uses Summarised. 

TheoC_10-1633050358188.png

S_Table3_Status is just "Enter Data" and column title "Status" with Top, Middle, Bottom as data.

Relationships in Model view as follows:

TheoC_11-1633050417218.png

Hope it's what you're after mate! Was interesting to say the least lol!

 

 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

TheoC
Super User
Super User

Hi @schoden, if you assign a number to each color (i.e. 1, 2, 3), you can then "Sort by Column" under the "Column Tools" ribbon.  I have applied something similar to the Covid Dashboard (https://www.axiar.com.au/covid19-australia) in terms of Close, Low Risk and Casual Contact exposures.

 

Best way to do this is by going to "Transform Data" and creating a Conditional Column.  If "color" equal "Black" then 1, Blue then 2, Red then 3, and so on. Close and apply. Click the "Color" field in the right Fields pane, then go to "Sort by Column" and sort by the new column. Finito 🙂

 

Let me know if you'd like more detailed steps 🙂

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

@schoden if you want to use dax to achieve the above, you can create a summarised table, add a Calculated Column using SWITCH to change the color name to a digit, and link the summarised Color field to original table. Then create a Calculated Column using "Related" in the original table to bring back the digits.

  1. Color Table = SUMMARIZE ( Table1 , [Color]) 
  2. Color Rank = SWITCH ( [Color] , "Black" , 1 , "Blue" , 2 , "Red" , 3 , 0 )
  3. Relationship Color Table [Color] to Table1 [Color] (one is to many)
  4. In Table1 create Calculated Coloumn
    1. rColor = RELATED ( Color Table[Color] )

Done 🙂

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Hi @TheoC  This will always put Black color as 1, Blue as  2, Red as 3 as its hardcoded, 

I want dynamically sorting based on values  alloted to the colors. 

 

Feb  ( Black= 7, Blue=6, Red= 5 )  which is stacked as Black, Blue , Red .

March ( Blue= 8, Red=8, Black= 1) which is stacked as Blue , Red , Black.

 

@schoden ,I found this. Using the logic behind it, if the number of colors is fixed (I.e. 3 each month), you could create unique measures to total the count of a category by rank, then allocate ta colour to the rank... the rank (I.e. 1, 2 or 3) is used to sort the chart (apologies for typos, on phone) https://community.powerbi.com/t5/Desktop/How-do-I-color-the-stacked-bar-chart-by-Dax-query-according...

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Hi @TheoC  Thanks for the getting back to me. The solution provided will not work for me as everything is dynamic and number to the color is will varry and change.  

Thanks for your response though. appreciate it. 

@schoden I just finished adding a new comment with screenshots and all that fun stuff. Hopefully achieves what you want! Link to PBIX below!

 

PBIX File 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

@schoden did you check out the solution I put forward? I attached the PBIX in an earlier reply. I believe it is what you were after. Hope it helps!

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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