Forum Discussion
abdsk
2 years agoNew Member
Skipping overlapping date & get date difference.
Hi there, I got a requirement where I need to skip overlapping dates & only consider minimum start date & max end date e.g. One Machine started at 12-05-2023 02:00 & ended at 12-05-2023 10:30 meanw...
- 2 years ago
Hello abdsk ,
this can be achieved by the magical 5th parameter in the Table.Group-function.
If you paste the following M-code into the advanced editor of a blank query, you can follow the steps along:let Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "i45W8k1MzsjMS1XSUTI00jcw1TcyMDJWMDCyMjAAIlRRQwMrY7BorA4ufZZQFaj6zKCm4dRnaI7NPiMD7PqMkV0E12egb2AGdQV2fQYmCBXGCH3GcFG4abGxAA==", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Equipment = _t, #"Start Time" = _t, #"End Time" = _t] ), #"Changed Type" = Table.TransformColumnTypes( Source, {{"Start Time", type datetime}, {"End Time", type datetime}} ), #"Grouped Rows" = Table.Group( #"Changed Type", {"Equipment", "Start Time", "End Time"}, { {"Count", each _}, {"MinStart", each List.Min([Start Time]), type nullable datetime}, {"MaxEnd", each List.Max([End Time]), type nullable datetime} }, GroupKind.Local, (x, y) => Number.From(x[End Time] < y[Start Time]) ), #"Inserted Time Subtraction" = Table.AddColumn( #"Grouped Rows", "Duration", each Duration.TotalHours([MaxEnd] - [MinStart]) ) in #"Inserted Time Subtraction" - 2 years ago
Hi abdsk ,
unfortunately this is not so easy any more, as it has a different logic than I originally thought.
Please use this code instead:let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("xdNBDsIwDATAr6Ccg+S4LbS8BKj6/2/UvsGu1hInpJyckRPbyb635+vderNxjeXmfjF7mGXoBqGjV3r5SU9ab6THXerBuSuNx9YaS6o1bpXajbTPWjtrPR3nKnM6LjQeG6FV58Zje5uwlA/NPcnHoG7Ck883KDRtRb9N65W1a72R9v9oKilCs9aYKEK6g/kbUGObvn8D3pv6fZw=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Equipment = _t, #"Start Time" = _t, #"End Time" = _t]), ChangedType = Table.TransformColumnTypes( Source, {{"Equipment", type text}, {"Start Time", type datetime}, {"End Time", type datetime}} ), #"Grouped Rows" = Table.Group( ChangedType, {"Equipment"}, { { "Result", each let InputTable = Table.Buffer(Table.SelectColumns(_, {"Start Time", "End Time"})), Custom1 = List.Generate( () => [ MinStart = InputTable{0}[Start Time], MaxEnd = InputTable{0}[End Time], Group = 1, Counter = 0 ], each [Counter] <= Table.RowCount(InputTable), each [ CurrentStart = InputTable{[Counter]}[Start Time], CurrentEnd = InputTable{[Counter]}[End Time], GroupChange = CurrentStart > [MaxEnd] and CurrentEnd > [MinStart], MinStart = if GroupChange then CurrentStart else List.Min({CurrentStart, [MinStart]}), MaxEnd = if GroupChange then CurrentEnd else List.Max({CurrentEnd, [MaxEnd]}), Group = if GroupChange then [Group] + 1 else [Group], Counter = [Counter] + 1 ] ), #"Converted to Table" = Table.FromList( Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error ), #"Expanded Column1" = Table.ExpandRecordColumn( #"Converted to Table", "Column1", {"MinStart", "MaxEnd", "Group"} ), #"Grouped Rows" = Table.Group( #"Expanded Column1", {"Group"}, { {"MinStart", each List.Min([MinStart]), type datetime}, {"MaxEnd", each List.Max([MaxEnd]), type datetime} } ) in #"Grouped Rows" } } ), #"Expanded Result" = Table.ExpandTableColumn( #"Grouped Rows", "Result", {"MinStart", "MaxEnd"} ) in #"Expanded Result"