Sort legend based on a measure field

It’s possible to sort legend in alphabetic ascending/descending order. But is there any option to sort legend based on a measure? For example - In a pie chart legend, I want to show top 5 sales region name sorted by sales amount. Thank you.

1 Like

Good question, I’ve never done it before.

There is an option of ‘data source’ as the ‘legend source order’. However this option is in the CHART properties not in the legend. Maybe try that?

Thank you for the suggestion :slight_smile: . I have already tried all those in chart option, nothing provided the desired output.

If the measure in the metric set is sorted descending then the legend will be sorted in the same way.

But if you only want to display 5 categories in your legend, and your pie chart has > 5 categories it gets tricky.

You could create a duplicate chart - and then have your duplicate pie chart only display the top 5 categories - and then drag your duplicate off canvas and create a legend connected to the off canvas pie chart.

If the styling is the same you can it looking like this:

However this solution is not very “clean” :slight_smile:

Or go totally script based. Look in the chart to get the top 5 and then change the colour of rectangles and text in labels alongside.

This might work, I’ll try … cool stuff … thanks :slight_smile:
yes, my pie chart has more than 5 categories, but I need to show top 5 in the chart as well as in the legend. I was able to show top 5, just couldnt sort them based on the numeric field.

Thank you so much. Keep up the good work :+1:

I wanted to avoid scripts, but if nothing works then I might turn towards script. As an alternative, I was thinking of creating top 5 sorted table and show it as legend with state colors, all messy solutions for a small thing :stuck_out_tongue:

Thank you so much.