英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论