Skip to main content

Table.SelectColumns

Returns a table with only the specified columns.

Syntax

Table.SelectColumns(
table as table,
columns as any,
optional missingField as MissingField.Type
) as table

Remarks

Returns the table with only the specified columns.

  • table: The provided table.
  • columns: The list of columns from the table table to return. Columns in the returned table are in the order listed in columns.
  • missingField: (Optional) What to do if the column does not exist. Example: MissingField.UseNull or MissingField.Ignore.

Examples

Example #1

Only include column [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"
)

Result:

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

Example #2

Only include columns [CustomerID] and [Name].

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

Result:

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

Example #3

If the included column does not exist, the default result is an error.

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

Result:

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

Example #4

If the included column does not exist, option <code>MissingField.UseNull</code> creates a column of null values.

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

Result:

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

Category

Table.Column operations