次の方法で共有


Table.TransformColumns

構文

Table.TransformColumns(
    table as table,
    transformOperations as list,
    optional defaultTransformation as nullable function,
    optional missingField as nullable number
) as table

バージョン情報

リスト内の各列操作を適用して、指定したテーブルを変換します。

  • table: 変換するテーブル。
  • transformOperations: テーブルに対して行う変換。 このパラメーターの形式は、{ 列名、変換 } または { 列名、変換、新しい列の種類 } のいずれかです。
  • 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)}
})