How can I expand all lists in a row of lists at the same time without repeating values?

你离开我真会死。 提交于 2019-12-23 05:32:29

问题


In response to this question regarding how to expand all lists in a row of lists at the same time, @Carl Walsh kindly provided this succinct and helpful code:

let
Source = #table({"A", "B"}, {{ {1,2}, {3,4}} }),
Expanded = List.Accumulate(
    Table.ColumnNames(Source), 
    Source, 
    (state, column) => Table.ExpandListColumn(state, column))
in
Expanded

Which yields this:

I would like to get this result instead:

I don't want values repeated in previously processed columns as each follow-on column is processed.

Is there a simple modification to Carl's code that will get me there?


回答1:


Maybe not really simple, but effective: the code below combines the columns with adjusted code so the lists are zipped and the inner lists are transformed into records. Next the list column is expanded, resulting in a column with nested records which are subsequently expanded.

Unfortunately, you can't use combine columns with nested lists, so I created some dummy text columns first that I combined in order to generate the base code, which I subsequently adjusted and I removed the steps with the dummy columns.

let
    Source = #table({"A", "B"}, {{ {1,2}, {3,4}} }),
    #"Merged Columns" = Table.CombineColumns(Source,{"A", "B"}, each List.Transform(List.Zip(_), each Record.FromList(_,{"A","B"})),"Merged"),
    #"Expanded Merged" = Table.ExpandListColumn(#"Merged Columns", "Merged"),
    #"Expanded Merged1" = Table.ExpandRecordColumn(#"Expanded Merged", "Merged", {"A", "B"}, {"A", "B"})
in
    #"Expanded Merged1"


来源:https://stackoverflow.com/questions/44675481/how-can-i-expand-all-lists-in-a-row-of-lists-at-the-same-time-without-repeating

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!