Share via


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

將資料表分組,並新增包含價格總和 ("each List.Sum([price])") 的彙總資料行 [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"}
)