Table.FromValue

D

T

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

Overview of Table.FromValue Function

The Table.FromValue function is used to create a table in Power Query from a single value or a list of values. It takes two parameters: the first parameter is the value or list of values, and the second parameter is an optional string that represents the name of the column.

Syntax: `Table.FromValue(value as any, optional columnName as text)`

Example: `Table.FromValue(5, “Value”)`

This code will create a table with one column named “Value” and one row with the value of 5.

Understanding the M Code Behind Table.FromValue

The M code behind Table.FromValue is straightforward. Let’s take a look at the code:


let

Source = #table({"Value"}, {{value}})

in

Source


The code starts with the let statement, which is used to define a variable “Source.” The variable is assigned the value of the #table function. The #table function is used to create a table with the specified columns and rows.

In this case, the #table function is creating a table with one column named “Value” and one row with the value passed as the parameter in the Table.FromValue function.

The curly braces represent a list of column names, and the double curly braces represent a list of rows. Since we are passing only one value, there is only one row in the table.

Advanced Usage of Table.FromValue Function

The Table.FromValue function can be used in many ways, including creating a table from a list of values, creating a table with multiple columns, and creating a table with dynamic column names.

Creating a Table from a List of Values

To create a table from a list of values, you can pass the list of values as the first parameter in the Table.FromValue function. For example:


let

Source = Table.FromValue({1,2,3,4,5}, "Value")

in

Source


This code will create a table with one column named “Value” and five rows with the values 1,2,3,4,5.

Creating a Table with Multiple Columns

To create a table with multiple columns, you can use the #table function with multiple column names and rows. For example:


let

Source = #table({"Name", "Age", "Gender"}, {{"John", 30, "Male"}, {"Jane", 25, "Female"}})

in

Source


This code will create a table with three columns named “Name,” “Age,” and “Gender” and two rows with the values “John”, 30, “Male” and “Jane”, 25, “Female”.

Creating a Table with Dynamic Column Names

To create a table with dynamic column names, you can use the Table.FromRecords function along with the List.Zip function. For example:


let

ColumnNames = {"Name", "Age", "Gender"},

RowValues = {"John", 30, "Male"},

Source = Table.FromRecords({List.Zip(ColumnNames, RowValues)})

in

Source


This code will create a table with three columns named “Name,” “Age,” and “Gender” and one row with the values “John”, 30, “Male”.

Table.FromValue is a simple yet powerful function in Power Query that allows you to create a table from a single value or a list of values. The M code behind this function is straightforward and easy to understand. With advanced usage, you can create a table with multiple columns and dynamic column names, making your data transformation tasks even more efficient and effective.

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

Upcoming Courses

10-12 Jul 23 (London or Online)
25-27 Sep 23 (London or Online)
20-22 Nov 23 (London or Online)

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)