Forum Discussion
Group measure into date ranges
- 6 years ago
Hi BolaSquirrel,
You could try to use M code to see whether it work or not
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY1BDsAgCAT/wtkEAS3pW4z//0YTW6jIdbKzMwY0KMCVbiQkhlkC4ZfwRvoiZISRMtFoOfFnQYoTSTdicZfaJ/mkmyQ/4Ro31ynpmdaUVkvLRgTmfAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [rig = _t, date = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"rig", Int64.Type}, {"date", type date}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1), #"Grouped Rows" = Table.Group(#"Added Index", {"rig"}, {{"all", each _, type table [rig=number, date=date, Index=number]}}), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([all],"newc",1,1)), #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"date", "newc"}, {"date", "newc"}), #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"all"}), #"Replaced Value" = Table.ReplaceValue(#"Removed Columns",each [newc],each if [rig]=1 then [newc] else null,Replacer.ReplaceValue,{"newc"}), #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"date", Order.Ascending},{"rig", Order.Ascending}}), #"Filled Down" = Table.FillDown(#"Sorted Rows",{"newc"}), #"Filled Up" = Table.FillUp(#"Filled Down",{"newc"}) in #"Filled Up"Best Regards,
Zoe ZhiIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
I've editted my initial post with the data. The first data set is a collection of working times. The second data set is when parts were changed out. So for rig 1 the repair dates were 2/15, 2/17, 3/23, 5/1, 5/4, 5/5, and 6/4. I'd like to add a new column to the first table that lists a part number. For anything before 2/15 it would be part 1, for 2/17-3/23 it would be part 2, for 3/23-5/1 it would be part 3, and so on and so forth. I'm not sure what the best way to tackle this is since there's a variable number of days between new parts. And I don't know how many rows I'll have for any particular rig.
Hi BolaSquirrel,
You could try to use M code to see whether it work or not
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY1BDsAgCAT/wtkEAS3pW4z//0YTW6jIdbKzMwY0KMCVbiQkhlkC4ZfwRvoiZISRMtFoOfFnQYoTSTdicZfaJ/mkmyQ/4Ro31ynpmdaUVkvLRgTmfAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [rig = _t, date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"rig", Int64.Type}, {"date", type date}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Grouped Rows" = Table.Group(#"Added Index", {"rig"}, {{"all", each _, type table [rig=number, date=date, Index=number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([all],"newc",1,1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"date", "newc"}, {"date", "newc"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"all"}),
#"Replaced Value" = Table.ReplaceValue(#"Removed Columns",each [newc],each if [rig]=1 then [newc] else null,Replacer.ReplaceValue,{"newc"}),
#"Sorted Rows" = Table.Sort(#"Replaced Value",{{"date", Order.Ascending},{"rig", Order.Ascending}}),
#"Filled Down" = Table.FillDown(#"Sorted Rows",{"newc"}),
#"Filled Up" = Table.FillUp(#"Filled Down",{"newc"})
in
#"Filled Up"
Best Regards,
Zoe Zhi
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.