Forum Discussion
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 = {
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 Resultwhich 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
- johnt75
Super User
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 Resultwhich 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_8934Frequent Visitor
Unfortunately, that does not have any effect either. I like the idea though.
- johnt75
Super User
did you put the TopN filter on the field parameter ? what behaviour are you seeing ?