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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
RMV
Helper V
Helper V

Calculate function with 2 filters

Hi,

 

I'm trying to calculate an amount with 2 filters for sales amount with Type = Inflow and which falls in previous month.

I have this formula, but it resulted blank.

CALCULATE(SUM(Entry_all[Amount]),FILTER(Entry_all,Entry_all[Type]="Inflow"),DATEADD('Date'[Date],-1,MONTH))

 

Can anyone advise what did I do wrong & advise the correct one?

 

Thanks

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @RMV

 

Try this:

=
CALCULATE (
    SUM ( Entry_all[Amount] ),
    Entry_all[Type] = "Inflow",
    DATEADD ( 'Date'[Date], -1, MONTH )
)

The problem with the original formula is due to how table filter arguments provided to CALCULATE are treated.

 

As a rule of thumb, if you want to filter a particular column using CALCULATE, you should provide a filter argument referring to that column only, not an entire table.

 

If you provide a table filter argument, and that table is a physical table of your data model (potentially filtered), then in fact the "expanded" version of the table is applied as a filter argument. This means a filter consisting of all columns of the expanded table is created.

 

The expanded version of Entry_all includes any columns of tables reachable by tracing many:1 relationships starting from Entry_all, which includes the 'Date' table. This means the expanded version of Entry_all includes columns corresponding to dates in the current filter context, which, if you are filtering on a single month, will not intersect with the dates returned by DATEADD, which are one month earlier.

(As it happens, dates from the "current month" are present in the original columns of Entry_all anyway regardless of the Date table.)

 

 Instead, for your first filter argument you really just want to apply a filter to the Entry_all[Type] column.

 

There are various articles on this subject.

A post I see referred to a lot related to this topic is The Logic behind the Magic of DAX Cross Table Filtering by Jeffrey Wang.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @RMV

 

Try this:

=
CALCULATE (
    SUM ( Entry_all[Amount] ),
    Entry_all[Type] = "Inflow",
    DATEADD ( 'Date'[Date], -1, MONTH )
)

The problem with the original formula is due to how table filter arguments provided to CALCULATE are treated.

 

As a rule of thumb, if you want to filter a particular column using CALCULATE, you should provide a filter argument referring to that column only, not an entire table.

 

If you provide a table filter argument, and that table is a physical table of your data model (potentially filtered), then in fact the "expanded" version of the table is applied as a filter argument. This means a filter consisting of all columns of the expanded table is created.

 

The expanded version of Entry_all includes any columns of tables reachable by tracing many:1 relationships starting from Entry_all, which includes the 'Date' table. This means the expanded version of Entry_all includes columns corresponding to dates in the current filter context, which, if you are filtering on a single month, will not intersect with the dates returned by DATEADD, which are one month earlier.

(As it happens, dates from the "current month" are present in the original columns of Entry_all anyway regardless of the Date table.)

 

 Instead, for your first filter argument you really just want to apply a filter to the Entry_all[Type] column.

 

There are various articles on this subject.

A post I see referred to a lot related to this topic is The Logic behind the Magic of DAX Cross Table Filtering by Jeffrey Wang.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Hi @OwenAuger,

 

It's really a comprehensive advise & help. Thanks a lot!

I'm just started to learn dax, and most of the times, learning by doing the cases.

To have a full explanation including what's wrong with my previous formula, it really a great help. Thanks again!

 

Regards,

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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