Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Regular Visitor

Advanced filter experience: "AND", "OR" , "NOT" filter logic using slicer visuals and DAX!!


There are a number of examples that have shown how to add logic so that you can use filters with an "AND" operator rather than the standard "OR" logic when using a slicer. This example shows how to also add the ability for a "NOT" statement as well, to create what I think is an incredibly powerful level of flexibility for end users.





The attached example has been built using open data and shows how the approach works

Download PBIX example 


Limitations - slow performance with large datasets

I am looking for help and suggestions on how to improve performance of these measures using DAX Studio or by re-engineering the approach. In my actual report the tables have millions of rows and performance is slow, the current approach results in a very large number of engine queries and depending on capacity in Power BI server can even result in visuals timing out due to lack of memory.


How it works (be warned, it may get confusing...)

I won't go over the full extent of how to implement AND filter logic. The approach I have used is based on the following excellent blog post.

Power BI: Implement AND/OR Selection | by ZhongTr0n | Towards Data Science


In my example, I have extended the logic to also allow users to EXCLUDE items. The core logic is the same, and the EXCLUSION criteria can be applied using either an OR logic or AND logic operator (i.e. Filter for Items that DON'T  have X OR Y attribute versus Items that DONT have X AND Y attribute)


1. If the attribute slicer is filtered, the measure below either counts the number of distinct items in the attribute list (if using an AND logic) or returns a 1 if using the OR logic



AndOrLogicSwitch (NOT) = IF (
    ISFILTERED ( 'LSOA Attributes - Filter (NOT)'[Attribute] ),
    IF (
        SUM ( 'ANDOR_Table (NOT)'[Binary] ) = 0,
        CALCULATE (
            DISTINCTCOUNT ( 'LSOA Attributes - Filter (NOT)'[Attribute] ),
            ALL ( 'LSOA Master')
        DISTINCTCOUNT ( 'LSOA Attributes - Filter (NOT)'[Attribute] ),
        ALL ( 'LSOA Master')




2. A further measure then compares, for each item in the master table, the number of attributes the item has compared to the measure value. If the item has fewer attributes than the number of attributes in the above measure, then it does NOT meet the exclusion criteria and is therefore included. This is also combined with similar logic for the INCLUSION criteria, which is essentially the same logic but in reverse.



VAR ANDORCondition = [AndOrLogicSwitch]
VAR NOTCondition = [AndOrLogicSwitch (NOT)]

VAR Include =

// This logic determines if the item meets the INCLUDE criteria
        or(not(isfiltered('LSOA Attributes - Filter'[Attribute])),DISTINCTCOUNT ( 'LSOA Attributes - Filter'[Attribute]) >= ANDORCondition)

// This logic determines if the item meets the EXCLUDE criteria
        , or(not(isfiltered('LSOA Attributes - Filter (NOT)'[Attribute])),DISTINCTCOUNT ( 'LSOA Attributes - Filter (NOT)'[Attribute] ) < NOTCondition))





3. Further measures can then be calculated, using this logic to filter the master table, for example



Count of LSOAs = 
var LSOACount = countrows(filter('LSOA Master',[ANDORNOT Logic]))




Hopefully this will be helpful to others, and I would be very grateful for any ideas on how to improve on what I have done, particularly to improve performance for larger datasets.


Thank you



Frequent Visitor



Taking a high level review of your data model, I recommend you consider eliminating the bidirection relationships where possible.

It seems that you should convert your filtering Attribute tables to dimension tables. This can done by making the "..Viz" table the main Fact Table then summarizing the filtering table by adding a Group By Transformation in the M Query to each filtering table. Your relationship should need be direction between the attribute filtering table and your "...Viz" fact table. This would eliminate bidirectional relationship and reduce table size thus reducing the number of rows for iteration. In your sample data this would reduce rows from 400k to 8k.


An observation by using this AND/OR filtering technigue through DAX expression in the measure, you maybe adding table filter iterations through the process so may need to evaluate the benefits.


As a general performance improvement strategy is to minimize the number of table row scans in the execution.


Best regards





Helpful resources

PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors