Forum Discussion

MagikJukas's avatar
MagikJukas
Resolver III
1 year ago
Solved

Merge table in loop

Hello, assuming I have a table like this: Father Son A B B C C D D E E F F G G H     I would like to merge this table to obtain: Father Son A B A ...
  • AlienSx's avatar
    1 year ago
    let
        Source = your_table,
        rec = Record.FromList(Source[Son], Source[Father]), 
        back_to_back = (s, name) => 
            [next_name = Record.FieldOrDefault(rec, name),
            next = if next_name is null then s else @back_to_back(s & {next_name}, next_name)][next],
        add_col = Table.AddColumn(Table.RemoveColumns(Source, {"Son"}), "Son", (x) => back_to_back({}, x[Father])),
        expand = Table.ExpandListColumn(add_col, "Son")
    in
        expand