跳至主要内容

Table.Pivot

指定一組代表屬性/值組的資料行之後,將 attribute 資料行中的資料輪換成資料行標題。

Syntax

Table.Pivot(
table as table,
pivotValues as list,
attributeColumn as text,
valueColumn as text,
optional aggregationFunction as function
) as table

Remarks

指定一組代表屬性/值組的資料行之後,將 attribute 資料行中的資料輪換成資料行標題。

Examples

Example #1

採用資料表 ({ [ key = "x", attribute = "a", value = 1 ], [ key = "x", attribute = "c", value = 3 ], [ key = "y", attribute = "a", value = 2 ], [ key = "y", attribute = "b", value = 4 ] }) 之 attribute 資料行中的 "a"、"b" 和 "c" 值,並將它們樞紐分析成各自的資料行。

Table.Pivot(
Table.FromRecords({
[key = "x", attribute = "a", value = 1],
[key = "x", attribute = "c", value = 3],
[key = "y", attribute = "a", value = 2],
[key = "y", attribute = "b", value = 4]
}),
{"a", "b", "c"},
"attribute",
"value"
)

Result:

Table.FromRecords({
[key = "x", a = 1, b = null, c = 3],
[key = "y", a = 2, b = 4, c = null]
})

Example #2

採用資料表 ({ [ key = "x", attribute = "a", value = 1 ], [ key = "x", attribute = "c", value = 3 ], [ key = "x", attribute = "c", value = 5 ], [ key = "y", attribute = "a", value = 2 ], [ key = "y", attribute = "b", value = 4 ] }) 之 attribute 資料行中的 "a"、"b" 和 "c" 值,並將它們樞紐分析成各自的資料行。索引鍵 "x" 的屬性 "c" 具有多個相關聯的值,因此請使用函數 List.Max 來解決衝突。

Table.Pivot(
Table.FromRecords({
[key = "x", attribute = "a", value = 1],
[key = "x", attribute = "c", value = 3],
[key = "x", attribute = "c", value = 5],
[key = "y", attribute = "a", value = 2],
[key = "y", attribute = "b", value = 4]
}),
{"a", "b", "c"},
"attribute",
"value",
List.Max
)

Result:

Table.FromRecords({
[key = "x", a = 1, b = null, c = 5],
[key = "y", a = 2, b = 4, c = null]
})

Category

Table.Column operations