简体   繁体   English

如何对 Power Query 中的 N 列求和

[英]How to sum N columns in Power Query

My data gets updated every month so I'm trying to create a power query table that would show the sum of the pivoted (N) columns that I created but I can't seem to figure out how to do it in power query.我的数据每个月都会更新,因此我正在尝试创建一个电源查询表,该表将显示我创建的旋转 (N) 列的总和,但我似乎无法弄清楚如何在电源查询中执行此操作。

I have this code currently:我目前有这个代码:

在此处输入图像描述

在此处输入图像描述

  • After Pivoting:转身后:
  • Create a list of the columns to sum创建要求和的列列表
  • Add an Index column to restrict to each row添加一个索引列以限制每一行
  • Add a column which Sums the columns for just that row添加一列,对仅该行的列求和
  • Remove the Index colum删除索引列
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month Yr", Date.Type}, {"Attribute", type text}, {"Value", Currency.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "MonthYear", each Date.ToText([Month Yr],"MMMM yyyy")),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Month Yr"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[MonthYear]), "MonthYear", "Value", List.Sum),

//NEW code added after your Pivoted Column line

//Get List of columns to sum
//  Assumes this list all columns **except the first** in the Pivot table
//  There are other methods of generating this list if this assumption is incorrect
colToSum = List.RemoveFirstN(Table.ColumnNames(#"Pivoted Column"),1),

//Add Index Column
IDX = Table.AddIndexColumn(#"Pivoted Column","Index",0,1),

//Sum each row of "colToSum"
totals = Table.AddColumn(IDX, "Sum", each List.Sum(
        Record.ToList(
            Table.SelectColumns(IDX,colToSum){[Index]})
    ), Currency.Type),
    #"Removed Columns1" = Table.RemoveColumns(totals,{"Index"})


in
#"Removed Columns1"

在此处输入图像描述

You can group and then merge into the table after pivoting您可以分组,然后在透视后合并到表格中

#"Grouped Rows" = Table.Group(#"Changed Type", {"Atribute"}, {{"Sum", each List.Sum([Value]), type number}}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type", {{"Month Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Changed Type", {{"Month Year", type text}}, "en-US")[#"Month Year"]), "Month Year", "Value", List.Sum),
#"Merged Queries" = Table.NestedJoin(#"Pivoted Column",{"Atribute"}, #"Grouped Rows",{"Atribute"},"Table2",JoinKind.LeftOuter),
#"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Sum"}, {"Sum"})
in #"Expanded Table"

Or you can group, add it to the table, then pivot the combined new set或者你可以分组,添加到表中,然后 pivot 组合新设置

#"Grouped Rows" = Table.Group(#"Changed Type", {"Atribute"}, {{"Value", each List.Sum([Value]), type number}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Month Year", each "Sum"),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Month Year", "Atribute", "Value"}),
combined = #"Reordered Columns" & #"Changed Type",
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(combined, {{"Month Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(combined, {{"Month Year", type text}}, "en-US")[#"Month Year"]), "Month Year", "Value", List.Sum)
in  #"Pivoted Column"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM