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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request 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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors