Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply

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
v-haibl-msft
Employee
Employee

@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

6 REPLIES 6
v-haibl-msft
Employee
Employee

@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

Hi, can someone please help me understand why my query is not working as it should be? I am trying to Count number of rows where the field is not equal to "None" and Blanks are not counted in. The query below seems to only take into account one of the filters.
 
Monitor 1 - Action taken by customer =
    CALCULATE(
        COUNT('Network protection '[Monitor 1 - Action taken by customer]),
            FILTER('Network protection ', OR('Network protection'[Monitor 1 - Action taken by customer] <> "None", NOT(ISBLANK('Network protection' [Monitor 1 - Action taken by customer])))))

This is very useful. However, I am trying to apply different targets to different months based on last years totals. So if month is September then add 70% to last years total, if month is October then 80 % to last year etc, then the months after that should be the same as last year. I have looked everywhere and cannot find the logic to do this. Any help would be great 

Thanks. This is very clear.

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 an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
lalthan
Resolver II
Resolver II

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.