Forum Discussion
Calculate the difference in minutes between lines
- 2 years ago
Hi, create a blank query and paste the following code to see how you can create a calulated column for minutes lapsed between rows.
let
StartDateTime = #datetime(2024, 1, 1, 0, 0, 0),
EndDateTime = #datetime(2024, 1, 2, 0, 0, 0),
DurationInMinutes = 5,
Source = List.Generate(
() => StartDateTime,
(DateTime) => DateTime < EndDateTime,
(DateTime) => DateTime + #duration(0, 0, DurationInMinutes, 0)
),
TableFromList = Table.FromList(Source, Splitter.SplitByNothing(), {"DateTime"}),
ChangedType = Table.TransformColumnTypes(TableFromList, {{"DateTime", type datetime}}),
SortedTable = Table.Sort(ChangedType, {{"DateTime", Order.Ascending}}),
AddIndex = Table.AddIndexColumn(SortedTable, "Index", 0, 1, Int64.Type),
AddMinutesLapsed = Table.AddColumn(AddIndex, "Minutes Lapsed", each if [Index] = 0 then 0 else Duration.TotalMinutes([DateTime] - (AddIndex{[Index]-1}[DateTime])), type number)
in
AddMinutesLapsedIf I answered your question, please mark this thread as accepted and Thums Up!
Follow me on LinkedIn:
https://www.linkedin.com/in/mustafa-ali-70133451/
dufoq3 this must be accompanied by v3 with list shift 😁
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNzDUNzIwMlYwtzIwVYrVQRMzNMQUMzJDF7O0MjZSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
dt = Table.TransformColumnTypes(Source,{{"Date", type datetime}}),
diff_lst = List.Buffer(List.Zip({dt[Date], {List.First(dt[Date])} & List.RemoveLastN(dt[Date], 1)})),
diff_col = List.Transform(diff_lst, (x) => Duration.Minutes(x{0} - x{1})),
tbl = Table.FromColumns(Table.ToColumns(dt) & {diff_col}, Table.ColumnNames(dt) & {"diff"})
in
tbl