语法
Table.Group(
table as table,
key as any,
aggregatedColumns as list,
optional groupKind as nullable number,
optional comparer as nullable function
) as table
关于
按 table 定义的键列对 key 行进行分组。
key 可以是单个列名,也可以是列名列表。 对于每个组,构造一个记录,其中包含键列(及其值)以及 aggregatedColumns 指定的任何聚合列。 此外,还可以指定 groupKind 和 comparer。
如果数据已按键列排序,groupKind则可以提供 GroupKind.Local。 在某些情况下,这可能会提高分组的性能,因为具有给定键值集的所有行都被认为是连续的。
传递 comparer 时,请注意,如果它将不同的键视为相等,则可以将一行置于其键与其自身不同的组中。
此函数不保证它返回的行的顺序。
示例 1
对表进行分组,添加包含价格总和(“每个 List.Sum([价格])”的聚合列 [total] 。
使用情况
Table.Group(
Table.FromRecords({
[CustomerID = 1, price = 20],
[CustomerID = 2, price = 10],
[CustomerID = 2, price = 20],
[CustomerID = 1, price = 10],
[CustomerID = 3, price = 20],
[CustomerID = 3, price = 5]
}),
"CustomerID",
{"total", each List.Sum([price])}
)
输出
Table.FromRecords(
{
[CustomerID = 1, total = 30],
[CustomerID = 2, total = 30],
[CustomerID = 3, total = 25]
},
{"CustomerID", "total"}
)