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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Qotsa
Helper V
Helper V

Hours and minutes between last shift of day and first shift of next day

Hi,

 

How do you calculate for each carer_id, the difference in hours and minutes between their last shift & the start of their next shift on the following day?

 

Below, carer_id 78315, their last shift on 18/11/2021 is 20:40

&

their first shift the next day 19//11/2021 is 08:00

so result should be 11hrs 20 mins.

 

Qotsa_0-1639393333863.png

 

 

1 ACCEPTED SOLUTION

Hi, @Qotsa 

 

Duration =
VAR n1 =
    CALCULATE (
        MIN ( 'Table'[Sched_Start] ),
        FILTER ( 'Table', [date] = EARLIER ( 'Table'[date] ) )
    )
VAR n2 = [date] - 1
VAR n3 =
    CALCULATE (
        MAX ( 'Table'[Sched_Finsh] ),
        FILTER ( 'Table', [date] = EARLIER ( 'Table'[date2] ) )
    )
RETURN
    IF (
        n3 = BLANK (),
        BLANK (),
        IF ( [Sched_Start] = n1, ( [date] + n1 ) - ( n2 + n3 ), BLANK () )
    )

vzhangti_1-1639706508882.png

 

Best Regards,

Community Support Team _Charlotte

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

5 REPLIES 5
v-zhangti
Community Support
Community Support

Hi, @Qotsa 

 

Your problem can be solved by adding several calculation columns. You can try the following methods.

MIN =
CALCULATE (
    MIN ( 'Table'[Sched_Start] ),
    FILTER ( 'Table', [date] = EARLIER ( 'Table'[date] ) )
)
date2 = [date]-1
MAX =
CALCULATE (
    MAX ( 'Table'[Sched_Finsh] ),
    FILTER ( 'Table', [date] = EARLIER ( 'Table'[date2] ) )
)
Duration =
IF ( [MAX] = BLANK (), BLANK (), ( [date] + [MIN] ) - ( [date2] + [MAX] ) )

vzhangti_0-1639647241734.png

The calculation column is the decomposition step, which is convenient for you to understand.

 

It can be solved directly by this formula. There is no need to add a calculated column.

Duration2 = 
VAR n1 =
    CALCULATE (
        MIN ( 'Table'[Sched_Start] ),
        FILTER ( 'Table', [date] = EARLIER ( 'Table'[date] ) )
    )
VAR n2 = [date] - 1
VAR n3 =
    CALCULATE (
        MAX ( 'Table'[Sched_Finsh] ),
        FILTER ( 'Table', [date] = EARLIER ( 'Table'[date2] ) )
    )
RETURN
    IF ( n3 = BLANK (), BLANK (), ( [date] + n1 ) - ( n2 + n3 ) )

vzhangti_1-1639647371971.png

 

Best Regards,

Community Support Team _Charlotte

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

@v-zhangti Thanks very much, that works. How could I remove the repeating times so it only shows the duration once ?

Qotsa_0-1639655553911.png

 

Hi, @Qotsa 

 

Duration =
VAR n1 =
    CALCULATE (
        MIN ( 'Table'[Sched_Start] ),
        FILTER ( 'Table', [date] = EARLIER ( 'Table'[date] ) )
    )
VAR n2 = [date] - 1
VAR n3 =
    CALCULATE (
        MAX ( 'Table'[Sched_Finsh] ),
        FILTER ( 'Table', [date] = EARLIER ( 'Table'[date2] ) )
    )
RETURN
    IF (
        n3 = BLANK (),
        BLANK (),
        IF ( [Sched_Start] = n1, ( [date] + n1 ) - ( n2 + n3 ), BLANK () )
    )

vzhangti_1-1639706508882.png

 

Best Regards,

Community Support Team _Charlotte

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

Fantastic. Thanks again.

amitchandak
Super User
Super User

@Qotsa , a new column  

 

var _datetime = [Date]+ [Sched_finish]
var _max = maxx(filter(Table, [Carter_id] = earlier([Carter_id]) && _datetime <[Date] +[Sched_start]),[Date]+ [Sched_finish])
return
[Date] +[Sched_start] - _max

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.