Forum Discussion

Ravi_G's avatar
Ravi_G
New Member
1 year ago

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

  • freginier's avatar
    freginier
    Solution Sage

    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!

    😁😁

     

     

    • Ravi_G's avatar
      Ravi_G
      New Member

      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?

       

  • 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.

     

    • v-aatheeque's avatar
      v-aatheeque
      Community Support

      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!