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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Jeanxyz
Power Participant
Power Participant

how does filter context work

I'm studying The Definitive Guide to DAX and get confused on how filter context works.

 

In the measure below (p193), which filter context is applied to values('Date'[Date])? The external filter context or the new context created by filter(all('Date'[Date])? Why? 

 

Similarly, which filter context is applied to YEAR ( 'Date'[Date] ? The external filter context or the new context created by filter(ll('Date'[Date])?

 

sales amount_1M test =
CALCULATE (
    [Sales Amount],
    FILTER (
        ALL ( 'Date'[Date] ),
        CONTAINS (
            VALUES ( 'Date'[Date] ),
            'Date'[Date], DATE ( YEAR ( 'Date'[Date] )MONTH ( 'Date'[Date] ) - 1DAY ( 'Date'[Date] ) )
        )
    )
)

 

According to the textbook, the measure should work similarly as Dateadd(). So I have written the measure below. 

sales amount_1M = calculate([Sales Amount],dateadd('Date'[Date],-1,month))
 
However, when I compared the two measures, the results are quite different in some product categories.  Why the 1st measure didn't work as exprected?
filter context2.PNG

 

 

1 ACCEPTED SOLUTION
v-easonf-msft
Community Support
Community Support

Hi, @Jeanxyz 

The same logic is difficult to reproduce in a general dax formula( without considering the number of days), which has been mentioned in 'The Definitive Guide to DAX'.

The following formula is similar, but not completely equivalent.

TREATAS(
    SELECTCOLUMNS(
        CALCULATETABLE(DISTINCT('Date'[Date])),
        "Date",DATE(
            YEAR('Date'[Date]),
            MONTH('Date'[Date])-1,
            DAY('Date'[Date])
        )
    ),
    'Date'[Date]))

This formula will work normally only when the target month and the current month have the same number of days, otherwise several days(1day=31-30, 2days=29-31,3days=28-31 )  will be lost or included.

Best Regards,
Community Support Team _ Eason

 

View solution in original post

4 REPLIES 4
v-easonf-msft
Community Support
Community Support

Hi, @Jeanxyz 

The same logic is difficult to reproduce in a general dax formula( without considering the number of days), which has been mentioned in 'The Definitive Guide to DAX'.

The following formula is similar, but not completely equivalent.

TREATAS(
    SELECTCOLUMNS(
        CALCULATETABLE(DISTINCT('Date'[Date])),
        "Date",DATE(
            YEAR('Date'[Date]),
            MONTH('Date'[Date])-1,
            DAY('Date'[Date])
        )
    ),
    'Date'[Date]))

This formula will work normally only when the target month and the current month have the same number of days, otherwise several days(1day=31-30, 2days=29-31,3days=28-31 )  will be lost or included.

Best Regards,
Community Support Team _ Eason

 

Ashish_Mathur
Super User
Super User

Hi,

I am not sure of what you want but i think you want to calculate the sales of the previous month.  Try this measure

=calculate([Sales Amount],previousmonth('Date'[Date]))

Please ensure that you drag Year and Month from the Date table to your visual and there is a relationship (Many to One and Single) from the Date column of your Data Table to the Date column of your Date table.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

Your first measure is missing the transition from January to December of the prior year. The book has that shown correctly.

 

The applied filter context is ALL(Date[Date]).

bcdobbs
Community Champion
Community Champion

@lbendlin is totally correct that the expression doesn't handle dates in January going back to December.

 

However I think the answer to your other question about context needs expanding.


ALL(Date[Date]) is only providing the base table for the iterator. FILTER doesn't modify the filter context (only calculate expressions can do that). Therefore VALUES(Date[Date]) comes from the external context.  YEAR(Date[Date]) comes from the row context of the iterator.



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors