Forum Discussion
Power Query : End date from the next Start Date
- 5 years ago
If your data source isn't too large, you can use this method. Add two INDEX columns, one that starts at 0, one that starts at 1.
Then merge the table to itself like this:When you expand the Date Created column, that will be your End Phase Date. Here is the table immedately after the merge - note that the sort order changes. We'll fix that in a minute.
Now just sort by the task and date created fields, and keep only the necessary fields.
Here is the M code to see how it works:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTLUN9Q3MjAyADKVYnUgYkZoYklAhjEWMRM0sWQgwxRNLAXIMMMiZo4sFgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [TaskName = _t, #"Date Created" = _t, Phase = _t]), #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type), #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1, Int64.Type), #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"TaskName", "Index.1"}, #"Added Index1", {"TaskName", "Index"}, "Added Index1", JoinKind.LeftOuter), #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"Date Created"}, {"Date Created.1"}), #"Renamed Columns" = Table.RenameColumns(#"Expanded Added Index1",{{"Date Created.1", "End Date"}}), #"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"TaskName", Order.Ascending}, {"Date Created", Order.Ascending}}), #"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows",{"TaskName", "Date Created", "Phase", "End Date"}) in #"Removed Other Columns"If this is slow Anonymous let me know. There is another way to do this. It is MUCH more complex - requires manual coding. Still, not overly ornerous, but not necessary for a few hundred or even a few thousand records. But it would perform well over hundreds of thousands of records.
Also, in the future, please post actual data to use - images are great for expected results, but we have to key stuff in.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
If your data source isn't too large, you can use this method. Add two INDEX columns, one that starts at 0, one that starts at 1.
Then merge the table to itself like this:
When you expand the Date Created column, that will be your End Phase Date. Here is the table immedately after the merge - note that the sort order changes. We'll fix that in a minute.
Now just sort by the task and date created fields, and keep only the necessary fields.
Here is the M code to see how it works:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTLUN9Q3MjAyADKVYnUgYkZoYklAhjEWMRM0sWQgwxRNLAXIMMMiZo4sFgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [TaskName = _t, #"Date Created" = _t, Phase = _t]),
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1, Int64.Type),
#"Merged Queries" = Table.NestedJoin(#"Added Index1", {"TaskName", "Index.1"}, #"Added Index1", {"TaskName", "Index"}, "Added Index1", JoinKind.LeftOuter),
#"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"Date Created"}, {"Date Created.1"}),
#"Renamed Columns" = Table.RenameColumns(#"Expanded Added Index1",{{"Date Created.1", "End Date"}}),
#"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"TaskName", Order.Ascending}, {"Date Created", Order.Ascending}}),
#"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows",{"TaskName", "Date Created", "Phase", "End Date"})
in
#"Removed Other Columns"
If this is slow Anonymous let me know. There is another way to do this. It is MUCH more complex - requires manual coding. Still, not overly ornerous, but not necessary for a few hundred or even a few thousand records. But it would perform well over hundreds of thousands of records.
Also, in the future, please post actual data to use - images are great for expected results, but we have to key stuff in.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
Hi edhans, can you please give me the idea for large dataset? I am working on a dataset which have 100K records.
Thanks