Power BI: exclude the filter selection from the calculation

To calculate the values of the non-selected options of a slicer, there are few things to do before like to create a new table and a measure so I have this data:

power bi

I will create a new table in order to extract the unique values of the “support” and “type” column:

power bi

NOTE:

power bi

I will create a measure with this formula:


var result=CALCULATE(COUNT('table1'[argument1]),KEEPFILTERS(EXCEPT(ALL('table1'[argument2]),ALLSELECTED('table2'[argument1]))))
return
IF(ISFILTERED('table2'[argument1]),result,COUNT('table1'[argument1]))
power bi

NOTE: change “table” and “argument” by yours

Now that I have everything set, I will create a slicer with the new table and those 3 visuals (1 without and 2 with the measure):

power bi

If I select “cx”:

power bi
  • The left visual is doing nothing because the measure is not included
  • The middle visual, which has only the measure, is calculating everything excluding the selected option
  • The right visual, which has the measure but also other information, is also calculating everything excluding the selected option. Moreover, I can see empty cells for the measure

If my slicer has multiple layers, I will have to use this formula:


var allexc1=EXCEPT(ALL('table1'[argument2]),ALLSELECTED('table2'[argument1]))
var allexc2=EXCEPT(ALL('table1'[argument3]),ALLSELECTED('table2'[argument2]))
var result=IF(ISFILTERED('table2'[newsupport]),CALCULATE(COUNT('table1'[argument1]),KEEPFILTERS(allexc1)),CALCULATE(COUNT('table1'[argument1]),KEEPFILTERS(allexc2)))
return
IF(ISFILTERED('table2'[argument1])||ISFILTERED('table2'[argument2]),result,COUNT('table1'[argument1]))
              
power bi
power bi

Now if I create a relationship, the formula will stop to work so to remediate it, I will include in the formula the CROSSFILTER function:

power bi
power bi

If you remember, my left visual didn’t change and the right one showed empty cell. The solution only works without relationship so I will create a new measure:

  • For a single layer filter

SWITCH(TRUE(),VALUES('table1'[argument2]) IN VALUES('table2'[argument1]) && COUNTROWS(ALLSELECTED('table2'[argument1]))<>COUNTROWS(ALL('table2'[argument1])),1,0)              
              
power bi
  • For a multiple layers filter


var allexc1=SWITCH(TRUE(),VALUES('table1'[argument1]) IN VALUES('table2'[argument1]) && COUNTROWS(ALLSELECTED('table2'[argument1]))<>COUNTROWS(ALL('table2'[argument1])),1,0)
var allexc2=SWITCH(TRUE(),VALUES('table1'[argument2]) IN VALUES('table2'[argument2]) && COUNTROWS(ALLSELECTED('table2'[argument2]))<>COUNTROWS(ALL('table2'[argument2])),1,0)
return
IF(ISFILTERED('table2'[argument1]),allexc1,allexc2)
              
power bi

Once done, for each visualization that doesn’t contain the measure and/or I want to make disappear the empty cells, I will add it in the “filters” panel by configuring like that:

power bi
power bi

NOTE: for the middle visualization, I don’t add it because it contains only the measure

Interesting Topics