0% found this document useful (0 votes)
22 views

YOY Growth in DAX and SQL

This document provides a comprehensive guide on Year-over-Year (YoY) analysis using DAX in Power BI and SQL queries. It details various methods for calculating YoY values and percentage growth, including SAMEPERIODLASTYEAR(), DATEADD(), PARALLELPERIOD(), and TOTALYTD() in DAX, as well as LAG() in SQL. Each method is explained with formulas and best use cases to assist data analysts in tracking business growth and trends effectively.

Uploaded by

Kounik Samanta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

YOY Growth in DAX and SQL

This document provides a comprehensive guide on Year-over-Year (YoY) analysis using DAX in Power BI and SQL queries. It details various methods for calculating YoY values and percentage growth, including SAMEPERIODLASTYEAR(), DATEADD(), PARALLELPERIOD(), and TOTALYTD() in DAX, as well as LAG() in SQL. Each method is explained with formulas and best use cases to assist data analysts in tracking business growth and trends effectively.

Uploaded by

Kounik Samanta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mandatory Questions for Every Data Analyst Interview

Year-over-Year (YoY) & Growth Analysis in Power BI &


SQL – A Complete Guide with DAX & LAG()!
Year-over-Year (YoY) analysis is essential for tracking business growth and identifying
trends over time. This guide explores various DAX methods in Power BI and SQL queries
for calculating YoY values and percentage growth efficiently.

YoY Calculation in Power BI using DAX


Below are multiple ways to calculate YoY value (absolute difference) and YoY % growth
using DAX.

1. Basic YoY Calculation Using SAMEPERIODLASTYEAR()

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

2. Flexible YoY Using DATEADD()

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

3. Fixed Period YoY Using PARALLELPERIOD()

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

4. Year-to-Date (YTD) YoY Using TOTALYTD()

This compares cumulative sales up to the same date in the previous year.

DAX Formula:

YoY Sales YTD =


VAR PreviousYearSalesYTD = CALCULATE(TOTALYTD(SUM(SalesData[Sales]),
SalesData[Date]), SAMEPERIODLASTYEAR(SalesData[Date]))
RETURN TOTALYTD(SUM(SalesData[Sales]), SalesData[Date]) - PreviousYearSalesYTD

YoY % Growth YTD =


VAR PreviousYearSalesYTD = CALCULATE(TOTALYTD(SUM(SalesData[Sales]),
SalesData[Date]), SAMEPERIODLASTYEAR(SalesData[Date]))
RETURN DIVIDE(TOTALYTD(SUM(SalesData[Sales]), SalesData[Date]) -
PreviousYearSalesYTD, PreviousYearSalesYTD, 0) * 100
Mandatory Questions for Every Data Analyst Interview

YoY Calculation in SQL using LAG()


SQL also provides an efficient way to calculate YoY and YoY % growth using LAG().

SQL Query for YoY & YoY % Growth Using LAG()

SELECT

YEAR(Date) AS Year,

SUM(Sales) AS CurrentYearSales,

LAG(SUM(Sales)) OVER (ORDER BY YEAR(Date)) AS PreviousYearSales,

SUM(Sales) - LAG(SUM(Sales)) OVER (ORDER BY YEAR(Date)) AS YoY_Change,

ROUND(((SUM(Sales) - LAG(SUM(Sales)) OVER (ORDER BY YEAR(Date))) /

NULLIF(LAG(SUM(Sales)) OVER (ORDER BY YEAR(Date)), 0)) * 100, 2) AS


YoY_Percentage_Growth

FROM SalesTable

GROUP BY YEAR(Date)

ORDER BY Year;

Function Description Flexibility Best For


Returns the same Simple YoY
Not Flexible
period (day, month, comparisons (e.g.,
SAMEPERIODLASTYEAR() (Always moves
year) from the Sales from the same
1 year back).
previous year. day last year).
Shifts the date Custom YoY
Highly Flexible
context forward or comparisons (e.g.,
(Allows
DATEADD() backward by any Moving YoY by 2
shifting by any
interval (days, years or comparing
period).
months, years). quarterly shifts).
Moves the entire Moderate
Fixed period YoY
period back by a Flexibility
(e.g., Last Year, Last
PARALLELPERIOD() fixed interval (Only works
Quarter, Last
(month, quarter, or with fixed
Month).
year). periods).
Mandatory Questions for Every Data Analyst Interview

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.

Each method has its strengths—choose based on your reporting needs!

Do you use a different approach for YoY analysis? Drop your thoughts in the
comments!

You might also like