Sub topic 7
Prior year comparison
This chapter assumes you have a contiguous, marked date table related to your fact table, and that you can write basic measures with SUM and CALCULATE. If not, review the date table chapter first. The goal here is a measure that returns the sales total for the same dates one year earlier, so you can place it next to the current value in a matrix or line chart.
The idea: shift the dates, not the data
A prior-year measure does not look up last year's rows directly. Instead, CALCULATE replaces the date filter with a shifted one. If a matrix cell is filtered to March 2025, the measure evaluates [Total Sales] with the filter set to March 2024. The sales table is then filtered through the relationship with the date table, and SUM adds up whatever rows remain.
SAMEPERIODLASTYEAR
SAMEPERIODLASTYEAR takes a column of dates and returns the same dates shifted back one year. Pass it to CALCULATE as a filter.
Total Sales = SUM ( Sales[Sales Amount] )
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )The function returns a table of dates, not a number. It looks at the dates currently visible in the filter context and moves each one back a year. For a month, it returns that month's days in the prior year. For a full year, it returns the whole prior year.
DATEADD as an alternative
DATEADD does the same shift but lets you choose the interval and the number of steps. A shift of -1 YEAR gives the same result as SAMEPERIODLASTYEAR.
Sales PY (DATEADD) =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, YEAR ) )Use SAMEPERIODLASTYEAR when you only need the prior year, because its name states the intent. Use DATEADD when you also need other shifts, such as -1, QUARTER or -1, MONTH, which are covered in a later chapter.
| Function | Shifts by | Best for |
|---|---|---|
| `SAMEPERIODLASTYEAR` | Exactly one year back | Plain prior-year comparison |
| `DATEADD` | Any number of days, months, quarters, or years | Flexible period shifts |
What to expect in a report
Place Year Month, [Total Sales], and [Sales PY] in a matrix. The first year in your data shows a blank for Sales PY, because no earlier rows exist. This is correct behavior, not an error. A date table that starts in 2020 does not help if the sales data itself starts later.
Common errors
- Using the fact table's date column. The shift then runs over only the dates that have sales, so periods with gaps return wrong or blank results.
- Forgetting to mark the date table. Time intelligence functions may behave unexpectedly when the table is not marked, or when the column has duplicates.
- Expecting a growth percentage.
Sales PYis only the earlier value. The percentage comes in the next chapter, where it is combined with variables andDIVIDE.
Exercise
A matrix shows [Total Sales] for each month of 2025, but [Sales PY] is blank for every month of 2020 and correct for all other years. Your date table starts on 2020-01-01 and your sales data starts on 2020-03-01. Is something wrong with the measure, and why?
Show answer
Nothing is wrong. For 2020, SAMEPERIODLASTYEAR looks for dates in 2019, which are not in the date table and have no sales rows, so the measure returns blank. Prior-year values begin only in 2021, when the shifted dates fall inside the data.
Exercise
Write a measure named Sales PY Quarter that returns the sales total for the same quarter in the previous year, using DATEADD. Why is the result the same as SAMEPERIODLASTYEAR at the quarter level?
Show answer
Sales PY Quarter = CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, YEAR ) ). Shifting the visible dates back one year yields the same quarter of the prior year, which is the same set of dates that SAMEPERIODLASTYEAR returns.