Frage

How do you reliably add 10 months to a date in the DAX formula for a PowerPivot calculated column?

My first attempt only works for dates that already exist in the EventDate column.

=DATEADD(Dates[EventDate], 10, month)

As Chris Webb wrote in his blog DATEADD is unreliable as it returns blanks whenever the resulting date is not found in the EventDate column.

My second attempt always returns data, but the day of the month is incorrect.

=[EventDate] + 10 * 31

Notes: All dates in EventDate are on the first day of the month. I am using PowerPivot for Excel 2010. The formula is for the financial year hierarchy in a date dimension.

Is there a better way to add months in DAX?

War es hilfreich?

Lösung

This is an old one but may still be of use......

=IF(
    DAY([Eventdate])>DAY(EOMONTH([Eventdate],10)),
             DATE(year(eomonth([Eventdate],10)),month(eomonth([Eventdate],10)),day(EOMONTH([Eventdate],10))),
             DATE(year(eomonth([Eventdate],10)),month(eomonth([Eventdate],10)),day([Eventdate]))
    )

This assumes that where the 'partner' 10 months on has fewer days it will default to the last day of that month.

HTH Jacob

Andere Tipps

Another way to add time to a date using DAX is using EDATE(), this function add months and it will return the right day of the month. For example:

Foo = EDATE(Dates[EventDate], 10)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top