Power Query to calculate duration

samahiji

Board Regular
Joined
Oct 6, 2015
Messages
82
Office Version
  1. 2019
Platform
  1. Windows
Hello
I have the following table and need to calculate the duration among users for each status changed (Open>Closed):

USERTIMESTATUS
Tony
04/05/2023 08:45​
Opened
Mary
04/05/2023 08:55​
Opened
Tony
04/05/2023 09:24​
Closed
Mary
04/05/2023 14:06​
Closed
Alan
04/05/2023 16:07​
Opened
Alan
04/05/2023 17:10​
Closed
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
See if this suits your need. Note "Table1" will need to be updated to match that of your table name.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"USER", type text}, {"TIME", type datetime}, {"STATUS", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"USER", Order.Ascending}, {"TIME", Order.Ascending}}),
    #"Filtered Rows - STATUS Opened" = Table.SelectRows(#"Sorted Rows", each ([STATUS] = "Opened")),
    #"Added Opened Index" = Table.AddIndexColumn(#"Filtered Rows - STATUS Opened", "Opened Index", 1, 1, type text),
    #"Renamed Column - Opened Time" = Table.RenameColumns(#"Added Opened Index",{{"TIME", "Opened Time"}}),
    #"Filtered Rows - STATUS Closed" = Table.SelectRows(#"Sorted Rows", each ([STATUS] = "Closed")),
    #"Added Closed Index" = Table.AddIndexColumn(#"Filtered Rows - STATUS Closed", "Closed Index", 1, 1, type text),
    #"Renamed Column - Closed Time" = Table.RenameColumns(#"Added Closed Index",{{"TIME", "Closed Time"}}),
    #"Merged Queries" = Table.NestedJoin(#"Renamed Column - Opened Time", {"Opened Index"}, #"Renamed Column - Closed Time", {"Closed Index"}, "Renamed Column - Closed Time", JoinKind.LeftOuter),
    #"Expanded Renamed Column - Closed Time" = Table.ExpandTableColumn(#"Merged Queries", "Renamed Column - Closed Time", {"Closed Time"}, {"Closed Time"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Renamed Column - Closed Time",{"STATUS", "Opened Index"}),
    #"Added Column - Duration" = Table.AddColumn(#"Removed Columns", "Duration", each [Closed Time] - [Opened Time], type duration)
in
    #"Added Column - Duration"
 
Upvote 0
Sorry,
when tested in real data, the query is giving wrong result!!
the issue with adding index maybe.
 
Upvote 0

Forum statistics

Threads
1,223,355
Messages
6,171,608
Members
452,411
Latest member
sprichwort

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top