Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Durbslaw
Helper I
Helper I

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 not blank or -1.

 

I have...

 

NA DateDiff = IF(NOT('MovementSnapshot'[AppointmentDateKey])= -1 && NOT(ISBLANK('MovementSnapshot'[AppointmentDateKey])) ,DATEDIFF(SELECTEDVALUE('Period'[YearMonth]),MAX('MovementSnapshot'[AppointmentDatekey]),MONTH))
 
2 ACCEPTED SOLUTIONS
tamerj1
Super User
Super User

@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
)

 

View solution in original post

@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
    )

 

View solution in original post

11 REPLIES 11
tamerj1
Super User
Super User

@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
)

 

syntax error...

New measure syntax error.PNG

@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
    )

 

PC2790
Community Champion
Community Champion

Hey @Durbslaw ,

 

Is it not giving you right result?

What is your end requirement?

errorerror

PC2790
Community Champion
Community Champion

This will not work as a measure. Try having a calculated column. Something like:

Column = if(Not(ISBLANK('MovementSnapshot'[AppointmentDateKey])) || 'MovementSnapshot'[AppointmentDateKey] <> -1,DATEDIFF('Period'[YearMonth],'MovementSnapshot'[AppointmentDateKey],MONTH))
 
Also, another thing to make sure is the two values you are using are of date datatype.Otherwise it won't work like this.

@Durbslaw 

The location of the closing bracket of not should be after the "1". But even though I don't believe this is a correct code. Please provide more details about your data. 

tamerj1
Super User
Super User

@Durbslaw 

How does the data in each column look like?

YearMonth.PNG

Hi @Durbslaw 
Please use the following formula

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

Aplogies for the non clarity, I need a measure as I am using SSAS as a source and cannot create columns.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.