Forum Discussion

abaconsup's avatar
abaconsup
Advocate I
10 years ago
Solved

DateAdd returns blank values

I've been struggling with the DATEADD function today, I was expecting that this function could alter a date such that you can simply add or subtract days, months or years.   However, the function a...
  • KGrice's avatar
    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.