Forum Discussion

Durbslaw's avatar
Durbslaw
Helper I
4 years ago
Solved

conditional datediff

Hi,   I have 2 columns Period [YearMonth] which is a date filter and MovementSnapshot[AppointmentDatekey]. I would like a measure to datediff the Month if MovementSnapshot[AppointmentDatekey] is n...
  • tamerj1's avatar
    4 years ago

    Durbslaw 
    Please try this as a measure

     

    NewColumn =
    IF (
        'MovementSnapshot'[AppointmentDateKey] <> BLANK ()
            && 'MovementSnapshot'[AppointmentDateKey] <> -1,
        VAR CurrentMovement =
            MAX ( 'MovementSnapshot'[AppointmentDateKey] )
        VAR CurrentPeriod =
            MAX ( 'Period'[YearMonth] )
        VAR MovementYear =
            VALUE ( LEFT ( CurrentMovement, 4 ) )
        VAR MovementMonth =
            VALUE ( MID ( CurrentMovement, 5, 2 ) )
        VAR PeriodYear =
            VALUE ( LEFT ( CurrentPeriod, 4 ) )
        VAR PeriodMonth =
            VALUE ( RIGHT ( CurrentPeriod, 2 ) )
        RETURN
            ( MovementYear - PeriodYear ) * 12 + MovementMonth - PeriodMonth
    )

     

  • tamerj1's avatar
    tamerj1
    4 years ago

    Durbslaw 
    Sorry my mistake. Please try

     

    NewColumn =
    VAR CurrentMovement =
        MAX ( 'MovementSnapshot'[AppointmentDateKey] )
    VAR CurrentPeriod =
        MAX ( 'Period'[YearMonth] )
    VAR MovementYear =
        VALUE ( LEFT ( CurrentMovement, 4 ) )
    VAR MovementMonth =
        VALUE ( MID ( CurrentMovement, 5, 2 ) )
    VAR PeriodYear =
        VALUE ( LEFT ( CurrentPeriod, 4 ) )
    VAR PeriodMonth =
        VALUE ( RIGHT ( CurrentPeriod, 2 ) )
    RETURN
        IF (
            CurrentMovement <> BLANK ()
                && CurrentMovement <> -1,
            ( MovementYear - PeriodYear ) * 12 + MovementMonth - PeriodMonth
        )