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

Calculate with OR/AND/NOT in filter

It's unclear to me how I can use FILTER() in a CALCULATE() function, and apply an AND / OR / NOT logic.

 

Could you please refer to an existing help page or give examples here below? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Microsoft
Microsoft

Re: Calculate with OR/AND/NOT in filter

@jesuisbenjamin

 

I’ll give an example as below. Assuming we have a table like below.

Calculate with ORANDNOT in filter_1.jpg

 

If we want to get the total sales of China on July, we can use following formula.

SalesFromChina_July = 
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER ( Table1, AND ( Table1[Country] = "China", MONTH ( Table1[Date] ) = 7 ) )
)

If we want to get the total sales of China and USA on July and August, we can use following formula.

SalesFromChina_OR_USA = 
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER ( Table1, OR ( Table1[Country] = "China", Table1[Country] = "USA" ) )
)

 

If we want to get the total sales of India on July and August, we can use following formula.

SalesFromIndia = 
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER (
        Table1,
        NOT (
            OR ( Table1[Country] = "China", Table1[Country] = "USA" )
        )
    )
)

Best Regards,

Herbert

View solution in original post

4 REPLIES 4
lalthan Resolver II
Resolver II

Re: Calculate with OR/AND/NOT in filter

Re: Calculate with OR/AND/NOT in filter

To do an AND, you should create 2 filter statements, one for each.

 

=CALCULATE([measure],filter(),filter())

 

for an OR, do it like this

 

=CALCULATE([measure],filter(table,table[col1] = "X" || table[col2] = "Y"))

note this is not overly efficient, but it is how you do it. (working on 2 columns at once can be inefficient.

 

for NOT, you can use teh NOT() function around your tests

 

Hope that helps



* Matt is a Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
Microsoft
Microsoft

Re: Calculate with OR/AND/NOT in filter

@jesuisbenjamin

 

I’ll give an example as below. Assuming we have a table like below.

Calculate with ORANDNOT in filter_1.jpg

 

If we want to get the total sales of China on July, we can use following formula.

SalesFromChina_July = 
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER ( Table1, AND ( Table1[Country] = "China", MONTH ( Table1[Date] ) = 7 ) )
)

If we want to get the total sales of China and USA on July and August, we can use following formula.

SalesFromChina_OR_USA = 
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER ( Table1, OR ( Table1[Country] = "China", Table1[Country] = "USA" ) )
)

 

If we want to get the total sales of India on July and August, we can use following formula.

SalesFromIndia = 
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER (
        Table1,
        NOT (
            OR ( Table1[Country] = "China", Table1[Country] = "USA" )
        )
    )
)

Best Regards,

Herbert

View solution in original post

Highlighted
jesuisbenjamin Helper I
Helper I

Re: Calculate with OR/AND/NOT in filter

Thanks. This is very clear.

Helpful resources

Announcements
‘Better Together’ T-Shirt Contest – Winner Announced!

‘Better Together’ T-Shirt Contest – Winner Announced!

And the winner is...

Announcing the New Spanish Forum

Announcing the New Spanish Forum

Do you need help in Spanish? Check out our new Spanish community section.

Attending MBAS? Claim your badge

Attending MBAS? Claim your badge

Whether you’re streaming the digital event live, or watching on-demand, claim your attendee badge to sport on your profile.

April 2020 Community Highlights

April 2020 Community Highlights

Info on our Super Users, MBAS content and badges, and updates to our support articles. - Read the full Community Highlights.

MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

Top Solution Authors
Top Kudoed Authors