If it is necessary to compare one month against the same month in the previous year, this calculation provides a good starting point:
Previous Year = CALCULATE ( [Sales Amount], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
With this measure, a report can show current year sales against previous year sales:
Unfortunately, the calculation is not perfect. At the year level, it compares the full previous year against an incomplete current year – in this example there are no sales after September 5th in the current year.
Besides, the problem appears not only at the year level, but also at the month level. Indeed, in September the Previous Year measure returns sales for the entire month of September in the previous year. The comparison is unfair, as there are only five days’ worth of sales in September of the current year.
A possible solution is to create a calculated column in the Date table in order to remove dates in the past that should be ignored. If the last date in the fact table is September 5th for the current year, then all the dates after September 5th in previous years can be marked to avoid considering them in the calculation.
The code for that calculated column is:
IsPast = VAR LastSaleDate = MAX ( Sales[Order Date] ) VAR LastSaleDatePY = EDATE ( LastSaleDate, -12 ) RETURN 'Date'[Date] <= LastSaleDatePY
This code stores the last date of sales into LastSaleDate, then it moves it back one year (twelve months) using the EDATE function. Finally, it checks whether the current date is earlier than the last date in the previous year. The next figure shows that IsPast calculated column, which changes value on September 6th, 2008 (in the demo model, the last year is 2009):
Once the column is in place, the Adjusted Previous Year measure can compute a variation of the Previous Year calculation. The variation specifies that it only considers dates in the previous year for which the IsPast column is True:
Adjusted Previous Year = CALCULATE( [Sales Amount], SAMEPERIODLASTYEAR ( 'Date'[Date] ), 'Date'[IsPast] = TRUE )
The next figure shows the result – it provides a fairer comparison between current and previous years:
Returns the date that is the indicated number of months before or after the start date.
EDATE ( <StartDate>, <Months> )
Articles in the DAX 101 series
- Computing running totals in DAX
- Counting working days in DAX
- Summing values for the total
- Year-to-date filtering weekdays in DAX
- Creating a simple date table in DAX
- Automatic time intelligence in Power BI
- Using CONCATENATEX in measures
- Previous year up to a certain date
- Sorting months in fiscal calendars
- Using USERELATIONSHIP in DAX
- Mark as Date table
- Row context in DAX
- Filter context in DAX
- Introducing CALCULATE in DAX
- Understanding context transition in DAX
- Using KEEPFILTERS in DAX
- Variables in DAX