How to use DAX date-based functions in Analysis Services tabular model
Part four of a six-part series of blogs

There are some wonderful date functions in DAX, with evocative names like TotalYtd, SamePeriodLastYear and ParallelPeriod. This blog shows how to use DAX date functions to summarise data, assuming that you have already created a calendar table.

  1. Creating date-based functions in DAX in SSAS Tabular
  2. Cumulative totals (TotalYTD, DatesYTD, etc)
  3. The same period in a previous month, quarter or year
  4. Using PARALLELPERIOD to get the whole of the last period (this blog)
  5. Moving averages using DAX date functions
  6. Semi-additive measures in DAX

This blog is part of our online SSAS Tabular tutorial; we also offer lots of other Analysis Services training resources.

Posted by Andy Brown on 08 February 2016

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Using PARALLELPERIOD to get the whole of the last period

The PARALLELPERIOD function returns a set of dates shifted forwards or backwards in time.  The easiest way to understand this sentence is by example!

Parallelperiod example

The shaded figures show cumulative sales to date for 2014 (1,490) as a percentage of total sales for the whole of 2013 (9,470).

 

Here's the formula for the PercentLastYear measure:

PercentLastYear2:=TOTALYTD(

sum('Transaction'[Quantity]),

Calendar[Date]

) /

CALCULATE(

sum('Transaction'[Quantity]),

PARALLELPERIOD(

Calendar[Date],

-1,

YEAR)

)

That is, divide total year-to-date quantity sold by the total quantity sold one year ago.

Avoiding returning an error

The measure above shows a divide-by-zero error for 2013 figures, because the previous year's data doesn't exist.  One way to get round this would be to use an IFERROR function to trap the error, and return blank:

PercentLastYear:=IFERROR(

TOTALYTD(

sum('Transaction'[Quantity]),

Calendar[Date]

) /

CALCULATE(

sum('Transaction'[Quantity]),

PARALLELPERIOD(

Calendar[Date],

-1,

YEAR

)

),

BLANK()

)

That is, if dividing the year-to-date total sales by the previous year's total sales returns an error, show a blank instead.  This would give:

Blanks instead of errors

Much better!

 

Having gone through 3 relatively straightforward tasks, let's now look at something a bit more challenging: displaying moving averages. 

This blog has 0 threads Add post