Table.Group

语法

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

关于

key 定义的键列对 table 行进行分组。 key 可以是单个列名,也可以是列名列表。 对于每个组,构造一个记录,其中包含键列(及其值)以及 aggregatedColumns 指定的任何聚合列。 此外,还可以指定 groupKindcomparer

如果数据已经按键列排序,则可以提供 GroupKind.Local 的 groupKind。 在某些情况下,这可能会提高分组的性能,因为具有给定键值集的所有行都被认为是连续的。

传递 comparer 时,请注意,如果它将不同的键视为相等,则可以将一行置于其键与其自身不同的组中。

此函数不保证它返回的行的顺序。

示例 1

对表进行分组,同时添加一个聚合列 [total],其中包含价格总和 ("each List.Sum([price])")。

使用情况

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"}
)