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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
hxnwx
Frequent Visitor

SQL DATEDIFF with lookup

Hello.

 

I'm trying to do a time duration here based on a lookup.

I want to find duration that Column A > x to value Column A NOT > x.

For example:

Column A > x at 12/12/21 09:00.

Column A became NOT > x at 12/12/21 09:43

Duration would be the difference.  I know this is a dumb question but I assume the lookup or Select moves forward in time?  Never backwards?

I would expect a result of list of duration values.

Thanks for any help.

 

2 ACCEPTED SOLUTIONS
v-yalanwu-msft
Community Support
Community Support

Hi, @hxnwx ,

You could create a measure as follow:

Measure = 
var _next=CALCULATE(MAX('Table'[DATE/TIME]),FILTER(ALL('Table'),[A]=MAX([A])&&[Status]="No"))
var _dura= DATEDIFF(MAX('Table'[DATE/TIME]), _next,SECOND)
return  RIGHT ( "0" & INT ( _dura / 3600 ), 2 )
    & ":"
    & RIGHT (
        "0"
            & INT ( ( _dura - INT (_dura / 3600 ) * 3600 ) / 60 ),
        2
    )
    & ":"
    & RIGHT ( "0" & MOD (_dura, 60 ), 2 )

The final show:

vyalanwumsft_0-1657519662624.png

 

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community


Best Regards,
Community Support Team _ Yalan Wu
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

v-yalanwu-msft
Community Support
Community Support

Hi, @hxnwx 

Try it.

Measure = 
var _pre=CALCULATE(MIN('Table'[DATE/TIME]),FILTER(ALL('Table'),[DATE/TIME]<MAX('Table'[DATE/TIME])&&[lo_ifr_ind]=1))
var _dur= DATEDIFF(_pre,MAX('Table'[DATE/TIME]),SECOND)
var _dura=IF(MAX('Table'[lo_ifr_ind])= CALCULATE(MAX('Table'[lo_ifr_ind]),FILTER(ALL('Table'),[DATE/TIME]= CALCULATE(MAX('Table'[DATE/TIME]),FILTER(ALL('Table'),[DATE/TIME]<MAX('Table'[DATE/TIME]))))),"00:00:00",_dur)
return 
 RIGHT ( "0" & INT ( _dura / 3600 ), 2 )
    & ":"
    & RIGHT (
        "0"
            & INT ( ( _dura - INT (_dura / 3600 ) * 3600 ) / 60 ),
        2
    )
    & ":"
    & RIGHT ( "0" & MOD (_dura, 60 ), 2 )

The final show:

vyalanwumsft_0-1657698922328.png


Best Regards,
Community Support Team _ Yalan Wu
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-yalanwu-msft
Community Support
Community Support

Hi, @hxnwx 

Try it.

Measure = 
var _pre=CALCULATE(MIN('Table'[DATE/TIME]),FILTER(ALL('Table'),[DATE/TIME]<MAX('Table'[DATE/TIME])&&[lo_ifr_ind]=1))
var _dur= DATEDIFF(_pre,MAX('Table'[DATE/TIME]),SECOND)
var _dura=IF(MAX('Table'[lo_ifr_ind])= CALCULATE(MAX('Table'[lo_ifr_ind]),FILTER(ALL('Table'),[DATE/TIME]= CALCULATE(MAX('Table'[DATE/TIME]),FILTER(ALL('Table'),[DATE/TIME]<MAX('Table'[DATE/TIME]))))),"00:00:00",_dur)
return 
 RIGHT ( "0" & INT ( _dura / 3600 ), 2 )
    & ":"
    & RIGHT (
        "0"
            & INT ( ( _dura - INT (_dura / 3600 ) * 3600 ) / 60 ),
        2
    )
    & ":"
    & RIGHT ( "0" & MOD (_dura, 60 ), 2 )

The final show:

vyalanwumsft_0-1657698922328.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

hxnwx
Frequent Visitor

Hi Yalan Wu,

 

I think this is close though I might have the column names incorrect in Measure.  

I get the following response.  I can see the Duration column(Measure). 

I would expect to see a value in this column on the 6/5/2022 1:42:00 PM row. 

This is where column 'lo_ifr_ind' changed from 1 to 0.  The duration it was = 1, should be from:

6/5/2022 10:39:00 AM (changed from 0 to 1)

6/5/2022 1:42:00 PM (changed from 1 back to 0)

So duration would be close to 03:03:00.

 

 hxnwx_0-1657562857504.png

  

Here is what I have for the Measure:

Duration = var _next=CALCULATE(MAX('Query1'[wthr_prd_iss_gts]),FILTER(ALL('Query1'),[wthr_prd_iss_gts]=MAX([wthr_prd_iss_gts])&&[lo_ifr_ind]=0)) var _dura= DATEDIFF(MAX('Query1'[wthr_prd_iss_gts]), _next,SECOND) return RIGHT ( "0" & INT ( _dura / 3600 ), 2 ) & ":" & RIGHT ( "0" & INT ( ( _dura - INT (_dura / 3600 ) * 3600 ) / 60 ), 2 ) & ":" & RIGHT ( "0" & MOD (_dura, 60 ), 2 ) 

 

 

 

 

 

v-yalanwu-msft
Community Support
Community Support

Hi, @hxnwx ,

You could create a measure as follow:

Measure = 
var _next=CALCULATE(MAX('Table'[DATE/TIME]),FILTER(ALL('Table'),[A]=MAX([A])&&[Status]="No"))
var _dura= DATEDIFF(MAX('Table'[DATE/TIME]), _next,SECOND)
return  RIGHT ( "0" & INT ( _dura / 3600 ), 2 )
    & ":"
    & RIGHT (
        "0"
            & INT ( ( _dura - INT (_dura / 3600 ) * 3600 ) / 60 ),
        2
    )
    & ":"
    & RIGHT ( "0" & MOD (_dura, 60 ), 2 )

The final show:

vyalanwumsft_0-1657519662624.png

 

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks Yalan.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors