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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Calculate working time between 2 timestamp in difference day

Hello, 

I'm newbie here please let me know if i miss anything.

 

I want to calculate working time (minute) between 2 timestamps

but not count time if it not in working hour (9.00 - 18.00)

the data have both finish in same day and finish next day.

 

If Time A is over 18.00, not count and let start counting on next day till time B

or if Time A start before 9.00, not count and let start counting on 9.00 onward till time B

or if Time A and Time B are in different day, need it to count till 18.00 and break then start counting next day 9.00

 

Time ATime BProcessing time (Min)
27-06-22 16:3228-06-22 9:28 
27-06-22 16:3228-06-22 11:28 
27-06-22 16:3228-06-22 0:35 
28-06-22 15:1329-06-22 14:52 
30-06-22 11:4830-06-22 14:10 
01-07-22 13:2301-07-22 13:47 
01-07-22 18:1101-07-22 15:47 

 

I try to use IF and DATEDIFF function in many ways but still can't calculate it correctly 

Very appreciate if someone can help

Thank you

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

I suggest you to try this code to create a calcualted column.

Processing time (Min) = 
VAR _DAYDIFF =
    DATEDIFF ( 'Table'[Time A], 'Table'[Time B], DAY )
VAR _REST_MIN = ( 6 + 9 ) * 60
VAR _STARTDATE =
    IF (
        TIMEVALUE ( 'Table'[Time A] ) < TIME ( 9, 0, 0 ),
        DATEVALUE ( 'Table'[Time A] ) + TIME ( 9, 0, 0 ),
        IF (
            TIMEVALUE ( 'Table'[Time A] ) > TIME ( 18, 0, 0 ),
            DATEVALUE ( 'Table'[Time A] ) + TIME ( 18, 0, 0 ),
            'Table'[Time A]
        )
    )
VAR _ENDDATE =
    IF (
        TIMEVALUE ( 'Table'[Time B] ) < TIME ( 9, 0, 0 ),
        DATEVALUE ( 'Table'[Time B] ) + TIME ( 9, 0, 0 ),
        IF (
            TIMEVALUE ( 'Table'[Time B] ) > TIME ( 18, 0, 0 ),
            DATEVALUE ( 'Table'[Time B] ) + TIME ( 18, 0, 0 ),
            'Table'[Time B]
        )
    )
VAR _PROCESSTIME1 =
    DATEDIFF ( _STARTDATE, _ENDDATE, MINUTE )
RETURN
    _PROCESSTIME1 - _REST_MIN * _DAYDIFF

Result is as below.

RicoZhou_0-1664433697179.png

 

Best Regards,
Rico Zhou

 

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

2 REPLIES 2
Anonymous
Not applicable

Hi @Anonymous 

It worked totally Perfect!

I'm really appreciated and Thank you very much your help Master.

Anonymous
Not applicable

Hi @Anonymous ,

 

I suggest you to try this code to create a calcualted column.

Processing time (Min) = 
VAR _DAYDIFF =
    DATEDIFF ( 'Table'[Time A], 'Table'[Time B], DAY )
VAR _REST_MIN = ( 6 + 9 ) * 60
VAR _STARTDATE =
    IF (
        TIMEVALUE ( 'Table'[Time A] ) < TIME ( 9, 0, 0 ),
        DATEVALUE ( 'Table'[Time A] ) + TIME ( 9, 0, 0 ),
        IF (
            TIMEVALUE ( 'Table'[Time A] ) > TIME ( 18, 0, 0 ),
            DATEVALUE ( 'Table'[Time A] ) + TIME ( 18, 0, 0 ),
            'Table'[Time A]
        )
    )
VAR _ENDDATE =
    IF (
        TIMEVALUE ( 'Table'[Time B] ) < TIME ( 9, 0, 0 ),
        DATEVALUE ( 'Table'[Time B] ) + TIME ( 9, 0, 0 ),
        IF (
            TIMEVALUE ( 'Table'[Time B] ) > TIME ( 18, 0, 0 ),
            DATEVALUE ( 'Table'[Time B] ) + TIME ( 18, 0, 0 ),
            'Table'[Time B]
        )
    )
VAR _PROCESSTIME1 =
    DATEDIFF ( _STARTDATE, _ENDDATE, MINUTE )
RETURN
    _PROCESSTIME1 - _REST_MIN * _DAYDIFF

Result is as below.

RicoZhou_0-1664433697179.png

 

Best Regards,
Rico Zhou

 

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

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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