Table.HasColumns

D

T

The M Code Behind the Power Query M function Table.HasColumns

Understanding the Table.HasColumns Function

The Table.HasColumns function is a Boolean function that returns “true” if a table contains a specific column or a set of columns, and “false” otherwise. The syntax of the function is as follows:


Table.HasColumns(table as table, columns as any) as logical


In this syntax, “table” is the table that we want to check for the presence of a column, and “columns” can be a single column name or a list of column names.

The Table.HasColumns function is particularly useful when dealing with large datasets that contain multiple columns. Instead of manually scanning through each column to check for the presence of a specific column, we can use the Table.HasColumns function to automate the process.

The M Code Behind the Table.HasColumns Function

The Table.HasColumns function is written in M code, which is the language used by Power Query to perform data transformations. The M code behind the Table.HasColumns function is as follows:


(Table as table, columns as any) =>

let

columnNames = if Type.Is(columns, type list) then columns else {columns},

tableColumns = Table.ColumnNames(table),

matches = List.Intersect(columnNames, tableColumns)

in

List.Count(matches) = List.Count(columnNames)


Let’s break this code down to understand how it works.

The first line defines the function and specifies the two input parameters – “table” and “columns”.


(Table as table, columns as any) =>


The next line of code checks if the “columns” parameter is a list of column names or a single column name. If it is a list, then it is stored in the “columnNames” variable. If it is not a list, then it is converted to a list and stored in the “columnNames” variable.


columnNames = if Type.Is(columns, type list) then columns else {columns},


The next line of code retrieves the names of all the columns in the “table” parameter and stores them in the “tableColumns” variable.


tableColumns = Table.ColumnNames(table),


The next line of code compares the “columnNames” variable with the “tableColumns” variable and returns a list of all the matches between the two variables.


matches = List.Intersect(columnNames, tableColumns)


Finally, the last line of code checks if the number of matches between the two variables is equal to the total number of columns in the “columnNames” variable. If they are equal, then the function returns “true”. Otherwise, it returns “false”.


List.Count(matches) = List.Count(columnNames)


Use Cases for the Table.HasColumns Function

The Table.HasColumns function can be used in a variety of scenarios, some of which are:

Data Cleaning

When cleaning large datasets, it is not uncommon to encounter missing columns or columns that are named differently from what we expect. The Table.HasColumns function can be used to check if a specific column or set of columns is present in a table and make the necessary adjustments to the data.

Data Validation

Data validation is an essential part of data analysis. The Table.HasColumns function can be used to validate that a table contains all the required columns before performing any calculations or transformations.

Dynamic Column Selection

In some cases, we may want to select a subset of columns from a larger table based on some criteria. The Table.HasColumns function can be used to filter out tables that do not contain the required columns.

The Table.HasColumns function is a powerful function in Power Query that allows users to check whether a table contains a specific column or set of columns. It is an essential tool when dealing with large datasets that contain multiple columns. Understanding the M code behind the Table.HasColumns function is essential for customizing its behavior and building more complex data transformations.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)