Forum Discussion
DateAdd returns blank values
- 10 years ago
You are correct about DATEADD. From the article you linked: "The result table includes only dates that exist in the dates column."
I think DATEADD is typically used for time intelligence formulas within a measure, when you're only looking to shift your context to a different scope of dates already in your model.
If you wanted to add a single day to your Date column using a new column, you could always take the Date column and add 1:
Column = TestDates[Date] + 1
If you want to subtract a single year, I tried this at first:
MinusOneYear = DATE(YEAR(TableName[Date])-1, MONTH(TableName[Date]), DAY(TableName[Date]))
That gives me an error ("An argument of function 'DATE' has the wrong data type or the result is too large or too small."), presumably for leap years, though I can't get rid of the error even accounting for leap years with an IF function. If I wrap all of it in IFERROR, it works, and 3/1/2015 is repeated because of the leap year, once for 3/1/2016 and once for 2/29/2016. This prevents the error, but I also don't see any blanks, so I'm not sure what caused the error:
MinusOneYear = IFERROR(DATE(YEAR(TableName[Date])-1, MONTH(TableName[Date]), DAY(TableName[Date])), BLANK())
You might also try this in M in the Query Editor as a new column there:
Custom = Date.AddMonths([Date], -12)
That results in 2/28/2015 being repeated instead of 3/1/2015, so you'll have to be careful either way.
An other work around is to add an auxiliar column year_month_id and work with integers. Ones you have it, you can do a lookup value to +- x*MONTHS to the same table.
The problem in my case, was that I had not all dates in the sales table.
Example for a 5 month shift:
shifted_date = LOOKUPVALUE(sales[date], sales[year_month_id],sales[year_month_id]-5).