共用方式為


Table.SelectColumns

語法

Table.SelectColumns(table as table, columns as any, optional missingField as nullable number) as table

關於

傳回 table,其中只包含指定的 columns

  • table: 提供的資料表。
  • columns: 資料表 table 中要傳回的資料行清單。 傳回資料表中的資料行順序會依照 columns 中所列的順序排序。
  • missingField: (選用) 資料行不存在時要採取的動作。 範例:MissingField.UseNullMissingField.Ignore

範例 1

僅包含資料行 [Name]。

使用方式

Table.SelectColumns(
    Table.FromRecords({
        [CustomerID = 1, Name = "Bob", Phone = "123-4567"],
        [CustomerID = 2, Name = "Jim", Phone = "987-6543"],
        [CustomerID = 3, Name = "Paul", Phone = "543-7890"],
        [CustomerID = 4, Name = "Ringo", Phone = "232-1550"]
    }),
    "Name"
)

輸出

Table.FromRecords({
    [Name = "Bob"],
    [Name = "Jim"],
    [Name = "Paul"],
    [Name = "Ringo"]
})

範例 2

僅包含資料行 [CustomerID] 和 [Name]。

使用方式

Table.SelectColumns(
    Table.FromRecords({[CustomerID = 1, Name = "Bob", Phone = "123-4567"]}),
    {"CustomerID", "Name"}
)

輸出

Table.FromRecords({[CustomerID = 1, Name = "Bob"]})

範例 3

如果所包含的資料行不存在,則預設結果為錯誤。

使用方式

Table.SelectColumns(
    Table.FromRecords({[CustomerID = 1, Name = "Bob", Phone = "123-4567"]}),
    "NewColumn"
)

輸出

[Expression.Error] The field 'NewColumn' of the record wasn't found.

範例 4

若包含的資料行不存在,則選項 MissingField.UseNull 會建立 null 值的資料行。

使用方式

Table.SelectColumns(
    Table.FromRecords({[CustomerID = 1, Name = "Bob", Phone = "123-4567"]}),
    {"CustomerID", "NewColumn"},
    MissingField.UseNull
)

輸出

Table.FromRecords({[CustomerID = 1, NewColumn = null]})