Often you may want to change the order of the items in a legend in Power BI.
Fortunately this is easy to do by creating a new column that specifies a custom order, then by using this new column as the legend in the chart.
The following example shows how to so in practice.
Example: How to Change Legend Order in Chart in Power BI
Suppose we have the following table in Power BI named my_data that contains information about items received, located, packed and shipped for two different retail stores:
Suppose that we would like to create a stacked column chart in which the bars in the chart are stacked in the following order:
If we create a stacked column chart, Power BI will simply place the items in each bar in alphabetical order by default:
To specify a custom order for the items in the legend, we must first create a new column that assigns the following numeric values to the various categories:
- Received – 1
- Located – 2
- Packed – 3
- Shipped – 4
To do so, click the Table tools tab and then click the New column icon:
Then type the following formula into the formula bar:
Stage Order = SWITCH('my_data'[Stage], "Received", 1, "Located", 2, "Packed", 3, "Shipped", 4)
This will create a new column named Stage Order:
Next, repeat the process to create another column that is a copy of the original Stage column by typing the following formula into the formula bar:
Stage Copy = 'my_data'[Stage]
This will create a new column named Stage Copy:
Next, with the Stage Copy column selected, click the Sort by column icon, then click Stage Order from the dropdown menu:
Next, switch back to the report view and click the stacked column chart to make it active.
Then drag the Stage Copy field under Legend:
Then right click Stage Copy, then click Rename for this visual, then name it Stage.
The items in each bar of the stacked column chart will now be sorted in the order that we specified:
The following tutorials explain how to perform other common tasks in Power BI: