Table.FromColumns

D

T

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

In this article, we will delve into the M code behind the Table.FromColumns function, and explore how it works and how to use it effectively.

The Syntax of Table.FromColumns

Before we dive into the details of the M code behind Table.FromColumns, let’s first take a look at its syntax. The basic syntax of the function is as follows:


Table.FromColumns(columns as list, optional headers as list)


The `columns` parameter is a required list of columns that will be used to create the table. Each column in the list should be a list of values of the same data type. The `headers` parameter is an optional list of headers for each column. If headers are not provided, the function will create default headers for each column.

The M Code Behind Table.FromColumns

The M code behind Table.FromColumns is relatively simple, yet powerful. The function takes a list of columns as input, and constructs a table from it. Here is the M code behind the function:


(Table as list, Headers as list) =>

let

Headers = if Headers = null then List.Transform(Table, each "Column " & Text.From(List.PositionOf(Table, _))) else Headers,

ColumnCount = List.Count(Table{0}),

TableWithHeaders = Table.FromColumns(Table, Headers),

Result = Table.Resize(TableWithHeaders, {ColumnCount})

in

Result


The function first checks whether headers are provided. If not, it creates default headers for each column using the `List.Transform` function. The `ColumnCount` variable is then set to the number of values in the first column of the `Table` parameter.

Next, the function creates a table from the columns using the `Table.FromColumns` function. The resulting table includes the headers provided (if any) and is stored in the `TableWithHeaders` variable.

Finally, the function resizes the table to include all the columns by calling the `Table.Resize` function. The resulting table is then returned as the `Result` variable.

Tips for Using Table.FromColumns

Here are some tips for using Table.FromColumns effectively:

– Ensure that each column in the list has the same number of values, otherwise the function will result in an error.

– If headers are not provided, make sure to inspect the default headers created by the function to ensure they are appropriate for your data.

– If your data contains null or empty values, you may need to handle them explicitly to avoid issues with Table.FromColumns.

Table.FromColumns is a powerful function in Power Query that allows users to create a table from a list of columns. Understanding the M code behind the function can help users use it more effectively, and avoid common issues that can arise. Hopefully, this article has provided a useful introduction to the M code behind Table.FromColumns.

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)