What if you could instantly see how your sales this year compare to last year without hunting through dates?
Why SAMEPERIODLASTYEAR in Power BI? - Purpose & Use Cases
Imagine you have a sales report in a spreadsheet. You want to compare this year's monthly sales to the same months last year. You try to find last year's data by scrolling and matching dates manually.
This manual method is slow and confusing. You might pick the wrong dates or miss some months. It's easy to make mistakes and hard to update when new data arrives.
The SAMEPERIODLASTYEAR function automatically finds the matching dates from last year for any selected period. It saves time and avoids errors by doing the date matching for you.
Filter sales where Date = Date - 365 daysSAMEPERIODLASTYEAR(DateColumn)
You can quickly create accurate year-over-year comparisons that update automatically as new data comes in.
A retail manager uses SAMEPERIODLASTYEAR to compare this December's sales to last December's, spotting trends and making better stocking decisions.
Manual date matching is slow and error-prone.
SAMEPERIODLASTYEAR automates finding last year's matching dates.
This makes year-over-year analysis fast, accurate, and easy to update.
Practice
SAMEPERIODLASTYEAR function in Power BI?Solution
Step 1: Understand the function's purpose
SAMEPERIODLASTYEARshifts the date context back exactly one year to compare the same time periods.Step 2: Identify the correct use case
It is used to compare measures like sales or revenue from this year to the same period last year.Final Answer:
To compare data from the same period in the previous year -> Option BQuick Check:
SAMEPERIODLASTYEAR compares last year's period [OK]
- Confusing it with total aggregation functions
- Using it to filter current year data
- Assuming it creates future dates
SAMEPERIODLASTYEAR with a date column named Sales[Date]?Solution
Step 1: Recognize correct table and column reference
In DAX, table and column names are referenced as 'TableName'[ColumnName], so'Sales'[Date]is correct.Step 2: Check syntax correctness
SAMEPERIODLASTYEAR(Sales[Date]) omits single quotes around the table name. SAMEPERIODLASTYEAR(Sales.Date) uses invalid dot notation. SAMEPERIODLASTYEAR([SalesDate]) uses an incorrect column name format.Final Answer:
SAMEPERIODLASTYEAR('Sales'[Date]) -> Option AQuick Check:
Correct DAX column reference uses quotes and brackets [OK]
- Omitting single quotes around table name
- Using dot notation instead of brackets
- Referencing columns without table name
Sales LY = CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Sales'[Date]))If total sales for March 2023 are 1000, and for March 2022 are 800, what will
Sales LY show when filtered on March 2023?Solution
Step 1: Understand the measure calculation
The measure sums sales amounts but shifts the date filter to the same period last year usingSAMEPERIODLASTYEAR.Step 2: Apply the filter context for March 2023
When filtered on March 2023, the measure returns sales from March 2022, which is 800.Final Answer:
800 -> Option AQuick Check:
Sales LY shows last year's same period sales [OK]
- Assuming it sums current year sales
- Adding current and last year sales
- Returning zero if no explicit filter
Sales LY = CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR(Sales[Date]))But it returns an error. What is the likely cause?
Solution
Step 1: Check table and column references
The table name 'Sales' must be enclosed in single quotes when referenced in DAX, soSales[Date]is invalid.Step 2: Understand function requirements
SUM inside CALCULATE is valid, SAMEPERIODLASTYEAR requires a date column, and measure names can have spaces.Final Answer:
Missing single quotes around the table name 'Sales' -> Option DQuick Check:
Table names need single quotes in DAX [OK]
- Forgetting single quotes around table names
- Thinking SUM can't be inside CALCULATE
- Assuming SAMEPERIODLASTYEAR needs numeric columns
SAMEPERIODLASTYEAR to calculate this?Solution
Step 1: Understand the percentage change formula
Percentage change = (Current - Previous) / Previous, so we need current sales minus last year's sales divided by last year's sales.Step 2: Analyze each option
Sales YoY % = DIVIDE(SUM('Sales'[Amount]) - CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Sales'[Date])), CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Sales'[Date]))) correctly uses DIVIDE to avoid errors and calculates the difference over last year's sales. Sales YoY % = SUM('Sales'[Amount]) / SAMEPERIODLASTYEAR('Sales'[Date]) divides sales by a date column, which is invalid. Sales YoY % = CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Sales'[Date])) - SUM('Sales'[Amount]) subtracts current from last year without division. Sales YoY % = SUM('Sales'[Amount]) - CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Sales'[Date])) calculates difference but not percentage.Final Answer:
Sales YoY % = DIVIDE(SUM('Sales'[Amount]) - CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Sales'[Date])), CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Sales'[Date]))) -> Option CQuick Check:
Percentage change = (Current - LastYear) / LastYear [OK]
- Dividing by a date column instead of sales
- Forgetting to divide difference by last year's sales
- Subtracting without calculating percentage
Ready to prove it?
Pass the test and this pattern moves unknown
