Previous Year, Previous Month DAX in Power BI

In Power BI, using DAX (Data Analysis Expressions) to calculate Previous Month and Previous Year Sales is a common requirement for time-based comparisons. Here’s how you can create these calculations, using the sample data structure described earlier.

Sample Data Structure

Assume we have the following columns in our Sales table:

  • OrderDate: The date when the order was placed.
  • SalesAmount: The amount of sales for the order.

We also assume a DateTable which includes the necessary columns for date intelligence calculations (e.g., Date, Year, Month, MonthName, Quarter, etc.).

Previous Month Sales

To calculate the previous month’s sales, you can use the PREVIOUSMONTH function. This function returns a table containing all the dates from the previous month, based on the current context.

Previous Month Sales Measure

DAX
PreviousMonthSales = 
CALCULATE(
    SUM(Sales[SalesAmount]),
    PREVIOUSMONTH(DateTable[Date])
)

In this measure:

  • SUM(Sales[SalesAmount]) sums the SalesAmount column.
  • PREVIOUSMONTH(DateTable[Date]) adjusts the filter context to include only dates from the previous month.

Previous Year Sales

To calculate the previous year’s sales, you can use the PREVIOUSYEAR function. This function returns a table containing all the dates from the previous year, based on the current context.

Previous Year Sales Measure

DAX
PreviousYearSales = 
CALCULATE(
    SUM(Sales[SalesAmount]),
    PREVIOUSYEAR(DateTable[Date])
)

In this measure:

  • SUM(Sales[SalesAmount]) sums the SalesAmount column.
  • PREVIOUSYEAR(DateTable[Date]) adjusts the filter context to include only dates from the previous year.

Example Usage in a Report

  1. Load Data: Ensure your data (both Sales and DateTable) is loaded into Power BI.
  2. Create Date Table: If you don’t have a DateTable, you can create one using:DAX
  1. DateTable = CALENDAR(MIN(Sales[OrderDate]), MAX(Sales[OrderDate]))
  2. Establish Relationships: Create a relationship between the OrderDate column in the Sales table and the Date column in the DateTable.
  3. Create Measures: Add the PreviousMonthSales and PreviousYearSales measures using the DAX expressions provided.
  4. Add to Report: Drag and drop the new measures into your report visualizations (e.g., tables, bar charts) to compare sales data with previous month and previous year.

Visualization Example

  • Table: Create a table with columns OrderDate, SalesAmount, PreviousMonthSales, and PreviousYearSales to compare current sales with the previous month and previous year.
  • Line Chart: Plot OrderDate on the x-axis and the SalesAmount, PreviousMonthSales, and PreviousYearSales measures on the y-axis to visualize trends over time.

Sample Data and Visualization

To demonstrate, let’s assume we have the following sample data in our Sales table:

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

In this scenario, the PreviousMonthSales and PreviousYearSales measures will dynamically calculate the sales for the previous month and previous year based on the current filter context in your reports. This allows you to make time-based comparisons and identify trends in your sales data effectively.