Powerquery将分隔的列拆分为唯一的行

时间:2019-04-19 01:21:16

标签: excel powerquery

我有一个如下所示的电子表格:

enter image description here

我希望它看起来像这样:

enter image description here

我知道我可以使用PowerQuery来定界并扩展为行,但这仅适用于第一列。第二次这样做会引入很多重复项。

有帮助吗?

2 个答案:

答案 0 :(得分:0)

我只是将它们分开进行组合

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Removed Columns" = Table.RemoveColumns(Source,{"People"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Emails] <> null)),
SCBD = Table.ExpandListColumn(Table.TransformColumns(#"Filtered Rows", {{"Emails", Splitter.SplitTextByDelimiter(";", QuoteStyle.None)}}), "Emails"),
#"Removed Columns1" = Table.RemoveColumns(Source,{"Emails"}),
#"Filtered Rows1" = Table.SelectRows(#"Removed Columns1", each ([People] <> null)),
SCBD1 = Table.ExpandListColumn(Table.TransformColumns(#"Filtered Rows1", {{"People", Splitter.SplitTextByDelimiter(";", QuoteStyle.None)}}), "People"),
Combined = SCBD  & SCBD1,
#"Sorted Rows" = Table.Sort(Combined,{{"Name", Order.Ascending}, {"Emails", Order.Descending}})
in #"Sorted Rows"

答案 1 :(得分:0)

使用这种方法会丢失原始订单(如果需要,您可以尝试在末尾重新排序)。

如果在某个时候您需要拆分更多列,则可以将其添加到let dataFromSheet = Table.FromColumns({{"Cereal Killers", "Acme Products", "Arkham Asylum"}, {"123 Sugar Way", "345 Whoville Place", "Gotham City"}, {"abc@def.com; xyz@abc.com; jkl@mno.com", null, "the.scarecrow@nolan.com; jokesonyou@joker.com"}, {"Tony Tiger; Toucan Sam; Lucky Leprauchan", "W. Coyote; R. Runner; R. Rabbit", null}}, type table [Name=text, Address=text, Emails=nullable text, People=nullable text]), columnsToSplit = {"Emails","People"}, loopOverColumnsToSplit = List.Accumulate(columnsToSplit, #table({}, {}), (tableState, currentColumn) => let reduceColumns = Table.SelectColumns(dataFromSheet, {"Name", "Address"} & {currentColumn}), dropNullRows = Table.SelectRows(reduceColumns, each Record.Field(_, currentColumn) <> null), splitIntoList = Table.TransformColumns(dropNullRows, {{currentColumn, each Text.Split(_, "; "), type list}}), expandList = Table.ExpandListColumn(splitIntoList, currentColumn), appendToAccumulatedTable = tableState & expandList in appendToAccumulatedTable ) in loopOverColumnsToSplit 列表中。

let
    dataFromSheet = Table.FromColumns({{"Cereal Killers", "Acme Products", "Arkham Asylum"}, {"123 Sugar Way", "345 Whoville Place", "Gotham City"}, {"abc@def.com; xyz@abc.com; jkl@mno.com", null, "the.scarecrow@nolan.com; jokesonyou@joker.com"}, {"Tony Tiger; Toucan Sam; Lucky Leprauchan", "W. Coyote; R. Runner; R. Rabbit", null}}, type table [Name=text, Address=text, Emails=nullable text, People=nullable text]),
    columnsToSplit = {"Emails","People"},
    numberOfColumnsToSplit = List.Count(columnsToSplit),
    loopOverColumnsToSplit = List.Accumulate(List.Positions(columnsToSplit), #table({}, {}), (tableState, currentIndex) =>
        let
        currentColumn = columnsToSplit{currentIndex},
        reduceColumns = Table.SelectColumns(dataFromSheet, {"Name", "Address"} & {currentColumn}),
        dropNullRows = Table.SelectRows(reduceColumns, each Record.Field(_, currentColumn) <> null),
        addIndex = Table.AddIndexColumn(dropNullRows, "toSortBy", currentIndex, numberOfColumnsToSplit),
        splitIntoList = Table.TransformColumns(addIndex, {{currentColumn, each Text.Split(_, "; "), type list}}),
        expandList = Table.ExpandListColumn(splitIntoList, currentColumn),
        appendToAccumulatedTable = tableState & expandList
        in appendToAccumulatedTable
    ),
    sorted = Table.Sort(loopOverColumnsToSplit, {"toSortBy", Order.Ascending}),
    dropHelperColumn = Table.RemoveColumns(sorted, {"toSortBy"})
in
    dropHelperColumn

如果保留顺序很重要,则可以尝试以下方法(可能需要花费一些时间,因为它还有一些额外的步骤)。

Emails

请澄清一下,如果您有一行Peoplenull列中的值均为filter,则该行将不会出现在输出表中。