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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
mandyporter
New Member

Stacked bar - need legend in right order

I am really really new to Power BI, but I have had quite a good search to try to find a solution...

 

I have a births dataset (one record for each birth). I want a stacked bar % chart with calendar year of birth on the x-axis and a record count on the y-axis (each record has a "record=1" value so it's the sum of that variable). The legend is age group of the mother. I have seven categories. I had two variables in my dataset: agegpmother that was just labels (i.e. Under 18, 18-19, 20-24, 25-29, 30-34, 35-39 and 40+) and another variable agegpmothersort was  numerical with values 1-7 which matched the labels so 1=Under 18, 7=40+. When I add agegpmothersort into the legend of the stacked bar chart I get the stacks in the right order (under 18s at the bottom and 40+ at the top) but I get values 1-7 in the legend which I don't want. If I add agegpmother into the stacked bar chart, I get the right labels for the age groups, but I get all the stacks in the wrong order (starts with 18-19 at the bottom and finishes with 40+ second from the top and under 18 at the top).

 

I thought one solution was to create a new table "age groups" which had seven records and two variables - agegp 1-7 and agedescription (Under 18, 18-19, 20-24, 25-29, 30-34, 35-39 and 40+). I then linked the agegp variable from the age groups table with the numerical agegpmothersort variable in the main analysis dataset with a one-to-many relationship applied in both directions.  I've tried variations of adding in these four variables (agegpmother, agegpmothersort, agegp, agedescription) into the legend, and the numerical variables into the "tooltips" box and selecting "minimum of the tooltips" variable. When I added a variable into the tooltips for other charts (simple bar charts not stacked bar charts) I could click on the three dots beside the chart and had the option to order by the tooltips variable. This is not the case here, it only gives me the option of ordering by whatever variable is in the legend box.

 

I saw something online about sorting by a variable. I don't want all other charts ordered by age of mother that are showing data for something entirely dfiferent, but I have "age groups" table and tried to sort "agegp" which is my numerical variable 1-7. This doesn't make any difference though.

 

How can I have both the right order and the right variable value labels at the same time? Nothing seems to work. It seems such a simple thing that I want to do! Apologies if it's a really simple solution - this is the first time I've used Power BI.

2 REPLIES 2
Anonymous
Not applicable

Hi @mandyporter 

Based on your needs, I have created the following table:

vjialongymsft_0-1738044881773.png

 

First, you can create an index column in Power Query, just like the other numeric column you mentioned in your question.

 

vjialongymsft_1-1738045085179.png

 

Then select the 'age group' column and click to sort by the index column.

vjialongymsft_2-1738045294525.png

 

vjialongymsft_3-1738045316792.png



Result:

vjialongymsft_4-1738045338329.png

 

 

 

 

 

Best Regards,

Jayleny

 

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

 

 

Tutu_in_YYC
Super User
Super User

Have you tried this? You might only need to have a custom sort order.
https://radacad.com/sort-a-column-with-a-custom-order-in-power-bi

FYI: if you add a diagram, screenshot or a sketch of what is happening and what you want to achieve, it will be easier for the community to visualize and find a solution for you.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.

Users online (1,964)