共用方式為


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

關於

依據 table 所定義的索引鍵資料行,將 key 的資料列分組。 key 可以是單一資料行名稱或資料行名稱清單。 針對每個群組,會建立一筆記錄,其中包含關鍵欄位及其值,還有任何由 aggregatedColumns 指定的彙總欄位。 可選擇性地指定 groupKindcomparer

如果資料已經依索引鍵資料列排序,那麼可以提供groupKind GroupKind.Local。 在某些情況下,這可能會改善分組的效能,因為會假設具有一組指定索引鍵值的所有資料列都是連續的。

傳遞 comparer 時請注意,如果它將不同的索引鍵視為相等,則資料列可能會放在索引鍵與其本身不同的群組中。

此函式不保證其傳回的資料列順序。

範例 1

將數據表分組,添加一個匯總欄位 [total],其中包含價格總和(每個 List.Sum([價格]))。

使用方式

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

比較子函式