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
Ravi_G
New Member

Dynamic Legends based hierarchy selection

Hi All,

 

I have matrix visual with 4 levels data in form of heirarchy , if we select level 1 values in matrix i want show level 2 values in Line chart , if we select level 2 in matrix visual, in lin chart i want to show level 3 data and so on.

i want to show dynamic levels in line chart legend.

Level 1Level 2Level 3Level 4Sum of AmountFY
ALevel1.1Level2.1Level3.1-458970FY22
ALevel1.1Level2.1Level3.2-458970FY22
ALevel1.1Level2.2Level3.3-458970FY22
ALevel1.1Level2.2Level3.4-458970FY22
ALevel1.1Level2.3Level3.5-458970FY22
ALevel1.1Level2.3Level3.6-458970FY22
BLevel1.2Level2.4Level3.7-458970FY22
BLevel1.2Level2.4Level3.8-458970FY22
BLevel1.2Level2.5Level3.9-458970FY22
BLevel1.2Level2.5Level3.10-458970FY22
BLevel1.2Level2.6Level3.11-458970FY22
BLevel1.2Level2.6Level3.12-458970FY22
CLevel1.3Level2.7Level3.13-458970FY22
CLevel1.3Level2.7Level3.14-458970FY22
CLevel1.3Level2.8Level3.15-458970FY22
CLevel1.3Level2.8Level3.16-458970FY22
CLevel1.3Level2.9Level3.17-458970FY22
CLevel1.3Level2.9Level3.18-458970FY22

 

4 REPLIES 4
rohit1991
Super User
Super User

Hi @Ravi_G

 

Power BI doesn’t let you directly switch the legend column based on a slicer, which makes dynamic legends tricky. But there’s a workaround that’s worked well for me.

 

What I usually do is create a separate slicer table (not related to the model) that just holds the level names  like “Level 1”, “Level 2”, “Level 3”. Based on what’s selected there, you can build a calculated column or measure that switches between the actual columns in your hierarchy.

Legend Label =
SWITCH(
    SELECTEDVALUE(LevelSelector[Level]),
    "Level 1", Table[Level2],
    "Level 2", Table[Level3],
    "Level 3", Table[Level4]
)

Then you use this Legend Label as the legend in your chart. It’s not truly dynamic in the sense of changing the metadata of the chart, but it gets the job done visually.

 

Just be aware that this works best when your hierarchy is flattened (i.e. columns for each level). If you're using a parent-child hierarchy, it’ll need a slightly different setup.

 


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

Hi @Ravi_G ,

Hi there, just following up on your query about Dynamic Legends based hierarchy selection. @rohit1991  has provided a response that may address your concern.

If his solution worked for you, please consider marking it as the Accepted Answer — this helps others in the community who might have the same question.

If you're still experiencing issues or need further clarification, feel free to add more details — we’re here to help!

 

Thanks again for being part of the Microsoft Fabric Community!

freginier
Super User
Super User

Hey there!

 

Here’s how you can achieve that using DAX measures and potentially slicers or selection controls in Power BI:

Create a Dynamic Measure for Line Chart: You'll need a dynamic DAX measure that changes its behavior based on the selected level from the matrix. For example, you can use a measure like this:

DynamicLevelMeasure =
SWITCH(
TRUE(),
SELECTEDVALUE('Hierarchy'[Level]) = 1, SUM('Data'[Level1Amount]),
SELECTEDVALUE('Hierarchy'[Level]) = 2, SUM('Data'[Level2Amount]),
SELECTEDVALUE('Hierarchy'[Level]) = 3, SUM('Data'[Level3Amount]),
SELECTEDVALUE('Hierarchy'[Level]) = 4, SUM('Data'[Level4Amount])
)

 

You can add a slicer or matrix to allow users to select which level they want to display. The slicer will control which level the line chart visual shows.

Create a line chart and add the DynamicLevelMeasure as the value.

For the x-axis, you can use a time-based or sequential field (e.g., a date or period column).

Ensure that the slicer (or matrix) that controls the level is properly interacting with the line chart. You can adjust the interactions via the Format pane in Power BI to control how selections in the matrix or slicer affect the line chart.

 

Hope this helps!

😁😁

 

 

Thank you for the reply ..i want claculated column that i need to place in Legend of line chart. and how we are assigning 1 to level SELECTEDVALUE('Hierarchy'[Level]) = 1 in above measure using parameters?

 

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.