跳到主要内容

Table.Pivot

给定一对表示属性-值对的列,将属性列中的数据旋转为列标题。

Syntax

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

Remarks

给定一对表示属性-值对的列,将属性列中的数据旋转为列标题。

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 ] }) 的属性列中的值 "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 ] }) 的属性列中的值 "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