Table.TransformColumns
语法
Table.TransformColumns(table as table, transformOperations as list, optional defaultTransformation as nullable function, optional missingField as nullable number) as table
关于
通过应用 transformOperations
中列出的每个列操作来转换 table
(格式为 { column name, transformation } 或 { column name, transformation, new column type })。 如果指定了 defaultTransformation
,它将应用于 transformOperations
中未列出的所有列。 如果 transformOperations
中列出的列不存在,则会引发异常,除非可选参数 missingField
指定了替代参数(例如,MissingField.UseNull 或 MissingField.Ignore)。
示例 1
将列 [A] 中的文本值转换为数字值,并将列 [B] 中的数字值转换为文本值。
使用情况
Table.TransformColumns(
Table.FromRecords({
[A = "1", B = 2],
[A = "5", B = 10]
}),
{
{"A", Number.FromText},
{"B", Text.From}
}
)
输出
Table.FromRecords({
[A = 1, B = "2"],
[A = 5, B = "10"]
})
示例 2
将缺失列 [X] 中的数字值转换为文本值,同时忽略不存在的列。
使用情况
Table.TransformColumns(
Table.FromRecords({
[A = "1", B = 2],
[A = "5", B = 10]
}),
{"X", Number.FromText},
null,
MissingField.Ignore
)
输出
Table.FromRecords({
[A = "1", B = 2],
[A = "5", B = 10]
})
示例 3
将缺失列 [X] 中的数字值转换为文本值,同时将不存在的列中的值默认设置为 null。
使用情况
Table.TransformColumns(
Table.FromRecords({
[A = "1", B = 2],
[A = "5", B = 10]
}),
{"X", Number.FromText},
null,
MissingField.UseNull
)
输出
Table.FromRecords({
[A = "1", B = 2, X = null],
[A = "5", B = 10, X = null]
})
示例 4
递增列 [B] 中的数字值并将其转换为文本值,并将所有其他列转换为数字。
使用情况
Table.TransformColumns(
Table.FromRecords({
[A = "1", B = 2],
[A = "5", B = 10]
}),
{"B", each Text.From(_ + 1), type text},
Number.FromText
)
输出
Table.FromRecords({
[A = 1, B = "3"],
[A = 5, B = "11"]
})