0

我想使用函数 Table.Group 在 Power Query 中创建动态分组依据

Table.Group(table as table, key as any, aggregatedColumns as list, optional groupKind as nullable number, optional comparer as nullable function) as table

#"Grouped Rows" = Table.Group(Source, {"name"}, 
                        {{"col1", each List.Max([col1]), type text},  //I used here max for simplification (otherwise text concat)
                         {"col2", each List.Sum([col1]), type number}}),

但是列出我需要从源表生成的聚合列。我可以得到 3 个列表:

ColumnNames = Table.ColumnNames(Source),
ColumnTypes = Table.Schema(Source)[TypeName],
ColumnFx = List.Max (type of function may differ based on column data type)

要创建聚合列列表,我需要生成类似的列表

aggCols = {ColumnNames{0}, ColumnFx{0}, ColumnTypes{0}} (I know it is nonsense in this format)

在另一种语言中,我会使用循环,但在我看来,使用 M 是不可能的。

样本输入:

name    col1    col2
n1    name11    1
n1    name12    2
n1    name13    3
n2    name21    4
n2    name22    5
n3    name32    6

样本输出:

name    col1    col2
n1    name13    6
n2    name22    9
n3    name32    6

有任何想法吗?

4

1 回答 1

0

您是否试图获得每列的最大值,按“名称”分组,而不考虑列数?

如果是这样,

右键单击名称列并取消透视其他列

单击选择名称和属性列、组,并为操作使用值列的最大值,调用新列“最大值”

单击选择属性列,转换...透视列...并选择最大列总和

示例代码

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"name"}, "Attribute", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"name", "Attribute"}, {{"Max", each List.Max([Value]), type number}}),
#"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Attribute]), "Attribute", "Max", List.Sum)
in #"Pivoted Column"
于 2020-11-18T21:36:05.643 回答