Forum Discussion

cristianml's avatar
cristianml
Post Prodigy
4 years ago
Solved

DATEADD Issue

Hi,

 I have an issue with DATEADD function. When I add -1 month it shows blank values.

 

_Rev Previous = CALCULATE([_Rev Actual],DATEADD('Calendar'[Date],-1,MONTH))

 

When I use 0 works fine. But I need to use -1, -3 months , etc

_Rev Previous = CALCULATE([_Rev Actual],DATEADD('Calendar'[Date],0,MONTH))

Any idea ?

 

Thanks!

 

 

  • OwenAuger's avatar
    OwenAuger
    4 years ago

    Thanks for posting back! 🙂

    The issue appears to be that the 'Calendar' tables is based on distinct dates from Actuals and Backlog, which appears to be one date per month.

    A Date table in Power BI must include all dates in the range from earliest to latest date present, even if some of those dates don't exist in fact tables.

    It's also good practice to include full financial years (and therefore full months), otherwise some time intelligence functions may give unexpected results.

     

    Here is a proposed definition of Calendar that would cover complete Sep-Aug years, and you should be able to mark as a date table:

     

    Calendar =
    VAR FYStartMonth =
    	9 // FY is Sep to Aug 
    VAR Offset =
        MOD ( 13 - FYStartMonth, 12 )
    VAR MinDate =
        MIN ( MIN ( Actuals[Date] ), MIN ( Backlog[Date] ) )
    VAR MaxDate =
        MAX ( MAX ( Actuals[Date] ), MAX ( Backlog[Date] ) )
    VAR MinDateFY =
        YEAR ( EOMONTH ( MinDate, Offset ) )
    VAR MaxDateFY =
        YEAR ( EOMONTH ( MaxDate, Offset ) )
    VAR CalendarStart =
        EDATE ( DATE ( MinDateFY, 1, 1 ), - Offset )
    VAR CalendarEnd =
        EOMONTH ( DATE ( MAXDateFY, 12, 1 ), - Offset )
    VAR CalendarFinal =
        CALENDAR ( CalendarStart, CalendarEnd )
    RETURN
        CalendarFinal

     

    There are other ways of building the Date table, e.g. Power Query scripts or tools like Bravo.

     

    Hopefully that helps!

    Regards,

    Owen

3 Replies

    • cristianml's avatar
      cristianml
      Post Prodigy

      hI OwenAuger ,

       

      It doesn't allow me to mark as a date table. It says date column can't have gaps in dates. But I don't see gaps there:

       

      • OwenAuger's avatar
        OwenAuger
        Super User

        Thanks for posting back! 🙂

        The issue appears to be that the 'Calendar' tables is based on distinct dates from Actuals and Backlog, which appears to be one date per month.

        A Date table in Power BI must include all dates in the range from earliest to latest date present, even if some of those dates don't exist in fact tables.

        It's also good practice to include full financial years (and therefore full months), otherwise some time intelligence functions may give unexpected results.

         

        Here is a proposed definition of Calendar that would cover complete Sep-Aug years, and you should be able to mark as a date table:

         

        Calendar =
        VAR FYStartMonth =
        	9 // FY is Sep to Aug 
        VAR Offset =
            MOD ( 13 - FYStartMonth, 12 )
        VAR MinDate =
            MIN ( MIN ( Actuals[Date] ), MIN ( Backlog[Date] ) )
        VAR MaxDate =
            MAX ( MAX ( Actuals[Date] ), MAX ( Backlog[Date] ) )
        VAR MinDateFY =
            YEAR ( EOMONTH ( MinDate, Offset ) )
        VAR MaxDateFY =
            YEAR ( EOMONTH ( MaxDate, Offset ) )
        VAR CalendarStart =
            EDATE ( DATE ( MinDateFY, 1, 1 ), - Offset )
        VAR CalendarEnd =
            EOMONTH ( DATE ( MAXDateFY, 12, 1 ), - Offset )
        VAR CalendarFinal =
            CALENDAR ( CalendarStart, CalendarEnd )
        RETURN
            CalendarFinal

         

        There are other ways of building the Date table, e.g. Power Query scripts or tools like Bravo.

         

        Hopefully that helps!

        Regards,

        Owen