Sameperiodlastyear in DAX Power bi

In DAX, the SAMEPERIODLASTYEAR function is used to compare values from the same period in the previous year. This is particularly useful for year-over-year (YoY) analysis, allowing you to compare, for instance, the sales of May 2024 with the sales of May 2023.

Syntax

DAX
SAMEPERIODLASTYEAR(<dates>)
  • <dates>: A column containing dates.

Implementation Example

Let’s implement a measure using the SAMEPERIODLASTYEAR function to compare the sales from the same period last year based on the sample data provided.

Steps

  1. Ensure Date Table: Make sure there is a date table in your data model. This table should have a continuous range of dates covering all periods in your sales data.
  2. Establish Relationships: Ensure the OrderDate column in the Sales table is related to the Date column in the Date table.
  3. Create Measures: Define a measure to calculate total sales and another to calculate sales for the same period last year.

Sample Date Table

Here’s a simple Date table to use:

DAX
DateTable = CALENDAR(DATE(2023, 1, 1), DATE(2024, 12, 31))

Create Measures

  1. Total Sales Measure:
DAX
Total Sales = SUM(Sales[SalesAmount])
  1. Sales Same Period Last Year Measure:
DAX
Sales Same Period Last Year = 
CALCULATE(
    [Total Sales], 
    SAMEPERIODLASTYEAR('DateTable'[Date])
)

Example Usage in a Power BI Report

Let’s use the sample data provided and see how these measures would work.

Sample Data in Power BI

OrderIDOrderDateSalesAmountCategory
12023-01-15150.00Technology
22023-02-20250.00Office Supplies
32023-03-25300.00Furniture
42023-04-10100.00Technology
52023-05-05400.00Office Supplies
62023-06-15350.00Furniture
72023-07-20450.00Technology
82023-08-25200.00Office Supplies
92023-09-10500.00Furniture
102023-10-15600.00Technology
112024-01-10700.00Office Supplies
122024-02-15800.00Furniture
132024-03-20900.00Technology
142024-04-251000.00Office Supplies
152024-05-011100.00Furniture
162024-05-051200.00Technology
172024-05-101300.00Office Supplies
182024-05-151400.00Furniture
192024-05-201500.00Technology
202024-06-011600.00Office Supplies
212024-06-051700.00Furniture
222024-07-101800.00Technology
232024-08-151900.00Office Supplies
242024-09-202000.00Furniture
252024-10-252100.00Technology
262024-11-012200.00Office Supplies
272024-12-052300.00Furniture
282024-12-102400.00Technology
292024-12-152500.00Office Supplies
302024-12-202600.00Furniture

Result Analysis

  1. Total Sales: This measure will simply sum up the SalesAmount for the selected period.
  2. Sales Same Period Last Year: This measure will sum up the SalesAmount for the same period in the previous year.

For example, if you are looking at sales in May 2024:

  • Total Sales for May 2024: The sum of SalesAmount for orders in May 2024.
    • 1100 + 1200 + 1300 + 1400 + 1500 = 6500
  • Sales Same Period Last Year for May 2023: The sum of SalesAmount for orders in May 2023.
    • 400

By placing these measures in a visual, such as a table or line chart, you can easily compare the sales of May 2024 with the sales of May 2023.

Visual Example in Power BI

You can create a table or a line chart in Power BI to visualize this comparison. For instance:

  • Table Visualization:
    • Columns: Date, Total Sales, Sales Same Period Last Year
    • Rows: May 2024
  • Line Chart:
    • X-Axis: Date
    • Y-Axis: Total Sales, Sales Same Period Last Year

This will help you to visually compare the performance of sales between the current period and the same period last year.