Forum Discussion

Test_User_8934's avatar
Test_User_8934
Frequent Visitor
1 year ago
Solved

Change Bar Chart Legend based on isfiltered()

I have a bar chart that displays the effects of projects by year.   The projects are hierarchically ordered in the following way: 5 Parent Categories ("Project Type") contain 5-10 Child Categorie...
  • johnt75's avatar
    1 year ago

    Your ISFILTERED approach won't work because a field parameter is basically a calculated table, and they are only calculated during data refresh, so they cannot take into account any slicers or filters.

    If you revert the field parameter table to what it was originally, or create a new one, you can create a measure like

    Selected Legend Level =
    VAR VisibleProjectTypes =
        COUNTROWS ( VALUES ( 'Table'[Project Type] ) )
    VAR CurrentLegendLevel =
        SELECTEDVALUE ( 'Legend Level'[Legend Level Order] )
    VAR Result =
        IF ( VisibleProjectTypes = 1 && CurrentLegendLevel = 0, 1, 0 )
    RETURN
        Result
    

    which will return 1 if there is only 1 project type selected and the appropriate legend level is in the filter context.

    On your bar chart use the field parameter as the legend and add a TopN filter to only show the top 1 rows, using the new measure as the value. It will automatically change the legend level based on the user interactions.