如何在电源查询中为一行中的特定键对多行进行分组?

How to group multiple rows for a specific key in one single row, in power query?

我一直试图在 Excel Power Query 中将多行组合成一行。

我正在使用的table列表如下:

Product ID Purchase ID Amount
1 10 50
1 11 25
2 12 10
2 13 20
3 14 10

我希望它如何显示

Product ID Purchase 1 Amount Purchase 2 Amount
1 10 50 11 25
2 12 10 13 20
3 14 10

有没有关于如何在一行中显示多行的简单过程? 我试图对产品 ID 进行分组,然后合并数据。但是我想尝试得到上面的结果。

谢谢!

假设 table 名称是表 1:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(
                Source,
                "Product ID",
                {
                    "a",
                    each let 
                            a =Table.ToRows(Table.RemoveColumns(_,"Product ID")),
                            b = List.Accumulate(
                                    {0..List.Count(a)-1},
                                    {},
                                    (s,d)=>s &{"Purchase ID " & Text.From(d+1),"Amount " & Text.From(d+1)}
                                ),
                            c =#table({"Product ID"} & b,{{_[Product ID]{0}} & List.Combine(a)})
                        in c
                }
),
    Res = Table.Combine(Group[a])
in
    Res

另一种方法,依靠反透视。可以直接处理与 ProductID

相邻的任意数量的列
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source , {"Product ID"}, "Attribute", "Value"),
#"Group"= Table.Group(#"Unpivoted Other Columns", {"Product ID"}, {{"GRP", each Table.TransformColumns(Table.AddIndexColumn(_, "Index", 0, (1/(List.Count(Table.ColumnNames(Source))-1))),{{"Index", each Number.RoundTowardZero(_), type number}}), type table}}),
#"Expanded GRP" = Table.ExpandTableColumn(#"Group", "GRP", {"Attribute", "Value", "Index"}, {"Attribute", "Value", "Index"}),
#"Added Custom" = Table.AddColumn(#"Expanded GRP", "Custom", each [Attribute]&Text.From([Index])),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute", "Index"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value", List.Sum)
in #"Pivoted Column"