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.
- NOTE: In the above the 24hr Total does
-
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.
Conclusion
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.
can i have the pbix file for it and underline model please. I am struggling with same situation where user can select like for first month selection he wants day shift data and for other month selection night shift (always two months and respective shift options at a time). Now he would like to see day for 1st month and night for second month and variance in 3rd column. I am using SSAS live connection so not able to do anything much on power BI
Hi there
Unfortunately I do not have the file any longer.
If it is always going to be the 1st day of the month for the day shift and the 2nd day for the night shift, you could code this using variables?
VARIABLE 1. Where what you could do, is to first find out if it is the first day of a month
VARIABLE 2. Find out if it is the second day of the month
VARIABLE 3. Day Shift
VARIABLE 4. Night Shift
Finally you can create a measure using the above variables to get the values you require?
Could you please explain what [UnitType (Selected)] is? Is UnitType an another dataset? If so, then what “Selected” is ? 🙂
Hi there
The [UnitType (Selected)] is a calculated measure, this calculated measure has got values of “Sales Amount” or “Cost Amount”, which is created with a disconnected measure. So the [UnitType (Selected)] is the output of the measure.
The measure is defined as roughly the following:
[UnitType (Selected)] = IF(MyTable’Selected’ = “Sales”, SUM(FactTable’SumSales’), SUM(FactTable’SumCosts’))
Hello I am trying to do a similar thing, but when I use the VALUES function I am getting an error to say that a single value for the column in my table cannot be determined (the table that I created for my slicer).
I have a table called SelectComparator with 4 rows in it that make up the 4 options in my slicer. I want to create a measure that does something when a particular one of these options is selected but something else when any of the other 3 are selected (it is a single select slicer). Can you help??
I have tried
Test =
IF (
ISFILTERED( SelectComparator[ComparatorLabel] ) && VALUES(SelectComparator[ComparatorLabel] = “Year to date”),
TRUE(),
FALSE()
)
But am getting the error I described above for the VALUES function
Hi there does your table ‘SelectComparator'[ComparatorLabel] have multiple rows for “Year to Date” or only one?
Hi – I have a problem in creating a dashboard, the user gave me 10 customers, details what they buy from them & also what they sell to end customer. Wanted us to calculate what is the delta between Units from Distributor & Units sold to EU.. Lets take Product 1 they get 100 units & sell 50 units – so here (-50% ) is the output (Units sold/Units bought)-1 is the measure.
The problem is user want to have the delta as the slicer,.. a button to show or a slicer (10%.-10%, 50%, -50%) when they click they can see customer data in a table for auditing, rather selecting & choosing the product in the list box.
Is there a way & step by step process to show the delta % (Measure) to show as slicers
Hi Mohan
Yes I am sure that this can be done. I would suggest that you work through the example I provided and break it down into individual parts and at the end you will get the result you are looking for.
Doesn’t the following measure do the same without the IF?
Day shift =
CALCULATE (
[UnitType (Selected)],
KEEPFILTERS ( ‘Prod'[Shift] = “Day Shift” )
) + 0
Hi there
Yeah, this too should work thanks for commenting!