Forum Discussion
brandonab
3 years agoFrequent Visitor
Merge rows with matching cell and consecutive dates
I need help with merging rows that have the same job title and consecutive dates. Example: Employee Number Job Title Start Date End Date 123 Manager May 11, 2011 Septemer 14, 2011 ...
ThxAlot
Super User
3 years agoA generic PQ solution
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJR8k3MS0xPLQKzKhUMDXUUjAwMDYHc4NSCktTcpNQiBUMTqGCsDqYuJGWmcL3+ySX5ICFjAzw6vRLzShOLgHZCFBkBhdxSk4rAYqZQIYQ+n9TEFCDll18Gsc0crMIEWZORGVjMHFMXTImROVQFyB1FyRkKFkg6jI0METog0pZoyo0NYepjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Employee Number" = _t, #"Job Title" = _t, #"Start Date" = _t, #"End Date" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee Number", Int64.Type}, {"Job Title", type text}, {"Start Date", type date}, {"End Date", type date}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
Grouped =
let rows = Table.ToRecords(#"Added Index")
in Table.Group(
#"Added Index",
Table.ColumnNames(#"Added Index"),
{"grp", each [Start = List.Min([Start Date]), End = List.Max([End Date])]},
0,
(x,y) => Byte.From(
x[Employee Number]<>y[Employee Number]
or
y[Start Date] - #duration(1,0,0,0) <> rows{y[Index]-1}[End Date])
),
#"Expanded grp" = Table.ExpandRecordColumn(Table.RemoveColumns(Grouped, {"Start Date","End Date","Index"}), "grp", {"Start", "End"})
in
#"Expanded grp"
From
to
- StefanoGrimaldi3 years ago
Resident Rockstar
try a more direct approach like Mahesh0016 or mine, looks like yours works but that will have a performance impact in the refresh query when data gets bigger and bigger, you can achieve the same using group by only.