MSBA-261 · Lesson 7 of 12 · 21 min
Time intelligence
▶ Video coming soon A video version of this lesson is on the way.
Every term, the deans asked the same question in slightly different words: are we ahead of or behind where we were at this point last year? For the bookstores, the question was year to date revenue against last year. For the registrar, it was registrations by week of term compared with the same week a year ago. Kwabena's first attempts used hand-written date ranges, and every new term meant editing formulas. Time intelligence functions solved that. This lesson covers the standard time intelligence patterns, how they depend on the date table, and semi-additive measures such as stock on hand, which need special treatment over time.
What time intelligence functions do
Time intelligence functions return sets of dates relative to the dates in the current filter context. Used as filter arguments of CALCULATE, they shift or extend the date filter: from October 2026 to October 2025, or from October 2026 to the whole of the fiscal year so far. Because they work on the filter context, the same measure works on a month, a quarter, a year or a single day, and it moves forward on its own as time passes.
These functions need a proper date table: one row per date, contiguous, marked as a date table and related to the fact tables, as built earlier in the course. Applied to a fact table's own date column, they can return wrong results wherever the fact table has gaps, such as days with no sales.
Year to date, quarter to date and month to date
The year to date pattern adds up everything from the start of the year to the last date in the current context.
Outlet Sales YTD =
CALCULATE ( [Outlet Sales], DATESYTD ( 'Date'[Date], "6/30" ) )
Outlet Sales YTD (shortcut) =
TOTALYTD ( [Outlet Sales], 'Date'[Date], "6/30" )DATESYTD returns the dates from the start of the year to the last date in context; CALCULATE then evaluates the measure over them. TOTALYTD is a shortcut that does both in one call. The year-end argument is a text value giving the month and day the year ends; without it, the calendar year is used. Quarter to date and month to date work the same way with DATESQTD and DATESMTD, or TOTALQTD and TOTALMTD.
Kwabena wrote the year-end as "6/30". The format of that text follows the locale of the model, so a team that works in several locales may prefer to keep fiscal attributes in the date table and use a fiscal-period pattern instead. He noted that in the measure description.
Comparing with the previous year
The prior-year comparison is the most requested number in most organizations. SAMEPERIODLASTYEAR returns the dates in the current context shifted back one year. DATEADD shifts dates by any number of days, months, quarters or years, forward or back. Both are used inside CALCULATE.
Registrations PY =
CALCULATE ( [Registrations], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
Registrations YoY Change =
VAR Current = [Registrations]
VAR Prior = [Registrations PY]
RETURN
IF ( NOT ISBLANK ( Prior ), DIVIDE ( Current - Prior, Prior ) )
Outlet Sales PY YTD =
CALCULATE ( [Outlet Sales YTD], DATEADD ( 'Date'[Date], -1, YEAR ) )The year-over-year change measure returns blank when there is no prior-year value, so a new program does not show a misleading increase. Prior-year year to date applies DATEADD to a measure that already includes DATESYTD: the inner year to date logic runs in the shifted context, producing July to October 2025 when the current context is October 2026.
PARALLELPERIOD shifts by whole periods and returns the whole period: in October 2026, PARALLELPERIOD by minus one year returns all of fiscal or calendar year 2025's dates in that period's granularity, which is useful for "full last year" comparisons. PREVIOUSMONTH, PREVIOUSQUARTER and PREVIOUSYEAR return the whole previous period, as do NEXTMONTH and the other next-period functions.
Same point in the term
Academic data has its own calendar. Comparing registrations on 15 October 2026 with 15 October 2025 is not quite fair if the fall term started on 8 September this year and on 2 September last year. Kwabena's date table had a Week of Term column, so he built the comparison on terms rather than calendar years.
Registrations Same Week Last Fall =
VAR Wk = SELECTEDVALUE ( 'Date'[Week of Term] )
RETURN
CALCULATE (
[Registrations by Registration Date],
REMOVEFILTERS ( 'Date' ),
'Date'[Term] = "Fall 2025",
'Date'[Week of Term] <= Wk
)This measure removes the date filters from the visual, then applies two new ones: last fall's term, and weeks up to the week in context. It is not a built-in time intelligence function, but it uses exactly the same idea of shifting the filter context. Hard-coding "Fall 2025" was acceptable for a term-specific report page; for a general measure, Kwabena later added a Previous Term column to the date table and looked it up instead.
Rolling and moving periods
DATESINPERIOD returns a run of dates of a given length ending on a given date, which is the basis of rolling totals and moving averages.
Outlet Sales Rolling 12 Months =
CALCULATE (
[Outlet Sales],
DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -12, MONTH )
)Rolling totals were particularly useful for the cafés, whose sales nearly vanished in July and August when classes stopped. A month-by-month chart made every September look like a triumph; a rolling twelve-month line showed whether the outlets were actually growing.
Semi-additive measures
Some numbers cannot be added up over time. The bookstores reported units on hand for each product category at the end of every month. Adding September's stock to October's stock gives a meaningless number, yet adding across outlets or categories is perfectly sensible. A measure that adds up over some dimensions but not over time is called semi-additive, and the PL-300 study guide asks candidates to create them.
Account balances, headcount at a point in time, inventory levels and waitlist lengths are all semi-additive. The usual rule is to show the closing balance: the value at the last date in the current period.
Units on Hand =
CALCULATE (
SUM ( 'Bookstore Stock'[Units on Hand] ),
LASTDATE ( 'Date'[Date] )
)
Units on Hand (last date with data) =
CALCULATE (
SUM ( 'Bookstore Stock'[Units on Hand] ),
LASTNONBLANK ( 'Date'[Date], CALCULATE ( COUNTROWS ( 'Bookstore Stock' ) ) )
)
Units on Hand Month End =
CLOSINGBALANCEMONTH ( SUM ( 'Bookstore Stock'[Units on Hand] ), 'Date'[Date] )The first version uses the last date in the period, which works if every period's last date has data. Bookstore stock was recorded on the last business day of each month, not the calendar month end, so LASTDATE returned blank for months ending on a weekend. The second version, using LASTNONBLANK, finds the latest date in the context that has stock rows, which handled the weekend months correctly. CLOSINGBALANCEMONTH, with its quarter and year siblings, is a shortcut for the month-end value and has the same weekend sensitivity as LASTDATE.
A semi-additive measure can still be added across outlets: in a matrix with outlets on rows and months on columns, each cell shows that outlet's closing stock, the row total for an outlet shows its stock at the end of the last month in the year, and the column total adds the outlets together for each month. That is exactly the behavior finance expects.
Opening balances use FIRSTDATE, FIRSTNONBLANK or OPENINGBALANCEMONTH and its siblings, and average balance over a period can be calculated with AVERAGEX over the dates in context, a pattern the next lesson on iterators explains.
Time intelligence and blanks in the future
A year to date line chart that covers the whole fiscal year will continue flat from today to June, because the year to date measure returns the same total for every future month. Kwabena hid future dates with a simple flag in the date table, Is Past, and a measure pattern that returns blank for dates after the last date with sales.
Outlet Sales YTD (to date) =
VAR LastSalesDate = CALCULATE ( MAX ( 'Outlet Sales'[Sale Date] ), REMOVEFILTERS ( 'Date' ) )
RETURN
IF ( MIN ( 'Date'[Date] ) <= LastSalesDate, [Outlet Sales YTD] )Worked problem
Building a year-over-year page
The bookstore manager wants a table by month with this year's fiscal year to date sales, last year's fiscal year to date sales, and the change. Which measures should Kwabena use, and what must be true of the model?
Show solution
- The model needs a marked, contiguous date table related to Outlet Sales.
- Outlet Sales YTD = TOTALYTD ( [Outlet Sales], 'Date'[Date], "6/30" ).
- Outlet Sales PY YTD = CALCULATE ( [Outlet Sales YTD], DATEADD ( 'Date'[Date], -1, YEAR ) ).
- YTD Change = DIVIDE ( [Outlet Sales YTD] - [Outlet Sales PY YTD], [Outlet Sales PY YTD] ), formatted as a percentage.
- Use the fiscal hierarchy from the date table on rows, and hide future months with the to-date pattern.
Answer: Fiscal year to date, prior-year year to date via DATEADD, and a DIVIDE-based change, all built on the marked date table with the 30 June year end.
Time intelligence and the date grain of a fact table
Time intelligence works smoothly when a fact table relates to the date table at the daily grain. Program Budget was monthly, related on MonthKey, so time intelligence functions applied to the Date table's Date column still worked: DATESYTD returned the dates from 1 July to the end of the current context, the many-to-many relationship mapped them to the months they belonged to, and the budget for whole months was added up. The subtlety was partial months. A year to date budget calculated on 15 October included all of October's budget, because the budget has no daily detail. For the deans, that was acceptable when explained. For finance, Kwabena built a version that included only completed months, filtering the date table to months that had ended.
Budget YTD Completed Months =
VAR LastCompleteMonthEnd = EOMONTH ( TODAY (), -1 )
RETURN
CALCULATE (
[Budgeted Revenue],
DATESYTD ( 'Date'[Date], "6/30" ),
'Date'[Date] <= LastCompleteMonthEnd
)TODAY and NOW make measures depend on when the query runs, so the same report shows different numbers on different days. That is the intention here, but it makes testing harder. Kwabena tested the measure in DAX query view by temporarily replacing TODAY with a fixed date, checking the result, and then restoring TODAY.
Moving averages
Rolling periods also support moving averages. A three-month moving average of outlet sales divides a rolling three-month total by the number of months in it, or uses AVERAGEX over the months in a DATESINPERIOD window.
Outlet Sales 3M Moving Avg =
VAR Window =
DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, MONTH )
RETURN
AVERAGEX (
CALCULATETABLE ( VALUES ( 'Date'[MonthKey] ), Window ),
CALCULATE ( [Outlet Sales] )
)The CALCULATE inside AVERAGEX forces context transition, so each month in the window is evaluated separately before the average is taken. Without it, AVERAGEX would evaluate Outlet Sales for the outer context on every row and average three copies of the same number. For a single visual, a visual calculation using MOVINGAVERAGE achieves the same result with less code, but a model measure can be reused in every report.
Worked problem
Choosing the right comparison
The Ridgeview café manager wants to know whether November is going better than last year. It is 20 November. Should Kwabena compare November to date with all of last November, or with 1 to 20 November last year?
Show solution
- A month to date total for 1 to 20 November cannot fairly be compared with a full month.
- Compare with the same days last year: CALCULATE ( [Outlet Sales MTD], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ), evaluated with the date context ending on 20 November.
- Check that the context really ends on the last day with data, using the to-date pattern, so it does not extend to 30 November.
- Consider teaching days: if the term started on a different weekday, compare teaching days to date instead.
Answer: Compare month to date with the same dates last year, limited to the last day with data, and consider teaching days for a fairer comparison.
Weeks are different
Calendar functions such as DATESYTD and SAMEPERIODLASTYEAR think in days, months, quarters and years. Weeks do not fit neatly into years, because 52 weeks are 364 days. A same-week-last-year comparison by calendar date drifts by a weekday each year, so a Monday is compared with a Sunday. For week-based reporting, Kwabena used columns in the date table, such as Week Start and Fiscal Week Number, and filtered on them, as in the same-week-last-fall measure earlier in this lesson. Organizations with retail-style 4-4-5 calendars face the same problem in a larger form, which is what calendar-based time intelligence is designed to address.
Common mistakes
MistakeUsing time intelligence functions on a fact table's own date column.
InsteadUse the marked, contiguous date table's date column.
MistakeForgetting the fiscal year-end argument for an organization whose year does not start in January.
InsteadSupply the year-end, such as "6/30", or build fiscal patterns from date table columns.
MistakeAdding up stock levels or headcount snapshots over time.
InsteadWrite semi-additive measures that take the closing value with LASTNONBLANK or a closing balance function.
MistakeComparing academic periods by calendar date when terms start on different days.
InsteadAdd term and week-of-term columns and compare at the same point in the term.
Summary and key terms
- Time intelligence functions return sets of dates relative to the current context and are used inside CALCULATE.
- They need a contiguous, marked date table related to the fact tables.
- DATESYTD and TOTALYTD give year to date; an optional year-end such as "6/30" sets a fiscal year.
- SAMEPERIODLASTYEAR and DATEADD shift dates; PARALLELPERIOD and PREVIOUSYEAR return whole periods.
- DATESINPERIOD supports rolling totals and moving averages.
- Semi-additive measures, such as stock on hand, add across dimensions but take closing values over time.
- Hide future periods in to-date measures so lines do not run flat past the latest data.
- Time intelligence
- DAX functions that shift or extend date filters relative to the current context.
- DATESYTD
- A function that returns the dates from the start of the year to the last date in context.
- SAMEPERIODLASTYEAR
- A function that returns the dates in context shifted back one year.
- DATEADD
- A function that shifts dates in context by a number of days, months, quarters or years.
- DATESINPERIOD
- A function that returns a run of dates of a given length ending on a given date.
- Semi-additive measure
- A measure that can be added across some dimensions but not over time.
- Closing balance
- The value at the last date of a period, used for balances and stock levels.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Write a measure for fiscal year to date registrations for a year ending 30 June.
Show answer
TOTALYTD ( [Registrations], 'Date'[Date], "6/30" )
Why did LASTDATE fail for some months of bookstore stock?
Show answer
Stock was recorded on the last business day; when the month ended on a weekend, the last calendar date had no rows.
Name a function for a rolling twelve-month total.
Show answer
DATESINPERIOD, used inside CALCULATE.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.