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! Learn more

Reply
Anonymous
Not applicable

Calculating difference between dates

Hello, I need to calculate the difference between dates if certain conditions are met, sample data:

IDCategoryHCDate
1A001/01/2019
2A101/01/2019
3B101/01/2019
1A015/01/2019
2A015/01/2019
3B115/01/2019
1A101/02/2019
2A101/02/2019
3B1

01/02/2019

 

ID has to be the same and the condition is if HC is 0 then i need to calculate the difference in dates until its 1 for the same ID, this condition can happen multiple times over the year and I need to calculate the difference in every ocation

1 ACCEPTED SOLUTION
v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can use the following measure to meet your requirement.

 

Days Until 1 = 
VAR thisdate =
    MAX ( 'Table'[Date] )
VAR thisid =
    MAX ( 'Table'[ID] )
VAR thisHCvalue =
    MIN ( 'Table'[HC] )
VAR next1date =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER(ALLSELECTED ( 'Table' ),
        'Table'[ID]=thisid && 'Table'[HC]=0
    ))
RETURN
IF (
        OR ( ISBLANK ( next1date ), thisHCvalue <> 0 ),
        0,
        DATEDIFF ( thisdate, next1date, DAY )
    )

 

The result like this,

 

Cal 1.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

It will be helpful if you can show us the exact expected result.

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

How about the result after you follow the suggestions mentioned in my original post?

Could you please provide more details or expected result about it If it doesn't meet your requirement?

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can use the following measure to meet your requirement.

 

Days Until 1 = 
VAR thisdate =
    MAX ( 'Table'[Date] )
VAR thisid =
    MAX ( 'Table'[ID] )
VAR thisHCvalue =
    MIN ( 'Table'[HC] )
VAR next1date =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER(ALLSELECTED ( 'Table' ),
        'Table'[ID]=thisid && 'Table'[HC]=0
    ))
RETURN
IF (
        OR ( ISBLANK ( next1date ), thisHCvalue <> 0 ),
        0,
        DATEDIFF ( thisdate, next1date, DAY )
    )

 

The result like this,

 

Cal 1.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

It will be helpful if you can show us the exact expected result.

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

mahoneypat
Microsoft Employee
Microsoft Employee

Please try this measure expression in a table with Date and ID columns.  It will get the shown result.

 

Days Until 1 =
VAR thisdate =
SELECTEDVALUE ( HC[Date] )
VAR thisid =
SELECTEDVALUE ( HC[ID] )
VAR thisHCvalue =
MIN ( HC[HC] )
VAR next1date =
CALCULATE (
MAX ( HC[Date] ),
ALL ( HC ),
VALUES ( HC[ID] ),
HC[Date] > thisdate,
HC[HC] = 1
)
RETURN
IF (
OR ( ISBLANK ( next1date ), thisHCvalue <> 0 ),
0,
DATEDIFF ( thisdate, next1date, DAY )
)
 
HC.png

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

its not working for my i only get null values

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