Sub topic 6
Year to date
This chapter assumes you can write basic DAX measures with SUM and DIVIDE. It also assumes you have a marked date table related to your fact table, as built earlier in this tutorial. The examples use a Sales table with a Sales Amount column and a 'Date' table with a Date column.
What year to date means
A year-to-date (YTD) measure returns the total from the start of the year up to the last date in the current filter context. In a matrix with months on rows, the March row shows January through March. The April row shows January through April.
Start with a base measure. Every YTD measure builds on it.
Total Sales = SUM ( Sales[Sales Amount] )Two ways to write it
TOTALYTD is shorthand. It takes the measure to evaluate and the date column.
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )The longer form uses CALCULATE with DATESYTD. Both return the same result.
Sales YTD (CALCULATE) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) )DATESYTD returns a single-column table of dates. It runs from January 1 of the current year to the last date visible in the current filter context. CALCULATE replaces the existing date filter with that table, and [Total Sales] is then evaluated over the new range.
Why the date table matters
DATESYTD reads dates from the 'Date' table, not from the fact table. If the date table has gaps, the returned range has gaps, and the YTD total can be wrong. Use a contiguous date table that covers every day in your data.
Fiscal year ends
By default the year ends on December 31. TOTALYTD accepts an optional year-end date as its third argument. This measure resets after June 30.
Sales YTD Fiscal (June) =
TOTALYTD ( [Total Sales], 'Date'[Date], "6-30" )DATESYTD accepts the same argument, so the CALCULATE form can use it too.
Sales YTD Fiscal (CALCULATE) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date], "6-30" ) )With a June year end, July 2024 starts a new fiscal year. The July row shows only July sales, and August shows July plus August.
Stopping the line at the last sale
A YTD measure keeps returning the same total for months after your data ends. A line chart then runs flat to December. This version returns blank for dates after the last recorded sale.
Sales YTD Capped =
VAR LastSaleDate =
CALCULATE ( MAX ( Sales[Order Date] ), ALL ( 'Date' ) )
RETURN
IF ( MAX ( 'Date'[Date] ) <= LastSaleDate, [Sales YTD] )ALL on the date table removes the date filter, so LastSaleDate is the last order date in the whole model. The IF compares it with the last visible date and returns blank when that date is later.
Common errors
| Symptom | Likely cause |
|---|---|
| YTD equals the monthly total | The visual uses a column from the fact table, not `'Date'[Date]` |
| YTD never resets | The date column is not a true date, or the year-end argument is wrong |
| Totals look too low | The date table is missing days |
| Line stays flat after the last sale | No cap on future dates |
Exercise
A company's fiscal year ends on March 31. Write a YTD measure over [Total Sales] that resets on April 1.
Show answer
Sales YTD Fiscal = TOTALYTD ( [Total Sales], 'Date'[Date], "3-31" ). The third argument names the last day of the fiscal year, so the running total restarts the day after, on April 1.
Exercise
Your YTD measure shows a value for every month through December, but sales data ends in August. How do you fix it?
Show answer
Compare the last visible date with the last sale date, and return blank if it is later. Use the Sales YTD Capped pattern: get the last order date with CALCULATE ( MAX ( Sales[Order Date] ), ALL ( 'Date' ) ), then wrap the YTD measure in IF ( MAX ( 'Date'[Date] ) <= LastSaleDate, [Sales YTD] ).