Power Query - 如何为多列添加总行数?

Power Query - How to add total rows for multiple column?

我正在寻找 Office 365 中 Power Query 的解决方案。

我有一个 table 有几列和 >30K 行。

基于文本的列之一是“关键”列。

我想要实现的简短描述是:

我需要按“关键”列分组并汇总这些年。

最后只保留聚合行。

以下是输入

Col1    Key 2016    2017    2018    2019    2020    col2    col3    amount
text1   Cat 15  20  15  20  10  text1   text1   500
text2   Cat 25  10      5   20  text2   text2   400
text3   Cat 5   15  5   20  25  text3   text3   200
text4   Dog 5   25  10  5   5   text4   text4   300
text5   Dog 5       25  25  15  text5   text5   200
text6   Bird    25  15  5   5   5   text6   text6   600

这就是我正在寻找的 结果:

Col1    Key 2016    2017    2018    2019    2020    col2    col3    amount
text1   Cat 45  45  20  45  55  text1   text1   500
text4   Dog 10  35  25  30  20  text4   text4   300
text6   Bird    25  15  5   5   5   text6   text6   600

这是我正在尝试做的视觉描述:

我很高兴得到任何帮助。

谢谢! 艾库特

这看起来应该适合你

它假定存在年份(2018、2020、2025 等)的列标题,因此它可以解析出这些列。它以任意顺序接受任意数量的列作为输入

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
LD=List.Difference(Table.ColumnNames(Source),List.Transform({1980..2050}, each Text.From(_)) ),  // look for columns titles that are not years between 1980 and 2050
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, LD, "Attribute", "Value"),
#"Grouped Rows1" = Table.Group(#"Unpivoted Other Columns", {"Key", "Attribute"}, {{"Value", each List.Sum([Value]), type number}}),
#"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Key"}, {{"Data", each Table.FirstN(Table.Sort(_,{{"amount", Order.Descending}}),1)}}),
#"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data",List.Difference(LD,{"Key"}), List.Difference(LD,{"Key"})),
#"Merged Queries" = Table.NestedJoin(#"Expanded Data",{"Key"},#"Grouped Rows1",{"Key"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Attribute", "Value"}, {"Attribute", "Value"}),
#"Pivoted Column" = Table.Pivot(#"Expanded Table2", List.Distinct(#"Expanded Table2"[Attribute]), "Attribute", "Value"),
#"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",Table.ColumnNames(Source))
in #"Reordered Columns"

它取消透视所有年份列,然后分组并添加年份列。然后它再次分组并为每个键选择数量最多的行。最后,它将第一组的所有年份数据合并到第二组并对其进行透视