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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Arne84
Frequent Visitor

Calculate the time difference from Work

Hi, i have some Data where the employee checks in and out. it is possible that he has a night shift. How can I calculate the time difference between come and go?

 

Thank you in advance.

 

DateIDTimeStatus
29.06.20221234522:20:19leave
28.06.20221234518:17:23come
23.06.20221234515:21:14leave
23.06.20221234507:50:03come
22.06.20221234514:12:56leave
22.06.20221234507:48:51come
14.06.20221234515:12:07leave
14.06.20221234508:45:07come
13.06.20221234515:26:06leave
13.06.20221234508:48:38come
09.06.20221234516:27:38leave
09.06.20221234508:41:33come
1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Below is PQ solution. See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdExDsMgDIXhuzBHCn5gcN5VogxRla1Vt56/tJHaBGfCw6dfBuY5lBHTiAiEIQhS1nYCRKRMbbxv62sLy/CF1kMxSiVSG2/Px88l55QQSu6DDlZqZOx7cL1MAbX0PQcrs1Hl3JN8sV/rxdr1PDRm3d2xd3nfwtjv56F99kt27rn/kELUnR1zzrWaMP1fb3kD", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, ID = _t, Time = _t, Status = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ID", Int64.Type}, {"Time", type time}, {"Status", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Status", Order.Ascending},{"Date", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Calculated Modulo" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, Table.RowCount(#"Added Index")/2), type number}}),
    Leave = Table.SelectRows(#"Calculated Modulo", each ([Status] = "leave")),
    Come = Table.SelectRows(#"Calculated Modulo", each ([Status] = "come")),
    #"Renamed Columns" = Table.RenameColumns(Come,{{"Time", "Incoming Time"}, {"Date", "Incoming Date"}}),
    #"Merged Queries" = Table.NestedJoin(#"Renamed Columns", {"Index"}, Leave, {"Index"}, "Leave", JoinKind.LeftOuter),
    #"Expanded Leave" = Table.ExpandTableColumn(#"Merged Queries", "Leave", {"Date", "Time"}, {"Outgoing Date", "Outgoing Time"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Leave",{"Index", "Status"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"ID", "Incoming Date", "Incoming Time", "Outgoing Date", "Outgoing Time"}),
    #"Added Custom" = Table.AddColumn(#"Reordered Columns", "Time Diff in Hours", each Duration.TotalHours([Outgoing Date]&[Outgoing Time]-([Incoming Date]&[Incoming Time])))
in
    #"Added Custom"

 

View solution in original post

3 REPLIES 3
Vijay_A_Verma
Super User
Super User

Below is PQ solution. See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdExDsMgDIXhuzBHCn5gcN5VogxRla1Vt56/tJHaBGfCw6dfBuY5lBHTiAiEIQhS1nYCRKRMbbxv62sLy/CF1kMxSiVSG2/Px88l55QQSu6DDlZqZOx7cL1MAbX0PQcrs1Hl3JN8sV/rxdr1PDRm3d2xd3nfwtjv56F99kt27rn/kELUnR1zzrWaMP1fb3kD", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, ID = _t, Time = _t, Status = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ID", Int64.Type}, {"Time", type time}, {"Status", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Status", Order.Ascending},{"Date", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Calculated Modulo" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, Table.RowCount(#"Added Index")/2), type number}}),
    Leave = Table.SelectRows(#"Calculated Modulo", each ([Status] = "leave")),
    Come = Table.SelectRows(#"Calculated Modulo", each ([Status] = "come")),
    #"Renamed Columns" = Table.RenameColumns(Come,{{"Time", "Incoming Time"}, {"Date", "Incoming Date"}}),
    #"Merged Queries" = Table.NestedJoin(#"Renamed Columns", {"Index"}, Leave, {"Index"}, "Leave", JoinKind.LeftOuter),
    #"Expanded Leave" = Table.ExpandTableColumn(#"Merged Queries", "Leave", {"Date", "Time"}, {"Outgoing Date", "Outgoing Time"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Leave",{"Index", "Status"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"ID", "Incoming Date", "Incoming Time", "Outgoing Date", "Outgoing Time"}),
    #"Added Custom" = Table.AddColumn(#"Reordered Columns", "Time Diff in Hours", each Duration.TotalHours([Outgoing Date]&[Outgoing Time]-([Incoming Date]&[Incoming Time])))
in
    #"Added Custom"

 

PurpleGate
Resolver III
Resolver III

And if you only want to see how long they have been at the office

 

Time at office = IF (
        'Table'[status] = "leave",
        VAR leave =
            CALCULATE (
                MAX ( 'Table'[DAteTime] ),
                ALLEXCEPT ( 'Table', 'Table'[ID] ),
                'Table'[DAteTime] < EARLIER ( 'Table'[DAteTime] )
            )
        RETURN
            IF ( NOT ISBLANK ( leave ), FORMAT ( 'Table'[DAteTime] - leave, "HH:MM" ) )
    )

PurpleGate_2-1656682085661.png

 

PurpleGate
Resolver III
Resolver III

Hi,

 

1. In Power Query I made a DateTimestamp

 

Custom Column > Date and Time (note that I had them both as text fields when I joined them. Afterwards I changed it to DateTimeStamp)

=[Date]&" "&[Time]

 

PurpleGate_0-1656681874276.png

 

2. I added a calculated column

 

Time Between =
IF (
    'Table'[status] = "come",
    VAR come =
        CALCULATE (
            MAX ( 'Table'[DAteTime] ),
            ALLEXCEPT ( 'Table', 'Table'[ID] ),
            'Table'[DAteTime] < EARLIER ( 'Table'[DAteTime] )
        )
    RETURN
        IF ( NOT ISBLANK ( come ), FORMAT ( 'Table'[DAteTime] - come, "HH:MM" ) ),
    IF (
        'Table'[status] = "leave",
        VAR leave =
            CALCULATE (
                MAX ( 'Table'[DAteTime] ),
                ALLEXCEPT ( 'Table', 'Table'[ID] ),
                'Table'[DAteTime] < EARLIER ( 'Table'[DAteTime] )
            )
        RETURN
            IF ( NOT ISBLANK ( leave ), FORMAT ( 'Table'[DAteTime] - leave, "HH:MM" ) )
    )
)

PurpleGate_1-1656681981104.png

 

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors