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
- Mark as Date table
- Using USERELATIONSHIP in DAX
- Sorting months in fiscal calendars
- Previous year up to a certain date
- Using CONCATENATEX in measures
- Automatic time intelligence in Power BI
- Creating a simple date table in DAX
- Year-to-date filtering weekdays in DAX
- Summing values for the total
- Counting working days in DAX
- Computing running totals in DAX