将多个列和行中的相关值收集到一个列中

时间:2017-03-30 14:30:11

标签: powerquery m

我有这张桌子:

enter image description here

我想改为:

enter image description here

如您所见,我希望将所有列中所有P1行的所有相关条目收集到P1下的单个列中,并对P2和P3相关条目执行相同操作。

在PowerQuery / M中有简单的方法吗?

1 个答案:

答案 0 :(得分:1)

是:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"P", type text}, {"15", Int64.Type}, {"25", Int64.Type}, {"35", Int64.Type}, {"45", Int64.Type}, {"55", Int64.Type}, {"65", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"P"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"P"}, {{"AllData", each _, type table}}),
    TablesToLists = Table.TransformColumns(#"Grouped Rows",{{"AllData", each _[Value]}}),
    #"Transposed Table" = Table.Transpose(TablesToLists),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    TableFromColumns = Table.FromColumns(Record.FieldValues(#"Promoted Headers"{0}),Table.ColumnNames(#"Promoted Headers"))
in
    TableFromColumns