PowerQuery 中基于列名的行总计

Row totals based on column name in PowerQuery

我有一个包含大约 400 列的数据文件。我需要将此数据导入 PowerPivot。为了减小文件大小,我想使用 PowerQuery 创建 2 个不同的 行总计 ,然后在加载时删除所有不需要的列。

虽然我的第一行总计列 (RowTotal1) 将对所有 400 列求和,但我还想要第二行总计 (RowTotal2) 从 RowTotal1 中减去名称 包含文本 [=68] 的任何列=]在里面。

其次,我想将“国家/地区”列中的值用作变量,以减去包含此变量的任何列。例如

站点----国家/地区----Col1---- Col2----ClickCol1----Col3----德国----RowTotal1----RowTotal2

1a--------美国--------2--------4------------8-- ----------16------------24------------54------------ --46--------

2a-----德国--------2--------4------------8-------- ----16------------24------------54----------------22--- ----

RowTotal1 = 2 + 4 + 8 + 16 + 24

RowTotal2 (第一行) = 54 - 8 (ClickCol1)

RowTotal2 (第二行) = 54 - 24 (德国) - 8 (ClickCol1)

这可能吗? (编辑:是的。请参阅下面的答案)

修改后的问题:有没有比一次将 300+ 百万行分组更有效的内存方法?

代码看起来像这样:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Site", type text}, {"Country", type text}, {"Col1", Int64.Type}, {"Col2", Int64.Type}, {"ClickCol1", Int64.Type}, {"Col3", Int64.Type}, {"Germany", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Country", "Site"}, "Attribute", "Value"),
    #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Other Columns", "Value2", each if [Country] = [Attribute] or [Attribute] = "ClickCol1" then 0 else [Value] ),
    #"Grouped Rows" = Table.Group(#"Added Conditional Column", {"Site", "Country"}, {{"RowTotal1", each List.Sum([Value]), type number},{"RowTotal2", each List.Sum([Value2]), type number}})
in
    #"Grouped Rows"

但是既然你的栏目很多,我应该解释一下步骤:

  • (假设您在 Excel 文件中有这些)将它们导入 Power Query
  • Select "Site" 和 "Country" 列(使用 Ctrl),右键单击 > 逆透视其他列
  • 使用此公式添加列(您可能需要使用高级编辑器):Table.AddColumn(#"Unpivoted Other Columns"、"Value2",如果 [Country] = [Attribute] 或 [属性] = "ClickCol1" then 0 else [Value])
  • Select 站点和国家列,右键单击 > 分组依据
  • 让它看起来像这样: