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 Categories ("concrete Projects") each.

 

I have a slicer that allows to select one or more "Project Types".

When more than one Project Type is selected, I want my bar chart legend to display the effect by project type.

When only one Project Type is selected, I want it to display the effect of this one type by "concrete Projects" .

 

I achieved the desired effect by creating a field parameter and inserting the respective slicer. Having to manually switch between both options.

This is cumbersome and I want the switching to take place automatically without the need for user action.

 

My approach so far has been to tinker with the hierarchical position of the field parameter options:

 

Legend Level = {

("Project Type", NAMEOF('Table'[Project Type]), 1),
("concrete Project", NAMEOF('Table'[concrete Project]), if(isfiltered([Project Type]),0,2))
}
 
This dynamic reordering of field parameters does not seem to work.
 
How do I achieve the automatic switching between Legend Levels?
  • 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.

4 Replies

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

    • Test_User_8934's avatar
      Test_User_8934
      Frequent Visitor

      Unfortunately, that does not have any effect either. I like the idea though.

      • johnt75's avatar
        johnt75
        Icon for Super User rankSuper User

        did you put the TopN filter on the field parameter ? what behaviour are you seeing ?