Sub topic 2
Filter context and dates
This chapter assumes you can write basic measures with SUM and DIVIDE. It also assumes you know a star schema, with a Sales fact table related to a Date dimension table. You should have placed a measure in a matrix or line chart. The previous chapter built the Date table used here.
What filter context is
Filter context is the set of filters active when DAX evaluates a measure. Every cell in a visual has its own filter context. The row and column headings, slicers, and report filters all contribute to it.
Take this measure:
Total Sales = SUM ( Sales[Sales Amount] )The formula never mentions a date. In a matrix with years on the rows, the 2024 row gets a different answer from the 2025 row. The matrix filters Date[Year] before the measure runs, and that filter flows through the relationship to Sales.
How dates enter the filter context
A date filter reaches the fact table in three steps:
- A visual, slicer, or filter puts a condition on a column of the
Datetable. - The relationship passes the filter from
Date[Date]to the matching column inSales. - The measure aggregates only the surviving
Salesrows.
Time intelligence functions work on this filter. They do not read dates from the fact table. They look at which dates are currently visible in the Date table, then calculate a new set of dates from them.
Inspecting the dates a measure sees
Before writing time intelligence, it helps to see what your filter context contains. These three measures expose it:
First Visible Date = MIN ( 'Date'[Date] )
Last Visible Date = MAX ( 'Date'[Date] )
Visible Days = COUNTROWS ( 'Date' )Put them in a matrix with Date[Year Month] on the rows. For the row 2025-03, you should see 31 visible days, a first date of March 1, and a last date of March 31. In the grand total row, the filter context holds every date in the table. The last visible date there is the last date in the table, not the last date with sales.
| Where the measure is evaluated | Dates in filter context |
|---|---|
| Row for `2025-03` | March 1 to March 31, 2025 |
| Row for year 2025 | January 1 to December 31, 2025 |
| Grand total | Every date in the Date table |
| Slicer set to Q2 2025 | April 1 to June 30, 2025 |
Functions such as MAX ( 'Date'[Date] ) read the dates in the current filter context. This is why one measure can return a different value in each cell. It is also how the moving-average and incomplete-period measures later in this tutorial find "today" for each cell.
Common errors
- Filtering with the fact table's date column. If a visual uses
Sales[Order Date]on its rows, the filter lands onSales. TheDatetable is not filtered, and time intelligence functions see all dates. Always use columns from theDatetable in visuals, slicers, and measures. - Gaps in the date table. If a date is missing, its filter context is incomplete. Period calculations can then skip days or fail. Use a contiguous table like the one from the earlier chapter.
- Multiple date columns on
Sales. Only one relationship toDatecan be active. The inactive ones do not filter unless a measure activates them. - Reading the grand total as "the latest period". The total row has the whole range in its context, so
MAXthere returns the end of the table.
Exercise
A matrix shows Date[Year Month] on the rows. For the row 2025-02 in a non-leap year, what do First Visible Date, Last Visible Date, and Visible Days return?
Show answer
First Visible Date is February 1, 2025. Last Visible Date is February 28, 2025. Visible Days is 28. The row's year-month filter keeps only the 28 Date table rows from that month.
Exercise
A visual puts Sales[Order Date] on its rows and shows a year-to-date measure. Each row shows the same value as the plain total. Why?
Show answer
The row filter is applied to the Sales table, not to the Date table. The Date table is not filtered, so the time intelligence function starts from every date and does not see the row's date.