Forum Discussion
IlseV
3 years agoFrequent Visitor
parent child extension
Hi everyone, I hope you can help me. I have a table with 2 columns. 1 is the parent, 2 is the child But some childs can be parents themselves and again and again, so multiple levels. I would li...
jgordon11
3 years agoResolver II
This might take 2 to 6 hours on 120000 rows. Start it before leaving work for the day and it should be done when you return.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
tbl = Table.Buffer(Table.RenameColumns(Source,{{"Child", "Child level 1"}})),
chld = List.Buffer(tbl[Child level 1]),
nokids = Table.SelectRows(tbl, each not List.Contains(chld, [Parent] )),
haskids = Table.Buffer(Table.SelectRows(tbl, each List.Contains(chld, [Parent] ))),
lsta = List.Accumulate({1..12}, {nokids, #table({"Parent","Child level 1"}, {})}, (s,c)=>
let
lastcol = List.Last(Table.ColumnNames(s{0})),
ct = List.Count(List.RemoveNulls(Table.Column(s{0},lastcol)))
in
if ct>0 then
let
tbl1 =Table.NestedJoin(s{0}, {"Child level " & Text.From(c)}, haskids, {"Parent"}, "Child level " & Text.From(c+1)),
tbl1e = Table.ExpandTableColumn(tbl1, "Child level " & Text.From(c+1), {"Child level 1"}, {"Child level " & Text.From(c+1)}),
tbl1k = Table.SelectRows(tbl1e, each Record.Field(_, "Child level " & Text.From(c+1)) <> null)
in
{tbl1k, s{1} & Table.SelectRows(tbl1e, each Record.Field(_, "Child level " & Text.From(c+1)) = null)}
else s),
tbla = lsta{0} & lsta{1}
in
tbla