DAX – Filtering Measure to show value when selected or no values selected
I had a requirement where I had a measure that I wanted to be filtered if it’s value was selected
then it must show the value. As well as if nothing is selected on the slicer then I also
want the value to be displayed.
I did this so that when the users are using the report, it will clearly show them what values they are looking at in the table. By making it clearly visible
once they selected a value from a slicer it is easier for the end users to get the right data.
This is best shown with an example below.
Current behavior is when I click on Day Shift it shows me the values for the Day Shift.
- NOTE: In the above the 24hr Total does
change to correctly reflect the Day Shift values.
Now when I click on Night Shift, I still see the values for Day Shift
- NOTE: In the above the 24hr Total does change to correctly reflect the Night Shift values.
So below is the DAX that I used to overcome this challenge so that it would display correctly.
Here is the syntax with an explanation afterwards
Day Shift = IF ( ISFILTERED ( 'Prod'[Shift] ) && VALUES ( 'Prod'[Shift] ) = "Day Shift" || NOT ( ISFILTERED ( 'Prod'[Shift] ) ), CALCULATE ( [UnitType (Selected)], 'Prod'[Shift] = "Day Shift" ), 0 )
- What is happening above is I have got an IF Statement and what I am doing is first to see if the column is being filtered. This uses the ISFILTERED and is highlighted in PURPLE above.
- Next is where I am also looking to see if the “Day Shift” has been selected from the Slicer, this is highlighted in ORANGE
- I put in an OR “||” Condition, and I used the NOT which is then saying it is NOT FILTERED, this is so that if neither “Day Shift” or “Night Shift” is selected then also make this part of the IF Statement TRUE, this is highlighted in GREEN
- Now that I had completed the Condition for my IF statement I then put in my calculation if it is TRUE, which is highlighted in BLUE
- And then finally if the Slicer has “Night Shift” selected make the value for “Day Shift” equal 0
So it is easier to see it in action as shown below.
Here is showing where nothing is selected on the Slicers it shows all the values.
Next if I select “Day Shift” I want it to show the values for Day Shift, but make “Night Shift” zero. As well as the 24hr Total should only show the Day Shift values.
And finally, if I select “Night Shift” I want it to show the values for Night Shift, but make “Day Shift” zero. As well as the 24hr Total should only show the Day Shift values.
As you can see above I have demonstrated how to leverage DAX measures so that it will display the values in a way that is meaningful to the end users of the report.
This will help them easily understand the data that they are looking at, and not have to second question or double check to see if the figures are the ones that they are expecting to see.