Sub topic 7
Prior Year Comparison
This chapter shows how to write a prior-year measure. It covers what SAMEPERIODLASTYEAR returns, how it differs from DATEADD, how to reuse it with other measures, and where it breaks. It assumes you have a working date table marked as the model's date table, and that you know the basic filter-context and CALCULATE behavior from the earlier chapters. It also assumes [Total Sales] is defined as SUM ( Sales[Sales Amount] ).
The idea: evaluate the same measure on shifted dates
A prior-year measure does not look up last year's row. It takes the dates the visual currently filters to, moves them back one year, and evaluates the same measure on those dates. CALCULATE applies the shifted dates as a replacement filter on 'Date'[Date].
The measure is short:
Total Sales = SUM ( Sales[Sales Amount] )
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )Read Sales PY from the inside out:
'Date'[Date]in the current filter context holds the dates the row or cell selects. In a month row, that is every day in the month.SAMEPERIODLASTYEARreturns a one-column table of the same dates, each one year earlier.CALCULATEuses that table as a filter on'Date'[Date]. The new filter replaces the existing filter on that column, so the current-year dates are gone.[Total Sales]is evaluated in the new context, and the result is returned to the original cell.
Other filters in the context stay in place. If the visual is split by product category, each cell shows that category's sales for the prior-year period. Only the date filter moves.
What SAMEPERIODLASTYEAR returns
The function returns a table, not a number. You can see its effect by returning the first and last date it produces:
PY First Date =
CALCULATE ( MIN ( 'Date'[Date] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
PY Last Date =
CALCULATE ( MAX ( 'Date'[Date] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )Put both measures in a matrix next to Sales PY, with 'Date'[Year Month] on the rows. For the 2024-03 row, you should see 2023-03-01 and 2023-03-31. These two measures are useful when a prior-year number looks wrong. They show which dates were used.
The function works on the dates in the current context, so it follows the grain of the visual:
| Visual grain | Current dates | Dates returned |
|---|---|---|
| Day | 15 Mar 2024 | 15 Mar 2023 |
| Month | 1 to 31 Mar 2024 | 1 to 31 Mar 2023 |
| Year | 1 Jan to 31 Dec 2024 | 1 Jan to 31 Dec 2023 |
| Day, leap day | 29 Feb 2024 | 28 Feb 2023 |
The leap-day row shows an edge case. The shift is by calendar date, and 29 February 2023 does not exist. That day's prior-year value is therefore the sales of 28 February. In a monthly or yearly view this does not matter, because the whole period is shifted as a block.
SAMEPERIODLASTYEAR and DATEADD
The spine includes a second form of the same measure:
Sales PY (DATEADD) =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, YEAR ) )For a prior-year comparison the two measures give the same result. SAMEPERIODLASTYEAR ( dates ) is shorthand for DATEADD ( dates, -1, YEAR ). Which one to use is a style decision:
- Use
SAMEPERIODLASTYEARwhen the intent is exactly "last year". It is easier to read. - Use
DATEADDwhen the interval may change, such as the previous quarter, the previous month, or two years back. A later chapter covers those shifts.
Both functions have the same requirements. They need a date column from a proper date table, and they need a contiguous selection of dates.
Reusing the shift with other measures
You can wrap any measure in the same shifted filter. A common case is comparing a year-to-date total with the year-to-date total for the previous year:
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales PY YTD =
CALCULATE ( [Sales YTD], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )This works because of evaluation order. CALCULATE first applies the shifted dates. Then [Sales YTD] runs in that context, and TOTALYTD looks at the last date of the shifted selection. If the visual shows 31 March 2024, the shifted selection ends at 31 March 2023. The year-to-date total is therefore the sum from 1 January to 31 March 2023.
To avoid duplicated logic, build prior-year measures on top of base measures. Don't copy the aggregation into each one:
- Define
[Total Sales]once. - Define
[Sales PY]from[Total Sales]. - Define
[Sales PY YTD]from[Sales YTD].
If the definition of sales changes, you edit one measure. The chapter on year-over-year growth builds on [Sales PY] in the same way.
A worked example
Suppose the model holds these monthly totals:
| Month | Total Sales | Sales PY |
|---|---|---|
| 2023-01 | 100 | blank |
| 2023-02 | 90 | blank |
| 2024-01 | 120 | 100 |
| 2024-02 | 85 | 90 |
| 2024-03 | 140 | blank |
Two of these results need an explanation.
- The 2023 rows are blank because the model has no 2022 data. The shifted dates match nothing in the fact table. The
Datetable does contain 2022 only if you created it that way. In the spine's example it starts in 2020, so the dates exist but there are no sales on them.SUMover no rows returns blank, so the measure stays blank. - The 2024-03 row is blank in
Sales PYbecause March 2023 has no sales rows in this sample. In a real model, check that the prior-year data was loaded before you blame the measure.
The blank result is correct behavior. It is useful because a blank prior-year value stays blank in later arithmetic with DIVIDE.
Where prior-year measures go wrong
The most common problems are these:
- Wrong date column in the visual. Axis, rows, and slicers must use columns from the
Datetable. If you dragSales[Order Date]onto the axis, the time-intelligence filter and the visual filter act on different columns. - Gaps in the date table. If the table skips days, the shifted period is incomplete and the result may be wrong or an error. The calculated table from the earlier chapter, built with
CALENDAR, has no gaps. - Date table not marked. An unmarked table can work in simple cases. A marked table is the supported setup, and it keeps the time-intelligence filters reliable when other filters touch the date table.
- Non-contiguous selections. A slicer where the user picks January and March only gives a selection with a gap.
SAMEPERIODLASTYEARandDATEADDcan return an error for such a selection, because they need a continuous range. - Comparing a partial period with a full one. If the current month is half finished, the prior-year value is the whole month. The chapter on incomplete periods handles that case.
- Hierarchies and text columns. A
Year Monthtext column is fine for display. The shift still works because the function reads'Date'[Date]from the filter context, not the label. But sortYear Monthcorrectly, or the rows appear in alphabetical order.
Summary of the pattern
- A prior-year measure is
CALCULATE ( <measure>, SAMEPERIODLASTYEAR ( 'Date'[Date] ) ). - The function returns a table of dates, shifted one year back from the current selection.
CALCULATEreplaces the date filter with that table and leaves other filters alone.- Any measure, including
TOTALYTDmeasures, can be wrapped this way. - Blank means no sales on the shifted dates, not an error.
Exercise
A matrix shows Sales PY by month, and the 2024-02 row returns the same value as Total Sales for 2024-02. The rows come from Sales[Order Date], not from the Date table. What is the likely cause, and what fix do you apply?
Show answer
The visual filters Sales[Order Date], and SAMEPERIODLASTYEAR shifts only 'Date'[Date]. The shifted filter is not applied to the column the visual uses, so the measure is not evaluated on prior-year dates. Replace the row field with a column from the Date table, such as 'Date'[Year Month]. Make sure the Date table is related to Sales[Order Date] and marked as the date table.
Exercise
Write a measure named Sales PY QTD that returns the quarter-to-date total for the same period one year earlier. Assume Sales QTD = TOTALQTD ( [Total Sales], 'Date'[Date] ) already exists.
Show answer
Sales PY QTD = CALCULATE ( [Sales QTD], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ) CALCULATE first shifts the selected dates back one year. TOTALQTD then runs on the shifted selection and sums from the start of that quarter to its last date.