Power BI: filter/slicer
I will explain different ways I use slicers (filters) but I will skip its default behaviour because mostly it is quite intuitive to use and there are tons of articles easy to find in the internet.
If I have multiple categories for 1 single or multiple tables, I like to use this free plug-in called HierarchySlicer that can be downloaded from Microsoft website. It allows me to create individual new category tables that contain only customized criteria, once done, I just have to link them in “manage relationships” to my main tables. For Power BI to detect automatically the relationship, I will recommend that when you will create your new category table, put the same column name as you have in your data table. It allows me to have only 1 slicer instead of many for all tables and its behaviour is the same as the default slicer. Let´s jump to some DAX measures.
To inactive/hide a filter, for instance, I have those 3 filters:
With the slicer “type”, if I select “business”, I want that “support” is blank and vice versa:
I will create those 2 measures with this formula:
INT(SELECTEDVALUE('table'[argument])="value")
NOTE: replace “table”, “argument” and “value” by yours
For “business” and “support”, put the measure corresponding to them then once configure them like that, it is done:
There is no way to inactive filters, this trick is just hiding the options so if an option is selected, it will not disappear until I clear it:
To not affect this undesired selection in my calculations, I will add the ALL function. For my example, it will be:
- ALL('Support'[Support]) to ignore the “support” slicer
- ALL('Business'[Business]) to ignore the “business” slicer
To count number of selection:
- For 1 category: COUNTROWS(VALUES('table1'[argument1]))
- For 2 categories or more: COUNTROWS(VALUES('table1'[argument1]))+COUNTROWS(VALUES('table2'[argument2]))
For category1, “others” is on the second position, if you prefer to have it at the bottom of the list, add a new column and put a number. Once done, click on the menu “column tools -> sort by column” then select “number”:
IF no selection is done, it will display the total number of options, to put something else, I will need to use IF and ISFILTERED functions, for instance:
IF(ISFILTERED('table1'[argument1]),COUNTROWS(VALUES('table2'[argument2])),0)
To display what I select:
- For 1 category: CONCATENATEX(VALUES('table1'[argument1]),[argument1],", ")
NOTE: CONCATENATEX will give the result in text format so for other formats, use CONVERT. For instance: CONVERT(CONCATENATEX(VALUES('table1'[argument1]),[argument1]),INTEGER) change text in number format. - For 2 categories or more:
VAR cat1 = CONCATENATEX(VALUES('table1'[argument1]),[argument1],", ")
VAR cat2 = CONCATENATEX(VALUES('table2'[argument2]),[argument2],", ")
RETURN
SWITCH(TRUE(),
ISFILTERED('table1'[argument1]) && ISFILTERED('table2'[argument2]),cat1&", "&cat2,
ISFILTERED('table1'[argument1]),cat1,
ISFILTERED('table2'[argument2]),cat2,
"No selection")
The result of the measure 5 is not sorted, if you do care, you can use the same solution as category1 by adding a number column, sort it correctly during the creation or if it is already created, just edit the category:
As for the counting, if I select nothing, it will display all the options, to tell him something else, I will use the same thing, for instance:
IF(ISFILTERED('table1'[argument1]),CONCATENATEX(VALUES('table1'[argument1]),[argument1],", "),"No selection")
To show the list by ending with “and”, I will use the SUBSTITUTE function:
SUBSTITUTE(CONCATENATEX(VALUES('table2'[argument2]),[argument2],", "),","," and")
To put each selection in a line, I will use UNICHAR(10), there are 2 options:
- Option 1 without comma: CONCATENATEX(VALUES('table2'[argument2]),[argument2],UNICHAR(10))
- Option 2 with comma: CONCATENATEX(VALUES('table2'[argument2]),UNICHAR(10)&[argument2],",")
To check if a specific option is selected or not, for instance, “comm”:
SEARCH("value1",[argument],"yes","no")
For 2 or more specific options, for instance, “comm” or “external”:
SWITCH(TRUE(),SEARCH("value1",[argument],1,0)>0,"yes",SEARCH("value2",[argument],1,0)>0,"yes","no")
And if I want both (“comm” and “external”), it will be:
SWITCH(TRUE(),SEARCH("value1, value2",[argument],1,0)>0,"yes","no")
I just have to make sure to match my measure 5 about how it displays my selection if I select those 2 words so if instead of comma, I have for instance a space, it will be “comm external” instead of “comm, external”.
This formula can be used to find a specific word in column too.
To cancel the selection, I will use CALCULATE and ALL functions, so for instance:
- For 1 category: CALCULATE([argument],ALL('table1'[argument1]))
- For 2 categories: CALCULATE([argument],ALL('table1'[argument1]),ALL('table2'[argument2]))
As you can see, for 2 categories, I put 2 times ALL, one for each category. This ignore option (or show me everything in spite of the selection) may be useful for some specific scenarios, for instance for calculating percentages and/or numbers. In general, ALL should be put before any conditions, for instance:
To show only for a specific category, I will use IF and SELECTEDVALUE functions, for instance, to display only if any options are selected inside “support”:
IF(SELECTEDVALUE('table1'[argument1])="value1",CONCATENATEX(VALUES('table2'[argument2]),[argument2],", "),"value2")
But this formula will show “not support category” if I select other things outside of “support”. To remediate it, I will use CALCULATE and FILTER functions:
CALCULATE(CONCATENATEX(VALUES('table2'[argument2]),[argument2],", "),FILTER('table1','table1'[argument1]="value"))
This formula will show blank if there are no support options selected, to replace the blank to “not support category”:
IF(ISBLANK([argument]),"value",[argument])
Interesting Topics
-
Be successfully certified ITIL 4 Managing Professional
Study, study and study, I couldn’t be successfully certified without studying it, if you are interested...
-
Be successfully certified ITIL 4 Strategic Leader
With my ITIL 4 Managing Professional certification (ITIL MP) in the pocket, it was time to go for the...
-
Hide visual and change background color based on selection
Some small tricks to customize the background colour of a text box...
-
Stacked and clustered column chart or double stacked column chart
In excel, I use a lot the combination of clustered and stacked chart...