Sub topic 6
Year to date
This chapter assumes you can write basic measures with SUM and DIVIDE. It also assumes you have a contiguous, marked Date table related to your Sales fact table, as built in the earlier chapters. Here you write year-to-date (YTD) measures in two ways, see what they return, and learn the errors that most often break them.
What a year-to-date measure does
A year-to-date value is the running total from January 1 of the current year up to a given date. In a matrix with months on the rows, each row shows the sum of all sales from the start of that year through the end of that month.
| Month | Total Sales | Sales YTD |
|---|---|---|
| January | 100 | 100 |
| February | 150 | 250 |
| March | 120 | 370 |
The measure does not loop over months or keep a running variable. For each cell, it widens the date filter from "this month" to "January 1 through the end of this month", then sums sales in that wider window.
The two forms
Both forms below use the same base measure.
Total Sales = SUM ( Sales[Sales Amount] )
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales YTD (CALCULATE) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) )TOTALYTD is shorthand for CALCULATE with DATESYTD. The two measures return the same numbers. Which one you choose depends on what else the measure needs:
TOTALYTDis shorter and reads clearly when the measure only needs a YTD total.CALCULATEwithDATESYTDis better when you want to add more filters. Add them as extra arguments toCALCULATE, such asProduct[Category] = "Bikes". You can also combine the YTD window with other table functions.
What DATESYTD returns
DATESYTD is a table function. It returns a one-column table containing every date from January 1 of the current year through the last date in the current filter context. It does not return a number.
Take a matrix cell for March 2024. The filter context on Date contains March 1 through March 31, 2024. DATESYTD finds the last date in that context, March 31, and returns every date from January 1 through March 31, 2024. CALCULATE then uses that table as the new date filter, and [Total Sales] is evaluated against it.
The key step is the replacement in CALCULATE. The original March filter is discarded, and the Jan 1 to Mar 31 window takes its place. Filters on other tables, such as a product category slicer, are untouched and still apply.
Checking the window yourself
When a YTD number looks wrong, check which dates the measure actually sees. These two diagnostic measures put the window's boundaries in a card or matrix:
YTD First Date =
CALCULATE ( MIN ( 'Date'[Date] ), DATESYTD ( 'Date'[Date] ) )
YTD Last Date =
CALCULATE ( MAX ( 'Date'[Date] ), DATESYTD ( 'Date'[Date] ) )For the March 2024 cell, they should return January 1, 2024 and March 31, 2024. If the first date is not January 1, or the last date is not the end of the period you expect, the date table or the visual's fields are the problem. The measure is probably fine.
Which date counts as "the last date"
DATESYTD uses the last date in the current filter context. This causes the most common confusing result: a visual that shows the same year-to-date number in several rows.
Suppose a matrix has only Month Name on the rows, such as January, February and March, with no year field. Each row's filter context contains that month in every year of the data. The last date in the context is therefore in the latest year, say March 31, 2025. DATESYTD then builds a window for 2025 only. Earlier years' sales for that month are dropped from the result.
To avoid this, put a year field in the visual or in a slicer. A hierarchy such as Year then Month works well. A single Year Month column such as 2024-03 also works, because each value maps to one specific month.
The same logic applies at other levels:
- In a year row, the last date is December 31, so the YTD value equals the full-year total.
- In a day row, the last date is that day, so the value is the running total up to that day.
- In a grand total row with several years selected, the window is the YTD of the latest year only. That total will not equal the sum of the rows above it. This is expected, and you should explain it to report readers.
Fiscal years
By default, the year resets on December 31. For a fiscal year that ends on another date, pass the year-end date as an extra argument:
Sales YTD Fiscal (June) =
TOTALYTD ( [Total Sales], 'Date'[Date], "6-30" )The measure now resets after June 30. In a matrix, the value for July 2024 is the total of July 1 only through the end of July, because a new fiscal year has just started. The CALCULATE form accepts the same argument: DATESYTD ( 'Date'[Date], "6-30" ). The string is the month and day of the fiscal year end. A later chapter covers fiscal calendars in more detail.
Stopping the line at the last sale
A YTD measure keeps returning the last running total for every later date in the year. On a line chart, that draws a flat line from the last sale to December 31. Blank out the dates after the last recorded sale:
Sales YTD Capped =
VAR LastSaleDate =
CALCULATE ( MAX ( Sales[Order Date] ), ALL ( 'Date' ) )
VAR CurrentDate = MAX ( 'Date'[Date] )
RETURN
IF ( CurrentDate <= LastSaleDate, [Sales YTD] )ALL ( 'Date' ) removes the date filters so that MAX finds the last order date in the whole fact table. The IF has no else branch, so it returns blank for later dates, and charts leave blank points out of the line. This measure repeats a pattern covered in the chapter on incomplete periods.
Common errors
These are the YTD problems that appear most often.
- The measure returns the same value as
Total Sales, or only the current month. The date column passed in is not the one the visual filters on. A frequent cause is usingSales[Order Date]instead of'Date'[Date]. Always pass the column from the date table. - The YTD value is wrong or missing for some days. The date table has gaps, or it does not cover the full range of your data. Check that every day appears once and that the table runs from before your earliest sale to after your latest.
- A relationship points at the wrong column. If the active relationship uses a different date column, such as ship date, the measure follows that one. Use
USERELATIONSHIPinsideCALCULATEto switch to another relationship. - The date table is not marked. Mark
Dateas the model's date table, using theDatecolumn. This lets the engine apply time-intelligence filters across the whole date table reliably. - A text or number "date" column is passed in. Time intelligence functions need a real date type. A text column such as
2024-03cannot be used as the dates argument. - The grand total does not match the sum of the rows. This is the behavior described earlier. It is not a bug.
Choosing between the two forms
If a model needs several YTD measures, define the base measure once and build each YTD variant on it. The YTD measures then inherit any fix to the base calculation.
Sales YTD Bikes =
CALCULATE (
[Total Sales],
DATESYTD ( 'Date'[Date] ),
Product[Category] = "Bikes"
)Here CALCULATE applies two filters at once. The category filter is a normal column filter, and the date filter comes from DATESYTD. TOTALYTD can do the same through its optional filter argument, but the CALCULATE form makes each filter easier to see.
Exercise
A matrix shows Month Name (January to December) on the rows and no year field. A YTD measure built on DATESYTD returns the same pattern of values as the latest year only, and earlier years' sales are missing from every row. Why does this happen, and how do you fix it?
Show answer
Each row's filter context holds that month across all years. DATESYTD uses the last date in that context, which falls in the latest year, so the window covers January 1 to the end of the month in that year only. Add a year field to the rows, use a Year > Month hierarchy, or add a year slicer, so each cell's filter context contains a single year.
Exercise
Write a measure named Sales YTD March that returns year-to-date sales for a fiscal year ending on March 31, using CALCULATE and DATESYTD.
Show answer
Sales YTD March = CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date], "3-31" ) )