YOY Growth in DAX and SQL
YOY Growth in DAX and SQL
This method shifts the date context exactly one year back to get last year's value.
DAX Formula:
YoY Sales =
VAR PreviousYearSales = CALCULATE(SUM(SalesData[Sales]),
SAMEPERIODLASTYEAR(SalesData[Date]))
RETURN SUM(SalesData[Sales]) - PreviousYearSales
YoY % Growth =
VAR PreviousYearSales = CALCULATE(SUM(SalesData[Sales]),
SAMEPERIODLASTYEAR(SalesData[Date]))
RETURN DIVIDE(SUM(SalesData[Sales]) - PreviousYearSales, PreviousYearSales, 0) *
100
DATEADD() shifts the time context by a specific period, making it more flexible.
DAX Formula:
YoY Sales =
VAR PreviousYearSales = CALCULATE(SUM(SalesData[Sales]),
DATEADD(SalesData[Date], -1, YEAR))
RETURN SUM(SalesData[Sales]) - PreviousYearSales
YoY % Growth =
VAR PreviousYearSales = CALCULATE(SUM(SalesData[Sales]),
DATEADD(SalesData[Date], -1, YEAR))
Mandatory Questions for Every Data Analyst Interview
RETURN DIVIDE(SUM(SalesData[Sales]) - PreviousYearSales, PreviousYearSales, 0) *
100
Similar to DATEADD(), but only works for fixed intervals (year, quarter, month).
DAX Formula:
YoY Sales =
VAR PreviousYearSales = CALCULATE(SUM(SalesData[Sales]),
PARALLELPERIOD(SalesData[Date], -1, YEAR))
RETURN SUM(SalesData[Sales]) - PreviousYearSales
YoY % Growth =
VAR PreviousYearSales = CALCULATE(SUM(SalesData[Sales]),
PARALLELPERIOD(SalesData[Date], -1, YEAR))
RETURN DIVIDE(SUM(SalesData[Sales]) - PreviousYearSales, PreviousYearSales, 0) *
100
This compares cumulative sales up to the same date in the previous year.
DAX Formula:
SELECT
YEAR(Date) AS Year,
SUM(Sales) AS CurrentYearSales,
FROM SalesTable
GROUP BY YEAR(Date)
ORDER BY Year;
Final Thoughts
YoY Value helps measure the absolute difference in sales between two years.
YoY Percentage Growth helps understand the relative increase or decrease.
If you need simple and quick YoY, use SAMEPERIODLASTYEAR().
If you need custom time comparisons, use DATEADD().
If you need cumulative YoY, use TOTALYTD().
If you need rolling 12-month analysis, use DATESINPERIOD().
In SQL, LAG() provides an efficient way to calculate YoY without joins.
Do you use a different approach for YoY analysis? Drop your thoughts in the
comments!