Forum Discussion
Adding custom column in Power Query
- 5 years ago
Try this code, I have broken it into a few columns to try and help make it more clear what I've done:
let
Source = RawData,
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Date Time", type datetime}, {"Complete Date Time", type datetime}, {"Machine", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Added PrevCompleteDateTime" = Table.AddColumn(#"Added Index", "Previous Row Complete Date Time", each if [Index] = 0 then null else if #"Added Index"{[Index]-1}[Machine] = [Machine] then #"Added Index"{[Index]-1}[Complete Date Time] else null),
#"Added IsNotLastRow" = Table.AddColumn(#"Added PrevCompleteDateTime", "Is Not Last Row", each if [Index] = List.Max(#"Added Index"[Index]) then null else if #"Added Index"{[Index]+1}[Machine] = [Machine] then 1 else null),
#"Inserted Time Gap" = Table.AddColumn(#"Added IsNotLastRow", "Time Gap", each [Is Not Last Row] * ( [Start Date Time] - [Previous Row Complete Date Time] ), type duration)
in
#"Inserted Time Gap" - 5 years ago
You can use Imke's custom function to improve performance: https://www.thebiccountant.com/2018/07/12/fast-and-easy-way-to-reference-previous-or-next-rows-in-power-query-or-power-bi/
I have modified it slightly for you, note this won't give you null for the last row, but will be null for first row of each machine.
See updated attachment.
You can use Imke's custom function to improve performance: https://www.thebiccountant.com/2018/07/12/fast-and-easy-way-to-reference-previous-or-next-rows-in-power-query-or-power-bi/
I have modified it slightly for you, note this won't give you null for the last row, but will be null for first row of each machine.
See updated attachment.
- LinlookZDJ5 years agoHelper I
Hi Allison,
Thank you so much!
Best regards
Lin