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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
ChristianCaz
New Member

Sort Legend Dynamically By field parameter

Hi everyone,

 

I have a stacked graph and it is made by a field parameter, however, I can't get the legend to be completely ordered by the rank, do you know how can I sort the legend of the graph?

 

Thank you!!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi, @ChristianCaz 

If you want to customize the sort order of the legend, then you can create a calculated column and use this column to sort. Here's a reference:

I have the following chart, the default sort is A, B, C:

vjianpengmsft_0-1713329162554.png

I created a calculated column with a custom order using the following DAX:

sort column = SWITCH('Table'[Type],
    "A",3,
    "B",1,
    "C",2
)

vjianpengmsft_1-1713329332341.png

I copied the Type column with the following DAX:

vjianpengmsft_2-1713329369100.png

Sort the Type copy columns according to the following image:

vjianpengmsft_3-1713329444576.png

Put the Type copy column in the legend of the chart:

vjianpengmsft_4-1713329515707.png

You can use your rank column to sort your legend columns. Keep in mind, however, that a legend corresponds to a unique ranking. In my example, I have two rows Type=A, both of which rank 3.

vjianpengmsft_5-1713329661285.png

If there are two different rankings corresponding to legend A, an error will be reported.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

View solution in original post

2 REPLIES 2
Doccrs
Regular Visitor

I know this question is a year old now. But the accepted solution is NOT a solution.

The OP ask how they do this on a Field Parameter (FP) - the solution given does not work when using a field parameter. You cannot use calc columns in a legend where an FP has been used on the Y-Axis. As of Oct-2025 there is still no solution to this.

Anonymous
Not applicable

Hi, @ChristianCaz 

If you want to customize the sort order of the legend, then you can create a calculated column and use this column to sort. Here's a reference:

I have the following chart, the default sort is A, B, C:

vjianpengmsft_0-1713329162554.png

I created a calculated column with a custom order using the following DAX:

sort column = SWITCH('Table'[Type],
    "A",3,
    "B",1,
    "C",2
)

vjianpengmsft_1-1713329332341.png

I copied the Type column with the following DAX:

vjianpengmsft_2-1713329369100.png

Sort the Type copy columns according to the following image:

vjianpengmsft_3-1713329444576.png

Put the Type copy column in the legend of the chart:

vjianpengmsft_4-1713329515707.png

You can use your rank column to sort your legend columns. Keep in mind, however, that a legend corresponds to a unique ranking. In my example, I have two rows Type=A, both of which rank 3.

vjianpengmsft_5-1713329661285.png

If there are two different rankings corresponding to legend A, an error will be reported.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.