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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Rochejf
Frequent Visitor

Sum of Time differences by two condition

Rochejf_0-1686299380894.png

 

Hi I really need some help here.

 

Referring to the image attached, I want to find the time difference in a logic of column TRANSACTION_STATUS(7-4). Probably this can be done in way with the latest timemark minus the second latest timemark then continue the loop. 

 

After that, all the time difference will be sum under BATCH_RUN_STEP_ID 12.

 

Thanks

1 ACCEPTED SOLUTION
Rochejf
Frequent Visitor

Thanks! 
The code works out perfectly.

However, after i swapped it to powerbi loaded database, it shows token identifier expected. How should i amend the code?

 

let
    Source = BATCH_RUN_STEP_LABOR_TRANSACTION, let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, TRANSACATION_DATE_TIME = _t, TRANSACTION_STATUS = _t, BATCH_RUN_STEP_LABOR_ID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source, {
        {"ID", Int64.Type},
        {"TRANSACATION_DATE_TIME", type datetime},
        {"TRANSACTION_STATUS", Int64.Type},
        {"BATCH_RUN_STEP_LABOR_ID", Int64.Type}
    }, "es"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Time Spent", each
        if [TRANSACTION_STATUS] = 7 then
            [TRANSACATION_DATE_TIME] -
            Table.Sort(
                Table.SelectRows(
                    #"Changed Type",
                    (k) => k[TRANSACTION_STATUS] = 4 and k[TRANSACATION_DATE_TIME] < [TRANSACATION_DATE_TIME]
                ),
                {{"TRANSACATION_DATE_TIME", Order.Descending}}
            ){0}[TRANSACATION_DATE_TIME]
        else null,
        type duration
    ),
    #"Grouped Rows" = ""
in
    #"Grouped Rows"

 

Hope you can assist me again on this manner

View solution in original post

6 REPLIES 6
Rochejf
Frequent Visitor

Thanks! 
The code works out perfectly.

However, after i swapped it to powerbi loaded database, it shows token identifier expected. How should i amend the code?

 

let
    Source = BATCH_RUN_STEP_LABOR_TRANSACTION, let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, TRANSACATION_DATE_TIME = _t, TRANSACTION_STATUS = _t, BATCH_RUN_STEP_LABOR_ID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source, {
        {"ID", Int64.Type},
        {"TRANSACATION_DATE_TIME", type datetime},
        {"TRANSACTION_STATUS", Int64.Type},
        {"BATCH_RUN_STEP_LABOR_ID", Int64.Type}
    }, "es"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Time Spent", each
        if [TRANSACTION_STATUS] = 7 then
            [TRANSACATION_DATE_TIME] -
            Table.Sort(
                Table.SelectRows(
                    #"Changed Type",
                    (k) => k[TRANSACTION_STATUS] = 4 and k[TRANSACATION_DATE_TIME] < [TRANSACATION_DATE_TIME]
                ),
                {{"TRANSACATION_DATE_TIME", Order.Descending}}
            ){0}[TRANSACATION_DATE_TIME]
        else null,
        type duration
    ),
    #"Grouped Rows" = ""
in
    #"Grouped Rows"

 

Hope you can assist me again on this manner

your #"Grouped Rows"  step is empty/missing.

Thanks!

lbendlin
Super User
Super User

Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot).
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Please show the expected outcome based on the sample data you provided.

https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Sorry for the inconvenience, let me try this again.

 

IDTRANSACATION_DATE_TIMETRANSACTION_STATUSBATCH_RUN_STEP_LABOR_ID
226-05-22 13:18:55412
426-05-22 13:19:43412
626-05-22 13:19:57412
326-05-22 13:19:41712
526-05-22 13:19:48712
726-05-22 13:19:58712
4218518-01-23 15:28:38712903
4217018-01-23 12:59:01412903

According to the table attached, there are 4 sets of running times under 2 groups, group 1 and group 2.

I wish to obtain the time difference and the sum separate by groups. However, the time recorded may not always be in sequence. Therefore, I wish there is a method to perform the calculation starting with the latest IN and OUT time in each group the follows.

The formula for the ideal outcome would be:

GROUP 1 = sum((ID7-ID6)+(ID5-ID4)+(ID3-ID2)) = 52 sec GROUP 2 = ID42185-ID42170 = 8977 sec

 

The sample table:

Time SpentGROUP
521
89772

 

Really hope to solve this thanks!

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc5hCoAwCAXgq8R+N/DpbM6rjO5/jVZQYe2HII+Pp70nTmviLZNm5gXiMFcdWRkDTvvarzWQ5kUi2f5EayQyacHI6kt0QiySOjn0IYVhZxMsEzLLAnU2l5c1kltWCpJdmxOezy+5Hw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, TRANSACATION_DATE_TIME = _t, TRANSACTION_STATUS = _t, BATCH_RUN_STEP_LABOR_ID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"TRANSACATION_DATE_TIME", type datetime}, {"TRANSACTION_STATUS", Int64.Type}, {"BATCH_RUN_STEP_LABOR_ID", Int64.Type}},"es"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Time Spent", each if [TRANSACTION_STATUS]=7 then [TRANSACATION_DATE_TIME]-Table.Sort(Table.SelectRows(#"Changed Type",(k)=>k[TRANSACTION_STATUS]=4 and k[TRANSACATION_DATE_TIME]<[TRANSACATION_DATE_TIME]),{{"TRANSACATION_DATE_TIME",Order.Descending}}){0}[TRANSACATION_DATE_TIME] else null,type duration),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"BATCH_RUN_STEP_LABOR_ID"}, {{"Time Spent", each List.Sum([Time Spent]), type duration}})
in
    #"Grouped Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

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