组合行 – 动态列名

huangapple go评论69阅读模式
英文:

Group rows - dynamic column names

问题

I need to group the table and sum with dynamic column names (which is x in the code)

Is there a way to do that?

This is the present list, it changes every month...

组合行 – 动态列名

Sample code for only two columns

let
    Source = #"Cost Summary",
    x = List.Difference(Table.ColumnNames(Source),{"Bütçe Kodu", "Bütçe Kalemi"}),
    #"Grouped Rows" = Table.Group(Source, {"Bütçe Kodu"}, {{"Sum Total", each List.Sum([Total Cost]), type number}, {"sum 2023.08", each List.Sum([2022.08]), type nullable number}, {"sum 2023.09", each List.Sum([2022.09]), type nullable number}})
in
#"Grouped Rows"
英文:

I need to group the table and sum with dynamic column names (which is x in the code)

Is there a way to do that?

This is the present list, it changes every month...

组合行 – 动态列名

Sample code for only two columns

let
Source = #"Cost Summary",
x = List.Difference(Table.ColumnNames(Source),{"Bütçe Kodu", "Bütçe Kalemi"}),
#"Grouped Rows" = Table.Group(Source, {"Bütçe Kodu"}, {{"Sum Total", each List.Sum([Total Cost]), type number}, {"sum 2023.08", each List.Sum([2022.08]), type nullable number}, {"sum 2023.09", each List.Sum([2022.09]), type nullable number}})
in
#"Grouped Rows"

答案1

得分: 1

以下是您要翻译的内容:

  • Right click and remove any columns you don't want to be part of the sum or the group
  • Click select the columns you want to group, right click and unpivot other columns
  • Click select then right click to group and use the sum operation
  • Code below groups on name and sums the other columns.

代码部分已被省略,不会翻译。

英文:

Right click and remove any columns you don't want to be part of the sum or the group

Click select the columns you want to group, right click and unpivot other columns

Click select then right click to group and use the sum operation

组合行 – 动态列名

Code below groups on name and sums the other columns.

let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"name","date"}, "Attribute", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Attribute"}, {{"Sum", each List.Sum([Value]), type number}})
in  #"Grouped Rows"

组合行 – 动态列名

ALTERNATE VIEW

Right click and remove any columns you don't want to be part of the group or the sum

Click select the columns you want to group, right click and unpivot other columns

Click select attribute column, transform ... pivot column ... , use Value for the Values column and choose Sum from Aggregate Value Function

That will add up all remaining columns dynamically.

组合行 – 动态列名

Code below groups on name and date, and sums the other columns.

let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"name","date"}, "Attribute", "Value"),
#"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value", List.Sum)
in  #"Pivoted Column"

组合行 – 动态列名

答案2

得分: 1

"Since you do not show any data samples that can be copy/pasted, I am assuming x will be a List of the columns you wish to Sum."
"All you need to do is Transform x into a series of aggregations in the Table.Group function."

eg:

    #"Dynamic Sums" = List.Transform(x, (li)=> {"Sum " & li, (t)=> List.Sum(Table.Column(t,li)), type number }),
    #"Grouped Rows" = Table.Group(Source, {"Bütçe Kodu"}, #"Dynamic Sums")
in 
    #"Grouped Rows"
英文:

Since you do not show any data samples that can be copy/pasted, I am assuming x will be a List of the columns you wish to Sum.

All you need to do is Transform x into a series of aggregations in the Table.Group function.

eg:

    #"Dynamic Sums" = List.Transform(x, (li)=> {"Sum " & li, (t)=> List.Sum(Table.Column(t,li)), type number }),
    #"Grouped Rows" = Table.Group(Source, {"Bütçe Kodu"}, #"Dynamic Sums")
in 
    #"Grouped Rows"

huangapple
  • 本文由 发表于 2023年5月7日 21:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定