Wednesday, 17 August 2016

Date shifting with PowerBI and the DateAdd DAX function

I was recently playing with PowerBI Desktop to build a report which compared one years' figures to another.

So for example, if you have a column of dates such as
01 August 2016
02 August 2016
03 August 2016
I wanted to create a new column that time shifts these dates to the previous year to give:
01 August 2015
02 August 2015
03 August 2015
Simple... or so I thought. The DateAdd function seemed a likely contender, so I tried
Column = DATEADD(MyTable[Date],-1,YEAR)
What stumped me was that this returned nothing for each entry, just empty cells.

After re-reading the MSDN function reference and reaching out via this PowerBI blog post I found that DateAdd only returns dates that are in the original data set, i.e.
"The result table includes only dates that exist in the dates column."
Ultimately, the most elegant solution for me was to create the modified dates in the Query Editor using M and the Date.AddYears function (not using DAX functions within PowerBI formula):
Custom = Date.AddYears([Date], -1)
Credit goes to KGrice for helping find this solution.

No comments:

Post a Comment