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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Calculate duration from latest Date / time and Earlier date/time with multiple condition

Hello Community,

 

            I would like to calculate rest time for Driver   condition is rest time must be equal to or more than 10 hours.

 

but the issue is a data structure I retrieve from Database  here is an example. 

JJ8098_0-1696845497798.png

 

D_NAMEMT_NAMEVLCTL_GPS_DATETime
MR.JOHN CONNORCard Swipe In09/10/202308:26:39
MR.JOHN CONNORCard Swipe In09/10/202309:54:44
MR.JOHN CONNORCard Swipe Out09/10/202309:04:35
MR.JOHN CONNORCard Swipe Out09/10/202309:56:44
MR.JOHN CONNORCard Swipe In07/10/202306:21:55
MR.JOHN CONNORCard Swipe In07/10/202306:41:15
MR.JOHN CONNORCard Swipe In07/10/202307:51:34
MR.JOHN CONNORCard Swipe In07/10/202313:54:04
MR.JOHN CONNORCard Swipe In07/10/202318:01:18
MR.JOHN CONNORCard Swipe Out07/10/202306:33:30
MR.JOHN CONNORCard Swipe Out07/10/202306:46:23
MR.JOHN CONNORCard Swipe Out07/10/202308:53:29
MR.JOHN CONNORCard Swipe Out07/10/202308:53:30
MR.JOHN CONNORCard Swipe Out07/10/202314:00:05
MR.JOHN CONNORCard Swipe Out07/10/202318:34:30
MR.JOHN CONNORCard Swipe In06/10/202312:47:09
MR.JOHN CONNORCard Swipe In06/10/202312:56:38
MR.JOHN CONNORCard Swipe In06/10/202313:17:25
MR.JOHN CONNORCard Swipe Out06/10/202312:49:46
MR.JOHN CONNORCard Swipe Out06/10/202313:02:00
MR.JOHN CONNORCard Swipe Out06/10/202313:20:22

 

As you can see there are many Swipe in and out in one day.

The conditoon to select what date and time for calculation is

 

1) lookup date and time  the latest "Card Swipe Out"  (Highlighted) on previous day and earliest date and time for "Card swipe In". date and time between Swipe In and Swipe out must be differ. Swipe out should be  Swipe In Date - 1 or 2, 3 .....
     [ex. John lastest data on 09/10/2023 the first "Card swipe In" at 08:26   and the lastest correct "Card swipe out" is on 07/10/2023 18:34  must exclude "Card swipe out" on 09/10/2023 because it come from he turn off the truck.]

 

2) compare 2 times  in hours format   
3) If 2 time different more than 10 hours  display " ENOUGH REST"  if not "NOT ENOUGH REST"

expect result 

D_NAMELatestCardSwipeInLatestCardSwipeOutRest Duration (HRS)RestStatus
MR.JOHN CONNOR09/10/2023 08:2607/10/2023 18:3437ENOUGH REST

JJ8098_1-1696845545796.png

 

I have attach pbix file for ref.

EXAMPLE 

 

is it possible to complete this in power BI or power query ?

 

 

 

Thank you

Best Regards,

JJ

1 ACCEPTED SOLUTION
mickey64
Super User
Super User

For your reference.

 

Step 1: I add a column 'Merged' and rename to 'Date_Time' in Power Query Editor.

mickey64_1-1696852767555.png

mickey64_2-1696853009507.png

Step 2: I make 3 measures below in Power BI Desktop.

    Latest In Date_Time = MAXX(FILTER('DATA','DATA'[MT_NAME]="Card Swipe In"),'DATA'[Date_Time])

    Latest Out Date_Time = MAXX(FILTER('DATA','DATA'[MT_NAME]="Card Swipe Out"&&'DATA'[VLCTL_GPS_DATE]<MAX('DATA'[VLCTL_GPS_DATE])),'DATA'[Date_Time])

    Duration Time hrs = DATEDIFF([Latest Out Date_Time],[Latest In Date_Time],HOUR)
 
Step 3: I make a matrix below.
mickey64_3-1696853211067.png

 

 

 

View solution in original post

4 REPLIES 4
mickey64
Super User
Super User

For your reference.

 

Step 1: I add a column 'Merged' and rename to 'Date_Time' in Power Query Editor.

mickey64_1-1696852767555.png

mickey64_2-1696853009507.png

Step 2: I make 3 measures below in Power BI Desktop.

    Latest In Date_Time = MAXX(FILTER('DATA','DATA'[MT_NAME]="Card Swipe In"),'DATA'[Date_Time])

    Latest Out Date_Time = MAXX(FILTER('DATA','DATA'[MT_NAME]="Card Swipe Out"&&'DATA'[VLCTL_GPS_DATE]<MAX('DATA'[VLCTL_GPS_DATE])),'DATA'[Date_Time])

    Duration Time hrs = DATEDIFF([Latest Out Date_Time],[Latest In Date_Time],HOUR)
 
Step 3: I make a matrix below.
mickey64_3-1696853211067.png

 

 

 

Anonymous
Not applicable

I follow your step and it work. 

 

for Rest time condition 


i used : 

IF([Duration Time] >= TIME(10, 0, 0), "Sufficient Rest Time", "Insufficient Rest Time")

the result is all become "insufficient Rest time"  so i change to 

IF([Duration Time] >= 10, "Sufficient Rest Time", "Insufficient Rest Time")

and it work.

mh2587
Super User
Super User

//Create Calcualeted Columns
Rest Time = [End Time] - [Start Time]
Rest Time Condition = IF([Rest Time] >= TIME(10, 0, 0), "Sufficient Rest Time", "Insufficient Rest Time")

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



Anonymous
Not applicable

I cant create Calculated columns 
when i create this 
Rest Time = [Latest Out] - [Latest In]   
Measure Latest Out = 

MAXX(FILTER('VEHICLE_LOCATION_LOG_2023','VEHICLE_LOCATION_LOG_2023'[MT_NAME]="Card Swipe Out"&&'VEHICLE_LOCATION_LOG_2023'[VLCTL_GPS_DATE]<MAX('VEHICLE_LOCATION_LOG_2023'[VLCTL_GPS_DATE])),'VEHICLE_LOCATION_LOG_2023'[Date_Time])


Lastest In = 

MAXX(FILTER('VEHICLE_LOCATION_LOG_2023','VEHICLE_LOCATION_LOG_2023'[MT_NAME]="Card Swipe In"),'VEHICLE_LOCATION_LOG_2023'[Date_Time])


error:A circular dependency was detected:

anyway i find the result  thank you.

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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
Top Kudoed Authors