project-by-names operator

Learn how to use the project-by-names operator to select a subset of columns from the input table by their names, dynamic arrays, or name patterns.

Select and reorder a subset of columns from the input table by their names, dynamic arrays, or name patterns.

Syntax

T | project-by-names ColumnSpecifier [, …]

Parameters

NameTypeRequiredDescription
Tstring✔️The tabular input from which to remove columns.
ColumnSpecifierstring✔️The name of the column, dynamic array of column names, or column wildcard pattern to keep and reorder.

Returns

A table containing only the columns specified in the ColumnSpecifier parameter, in the specified order. All unspecified columns are excluded from the result.

Examples

The following examples demonstrate how to use the project-by-names operator to select and reorder columns from a table. The input table has four columns: Name, Age, City, and Country.

Keep and reorder specific columns

Use the operator to keep only the Name and City columns.

datatable(Name:string, Age:int, City:string, Country:string) 
['Peter', 39, 'New York', 'USA']
| project-by-names "Name", "City"

Output

NameCity
PeterNew York

Keep columns using a dynamic array

Use the operator to select columns from the input table by providing a dynamic array of column names.

datatable(Name:string, Age:int, City:string, Country:string) 
['Peter', 39, 'New York', 'USA']
| project-by-names dynamic(["Name", "Country"])

Output

NameCountry
PeterUSA

Keep columns using column name patterns

Use the operator to select columns from the input table starting with C.

datatable(Name:string, Age:int, City:string, Country:string) 
['Peter', 39, 'New York', 'USA']
| project-by-names "C*"

Output

CityCountry
New YorkUSA

Lookup and keeping lookup columns using a dynamic array

Use the operator to keep columns from a table after performing a lookup, specifying which columns to retain using a dynamic array.

let LookupTable = 
    datatable(Name:string, Age:int, City:string, Country:string)
    ['Peter', 39, 'New York', 'USA']; 
let LookupColumns = (Source:(Name:string), lookup_columns: dynamic) { 
    Source 
    | lookup LookupTable on Name
    | project-by-names column_names_of(Source), lookup_columns
};
datatable(Name:string, Data:string)
['Peter', 'Source-data']
| invoke LookupColumns(dynamic(['Country']))

Output

NameDataCountry
PeterSource-dataUSA

Ignore nonexisting columns

Use the operator to keep columns from a table after performing a lookup, specifying which columns to retain using a dynamic array that includes a nonexisting column name.

let LookupTable = 
    datatable(Name:string, Age:int, City:string, Country:string)
    ['Peter', 39, 'New York', 'USA']; 
let LookupColumns = (Source:(Name:string), lookup_columns: dynamic) { 
    Source 
    | lookup LookupTable on Name
    | project-by-names column_names_of(Source), lookup_columns
};
datatable(Name:string, Data:string)
['Peter', 'Source-data']
| invoke LookupColumns(dynamic(['Country', 'NonExistent']))

Output

NameDataCountry
PeterSource-dataUSA