Sub topic 2
Filter context and dates
Time intelligence functions work by changing the dates that a measure can see. To predict what they will do, you first need to know which dates a measure sees in each cell of a visual. This chapter covers that, using the Date table and Sales fact table from the previous chapter.
This chapter assumes you can write a basic measure with SUM, you understand a one-to-many relationship between a dimension and a fact table, and you have placed a measure in a matrix or line chart. CALCULATE comes in the next chapter, so here you only read the context and do not change it.
What filter context is
Filter context is the set of filters that apply when a measure is calculated. Slicers, report filters, and the row and column headings of a visual all add filters. Each cell in a visual gets its own filter context, and DAX evaluates the measure once per cell.
Take this measure:
Total Sales = SUM ( Sales[Sales Amount] )The formula never mentions a date. In a matrix with 'Date'[Year] on rows, the row for 2024 returns only 2024 sales. The row heading adds the filter 'Date'[Year] = 2024, and the measure then sums whatever sales remain.
How a date filter reaches the fact table
The filter does not start on the Sales table. It starts on the Date table and travels along the relationship.
- A visual or slicer filters a column in
Date, such asYear. - The filtered
Daterows are the dates that remain visible. - The relationship from
Date[Date]toSales[Order Date]passes that set of dates toSales. - Only
Salesrows with a matching order date remain, and the measure aggregates them.
This is why time intelligence needs a dedicated Date table. The functions you will meet later, such as DATESYTD and SAMEPERIODLASTYEAR, return sets of dates taken from that table. They can only return a date that exists in it. If a day is missing from the Date table, no later calculation can bring it back.
Looking at the visible dates
You can inspect the filter context by writing measures that report on the visible dates. Create these four measures:
First Visible Date = MIN ( 'Date'[Date] )
Last Visible Date = MAX ( 'Date'[Date] )
Days In Context = COUNTROWS ( 'Date' )
Selected Year = SELECTEDVALUE ( 'Date'[Year] )Put Year Month on matrix rows and add all four measures as values. The format of First Visible Date and Last Visible Date should be a date format. Here is what you get when the Date table runs from 2020-01-01 to 2025-12-31:
| Row | First Visible Date | Last Visible Date | Days In Context | Selected Year |
|---|---|---|---|---|
| 2024-02 | 2024-02-01 | 2024-02-29 | 29 | 2024 |
| 2024-03 | 2024-03-01 | 2024-03-31 | 31 | 2024 |
| Total | 2020-01-01 | 2025-12-31 | 2192 | (blank) |
Three points follow from the table.
- A month row sees every day of that month, whether or not any sale happened on that day. The count comes from the
Datetable, not fromSales. - The Total row has no filter on
Year Month, so it sees every date in the table.MINandMAXreturn the earliest and latest dates in the whole table. SELECTEDVALUEreturns a value only when exactly one value remains. In the Total row six years are visible, so it returns blank.
MAX ( 'Date'[Date] ) is the most common way to ask "what is the last date in this cell?" Many time intelligence measures start from it, and the moving-average measures later in the tutorial do exactly that.
Filters on different columns combine
A visual can filter several Date columns at once. The filters combine with AND, so only dates that satisfy all of them stay visible.
Suppose a slicer sets Year to 2024 and the matrix rows show Month Number. In the row for month 3, the visible dates are those where Year = 2024 and Month Number = 3. That is 31 days in March 2024.
Now remove the year slicer and keep Month Number on rows. The row for month 3 now filters only Month Number = 3, so it sees March of every year from 2020 to 2025. Days In Context returns 186 for that row. A month with 31 days in each of six years gives 6 × 31. Last Visible Date returns 2025-03-31, because that is the latest date among all those Marches.
Using fact-table dates in a visual
The fact table has its own date column, Sales[Order Date]. You could drag it into a visual instead of using the Date table. That does filter the sales correctly, but the filter lands on the Sales table and not on Date.
The Date table then keeps all of its rows, so Days In Context would show 2192 in every cell. Time intelligence functions read the Date table, so they would see every date and return wrong results. Use the Date table's columns in visuals, slicers, and filters. Hide Sales[Order Date] from report view so that nobody uses it by accident.
Totals use their own filter context
Cells in the Total row are not the sum of the rows above them. DAX evaluates the measure again with the filters of that row, which here means no filter on the grouping column.
For Total Sales the result happens to equal the sum of the rows, because summation is additive. For many time intelligence measures it does not. A year-to-date measure in the Total row of a month-level matrix evaluates against all months at once, so it is not the sum of the monthly values, and nobody would want it to be. When a total looks odd, work out which dates that Total row sees. The measure is usually doing exactly what you wrote.
Row context and filter context are different
Row context means "the current row" while DAX iterates over a table. It exists inside calculated columns and inside iterator functions such as SUMX. Filter context is the set of filters that decides which rows a measure aggregates.
Row context does not filter other tables. In a calculated column on the Date table, a reference such as [Date] returns the date of the current row. A measure uses filter context, so there it must use an aggregation such as MAX ( 'Date'[Date] ) to turn the visible dates into one value. Writing 'Date'[Date] alone in a measure produces an error when more than one date is visible.
This distinction explains why the date table's own columns, such as Year Month, are calculated columns: they depend on row context, one date at a time. The measures that use them depend on filter context.
Common problems at this stage
| Symptom | Likely cause |
|---|---|
| `Days In Context` is 2192 in every cell | The visual uses `Sales[Order Date]` instead of a `Date` table column, or the relationship is inactive or missing. |
| `Last Visible Date` shows the same date in every month row | The visual's filter is not on the `Date` table, so no month filter reaches it. |
| A month row shows more days than the month has | There is no year in the filter context, so several years of that month are visible. |
| `Days In Context` is less than the month length | The `Date` table has gaps, or a page or slicer filter removed dates. |
| `SELECTEDVALUE` returns blank in a row | More than one value is visible for that column. |
Exercise
A matrix has 'Date'[Month Number] on rows and no year filter. The Date table covers 2020-01-01 to 2025-12-31. A year slicer is then set to 2024. What do Days In Context and Last Visible Date return for the row where Month Number is 2, before and after the slicer is applied?
Show answer
Before the slicer, February of all six years is visible. That is 28 days in 2020? No: 2020 and 2024 are leap years with 29 days, and the other four years have 28 days. The total is 29 + 28 + 28 + 28 + 29 + 28 = 170 days. Last Visible Date is 2025-02-28, the latest visible date. After the slicer is set to 2024, only February 2024 is visible. Days In Context is 29 and Last Visible Date is 2024-02-29.
Exercise
Why does Selected Year = SELECTEDVALUE ( 'Date'[Year] ) return blank in the Total row of a matrix that has Year Month on rows?
Show answer
The Total row has no filter on Year Month, so all years from 2020 to 2025 are visible. SELECTEDVALUE returns a value only when exactly one value of the column remains in the filter context. With six values visible, it returns blank.