語法
Table.TransformColumns(
table as table,
transformOperations as list,
optional defaultTransformation as nullable function,
optional missingField as nullable number
) as table
關於
透過清單中的每個資料行操作來轉換指定的資料表。
-
table:要轉換的表格。 -
transformOperations:要對表格進行的轉換。 此參數的格式為 { column name, transformation } 或 { column name, transformation, new column type }。 -
defaultTransformation:(選擇性)未列在transformOperations中的所有欄位將使用預設轉換。 -
missingField: (選擇性) 指定遺漏值的預期動作。 如果 中transformOperations列出的資料行不存在,則會擲回例外狀況 (MissingField.Error),除非此參數指定替代方案。 使用下列其中一個值:-
MissingField.UseNull:所有遺漏的欄位將作為null的值來包含在內。 -
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] 中的數字值轉換為文字值,對於不存在的欄,預設為 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]
})
範例 3
將資料行 [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"]
})
範例 4
將美國假日發生的排程維護作業移至隔天,如果假日發生在星期五,則移至下一個星期一。
使用方式
let
MaintenanceSchedule = #table(type table [Task = text, Date = date],
{
{"HVAC Check", #date(2025, 7, 10)}, // Not a holiday
{"Window Washing", #date(2025, 9, 1)}, // Labor Day
{"Fire Drill", #date(2025, 9, 17)}, // Not a holiday
{"Light Replacement", #date(2025, 11, 27)} // Thanksgiving
}),
USHolidays = {
#date(2025, 1, 1), // New Year's Day
#date(2025, 7, 4), // Independence Day
#date(2025, 9, 1), // Labor Day
#date(2025, 11, 27), // Thanksgiving
#date(2025, 12, 25) // Christmas
},
AdjustedSchedule = Table.TransformColumns(
MaintenanceSchedule,
{{"Date", each if List.Contains(USHolidays, _) then
if Date.DayOfWeek(_, Day.Sunday) = 5 then
Date.AddDays(_, 3) // Friday to Monday
else
Date.AddDays(_, 1) // Other to next day
else _, type date}}
)
in
AdjustedSchedule
輸出
#table(type table[Task = text, Date = date],
{
{"HVAC Check", #date(2025, 7, 10)},
{"Window Washing", #date(2025, 9, 2)},
{"Fire Drill", #date(2025, 9, 17)},
{"Light Replacement", #date(2025, 11, 28)}
})