cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Deeintu
Helper I
Helper I

How to apply filter on multiple columns with OR operator using DAX formula

Hello all,

 

I'm trying to apply multiple filter conditions in the DAX formula, but getting syntax error.

Filter condition 1:  Asset[Program] = "Primary Assets"

Filter condition 2: Asset[Category] IN ("ASM", "NSN","TPG","STB")

 

This part is execueted correctly without any error:

------------------------------------------------------

 

evaluate filter( SUMMARIZECOLUMNS(
Asset[Type],
Asset[Program],
Asset[Category],
Sales[Period],
Sales[Quantity])
, Asset[Program] = "Primary Assets")

 

But I want to include 2nd filter condition in the DAX but it's not working i.e.   

(Asset[Category] = "NSN" OR
Asset[Category] = "NSN" OR
Asset[Category] = "TPG" OR
Asset[Category] = "STB")

 

Could you please tell me how to add 2nd condtion to the above DAX formula with OR operator.

 

My expected output should be somting like this:

 

evaluate filter( SUMMARIZECOLUMNS(
Asset[Type],
Asset[Program],
Asset[Category],
Sales[Period],
Sales[Quantity])
, Asset[Program] = "Primary Assets"

below condition need to be included in the DAX

OR Asset[Category] = "NSN"

OR Asset[Category] = "NSN"

OR Asset[Category] = "TPG"

OR Asset[Category] = "STB"

)

 

Thanks

Dee

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Deeintu 

You can tweak your current syntax to apply Filter Condition 1 OR Filter Condition 2:

 

EVALUATE
FILTER (
    SUMMARIZECOLUMNS (
        Asset[Type],
        Asset[Program],
        Asset[Category],
        Sales[Period],
        Sales[Quantity]
    ),
    OR (
        Asset[Program] = "Primary Assets",
        Asset[Category] IN { "ASM", "NSN", "TPG", "STB" }
    )
)

 

One potential performance issue with FILTER( SUMMARIZECOLUMNS(...)) is that SUMMARIZECOLUMNS returns the full table (which contains some unwanted rows) which is then iterated over with FILTER.

 

An alternative, which may perform better, is to construct a table containing the filter condition, and provide that as an argument in SUMMARIZECOLUMNS after the Groupby columns. Something like this:

 

EVALUATE
VAR FilterTable =
    UNION (
        CROSSJOIN (
            TREATAS ( { "Primary Assets" }, Asset[Program] ),
            ALL ( Asset[Category] )
        ),
        CROSSJOIN (
            ALL ( Asset[Program] ),
            TREATAS ( { "ASM", "NSN", "TPG", "STB" }, Asset[Category] )
        )
    )
RETURN
    SUMMARIZECOLUMNS (
        Asset[Type],
        Asset[Program],
        Asset[Category],
        Sales[Period],
        Sales[Quantity],
        FilterTable
    )

 

 

See also this article (which the 2nd approach above is based on):

https://www.sqlbi.com/articles/using-or-conditions-between-slicers-in-dax/

 

Regards,

Owen


Owen Auger

Did I answer your question? Mark my post as a solution!

My Blog
Connect on Twitter
Connect on LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @Deeintu 

You can tweak your current syntax to apply Filter Condition 1 OR Filter Condition 2:

 

EVALUATE
FILTER (
    SUMMARIZECOLUMNS (
        Asset[Type],
        Asset[Program],
        Asset[Category],
        Sales[Period],
        Sales[Quantity]
    ),
    OR (
        Asset[Program] = "Primary Assets",
        Asset[Category] IN { "ASM", "NSN", "TPG", "STB" }
    )
)

 

One potential performance issue with FILTER( SUMMARIZECOLUMNS(...)) is that SUMMARIZECOLUMNS returns the full table (which contains some unwanted rows) which is then iterated over with FILTER.

 

An alternative, which may perform better, is to construct a table containing the filter condition, and provide that as an argument in SUMMARIZECOLUMNS after the Groupby columns. Something like this:

 

EVALUATE
VAR FilterTable =
    UNION (
        CROSSJOIN (
            TREATAS ( { "Primary Assets" }, Asset[Program] ),
            ALL ( Asset[Category] )
        ),
        CROSSJOIN (
            ALL ( Asset[Program] ),
            TREATAS ( { "ASM", "NSN", "TPG", "STB" }, Asset[Category] )
        )
    )
RETURN
    SUMMARIZECOLUMNS (
        Asset[Type],
        Asset[Program],
        Asset[Category],
        Sales[Period],
        Sales[Quantity],
        FilterTable
    )

 

 

See also this article (which the 2nd approach above is based on):

https://www.sqlbi.com/articles/using-or-conditions-between-slicers-in-dax/

 

Regards,

Owen


Owen Auger

Did I answer your question? Mark my post as a solution!

My Blog
Connect on Twitter
Connect on LinkedIn

Thanks for your help OwenAuger!

Helpful resources

Announcements
Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Power BI Release May 2022 768x460.png

Check it out!

Click here to read more about the May 2022 updates!