# Comparing Two Different Periods in DAX

Comparing different time periods is one of the most commonly required functionalities in models. Time functions such as cumulative sum or the same period last year are often needed in any BI solutions. Additionally, users increasingly express the need for flexibility in formulating comparative periods. How to address this if we want to compare different (even non-identical) time periods using two date slicers?

The following image best describes it:

How to create such a solution step by step?

- Create a model where the user can select two time periods.
- Create DAX measures to calculate values for both periods.
- Bring values to a common denominator.

The first step is to create a calendar table. We can do this in many ways; for the purposes of this exercise, let it be:

Calendar = CALENDAR( MIN(financials[Date]), MAX(financials[Date]) )

PrevCalendar = ALLNONBLANKROW('Calendar')

_Current Sales =

SUM(financials[Sales])

_Previous Sales =

CALCULATE([_Current Sales],

ALL(‘Calendar’),

USERELATIONSHIP(

‘Calendar'[Date],PrevCalendar[Date]
)

)

_Factor = DIVIDE( COUNTROWS('Calendar'), COUNTROWS('PrevCalendar') )

_Previous Sales = CALCULATE( [_Current Sales], ALL('Calendar'), USERELATIONSHIP( 'Calendar'[Date],PrevCalendar[Date] ) )*[_Factor]

The solution is not difficult to implement. I always plan it when building a data model, and when creating measures, I always ensure that they allow for the same calculations in two selected periods. This saves additional work when implementing a new change later on.