0% found this document useful (0 votes)
16 views

Creating Table Values in Power Query M (40+ Examples) 2

Uploaded by

mmlinhtut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Creating Table Values in Power Query M (40+ Examples) 2

Uploaded by

mmlinhtut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

BLOG RESOURCES SITEMAP C

Creating Table Values in Power WRIT T EN BY RICK

Query M (40+ Examples) Rick is the founder of B


life's greatest pleasure
U p date d on November 29, 2023 8 Power Query
improve your skills.

Tables have a fundamental place in Power Query. Most data transformations are based on
tables; mastering how to create them from scratch can help you solve many challenges.

In this article, we will show you 6 different ways to create table values using Power Query,
using examples and explanations for each method. We’ll show you how to create tables
using records, lists, rows and values. With more than 40 examples, this post is an
elaborate guide that will help you understand how to create tables in Power Query, even if
you are a beginner. Let’s get started!

Table of contents

1. Functions for Creating Tables

There are many different functions that can help you make tables from scratch. In this
chapter, I will give you an overview of all these functions. You’ll learn about the different
ways to make tables using different types of data. It might seem a bit tricky at first but
don’t worry, I’ll explain everything in a way that’s easy to understand. Let’s get started and
make some tables!
S

··· M

Below, you can find a summary of the functions that create tables in the M language. It
does not contain all optional parameters of the functions but does provide an overview of
how the functions compare.

All of the below functions can help you manually create tables in Power Query. Most of
them have a way to input Colum Names, and they all take the column and row input in
their own way:

1 #table(
2 ColumnNames, // as text, list or table type
3 List of Lists // each list represents a row
4 )
5
6 Table.FromRecords(
7 List of Records, // each record represents a row
8 ColumnNames, // as text, list or table type
9 )
10
11 Table.FromList(
12 List of Values or Records, // each value/record represents a row
13 Optional Splitter, // depends on the first argument
14 ColumnNames, // as text, list or table type
15 )
16
17 Table.FromColumns(
18 List of Lists, // each list represents a column
19 ColumnNames, // as text, list or table type
20 )
21
22 Table.FromRows(
23 List of Lists, // each list represents a row
24 ColumnNames, // as text, list or table type
25 )
26
27 Table.FromValue(
28 Value as any, // this can be a regular value, list, record,
29 ) // list of records

Get more out of Power Query


Receive tips and tricks to help you take your Power Query skills to the next
level.

First Name Your Email... G E T S TA R T E D

I consent to receiving emails and personalized ads.

···

With this overview, it’s now time to delve into the details of each function. How do you
create tables from scratch?

2. #table Constructor

One of the easiest ways to create a table is with the #table constructor. The general
syntax for this function is:

1 #table(
2 columns as any,
3 rows as any,
4 ) as any

To see how this constructor creates tables, have a look at the following examples.

···

1 #table(
2 null, // not specifying any column name
3 {
4 { 1, "Apple" }, // determines values first row
5 { 2, "Prume" } // determines values second row
6 }
7 ) // Creates Table without defining Column Names
8
9
10 #table(
11 2, // specifying number of columns, no name
12 {
13 { 1, "Apple" }, // determines values first row
14 { 2, "Prume" } // determines values second row
15 }
16 ) // Creates Table with n number of columns, but no Column Names
17 // Note that if the number of columns is wrong, you get an error
18
19
20 #table(
21 { "ProductKey", "Product" }, // defines column names
22 {} // returns no rows
23 ) // Creates table with only Column Names, no rows.
24
25
26 #table(
27 { "ProductKey", "Product" }, // defines column names
28 {
29 { 1, "Apple" }, // determines values first row
30 { 2, "Prume" } // determines values second row
31 }
32 ) // Creates a table without defined Data Types.
33
34
35 #table(
36 type table[ ProductKey = Int64.Type, Product = Text.Type ],
37 {
38 { 1, "Apple" },
39 { 2, "Prume" }
40 }
41 ) // Creates a table with specified Data Types and Column Names

3. Table.FromRecords

Another way to create a table out of thin air is by transforming a record into a table with
the Table.FromRecords function. Within this function, each record represents a row in the
table. The syntax for this function is:

···

1 Table.FromRecords(
2 records as list,
3 optional columns as any,
4 optional missingField as nullable number
5 ) as table

Within this function, you can input a MissingField.Type of Missing.FieldError (used by


default) or MissingField.UseNull. The latter returns a null when a record misses a value
that other records have.

The code that creates simple tables then looks as follows:

1 Table.FromRecords(
2 {
3 [ ProductKey = 1, Product = "Apple" ] , // first record
4 [ ProductKey = 2, Product = "Prume" ] // second record
5 }
6 )
7 // Creates a table without defined column types
8
9
10 Table.FromRecords(
11 {
12 [ProductKey = 1, Product = "Apple"],
13 [ProductKey = 2, Product = "Prume"],
14 [ProductKey = 3] // This record misses Product
15 }
16 )
17 // Creates table with error on table row 3. By default the function
18 // returns a missing values error , identical to MissingField.Error
19
20
21 Table.FromRecords(
22 {
23 [ ProductKey = 1, Product = "Apple" ] ,
24 [ ProductKey = 2, Product = "Prume" ] ,
25 [ ProductKey = 3 ]
26 },
27 null,
28 MissingField.UseNull // returns null for missing fields
29 )
30 // Identical to previous table, but shows null for missing values
31
32
33 Table.FromRecords(
34 {
35 [ ProductKey = 1, Product = "Apple" ] ,
36 [ ProductKey = 2, Product = "Prume" ]
37 },
38 type table[ProductKey = Int64.Type, Product = Text.Type ]
39 )
40 // Creates a table with specified column types
41
42 Table.FromRecords(
43 {
44 [ Product = "Apple", ProductKey = 1 ] ,
45 [ ProductKey = 2, Product = "Prume" ]
46 },
47 type table[ProductKey = Int64.Type, Product = Text.Type ]
48 )
49 // Creates a table with the values in their respective column names.

···

An important note to end on. Within the Table.FromRecords function, the order of column
names in the provided records does not matter. This is different from the Table.FromList
function, where the order does matter.

4. Creating Tables from Lists

You can create a table from lists with two functions. Both Table.FromList and
Table.FromColumns can transform lists into a table, but they have their own approach.

···

4.1. Table.FromList: Single Column

The first function we will look at is Table.FromList. This function turns a list into a table by
applying the optional splitter function. You can use the 3rd argument to input a number
of columns, a list of column names, or a Tabletype.

The first 2 determine column names, and the TableType can also determine the data type
of a column. The 4th argument returns a default value when column input is missing.

1 Table.FromList(
2 list as list,
3 optional splitter as nullable function,
4 optional columns as any,
5 optional default as any,
6 optional extraValues as nullable number
7 ) as table

The following examples create a single-column table by using lists with values.

1 Table.FromList(
2 { "Apple", "Prume" } // specifies column values
3 )
4 // Creates table with name Column1, no data type
5
6
7 Table.FromList(
8 { 1, 2 } // specifies column values
9 )
10 // Returns error, the function assumes text values for regular lists.
11
12
13 Table.FromList(
14 { "Apple", "Prume" },
15 null, // specifies optional splitter
16 { "Product" } // defines column name
17 )
18 // Creates table with a defined name, no data type
19
20
21 Table.FromList(
22 { "Apple", "Prume" },
23 null,
24 type table[ Product = Text.Type ] // specifies column type and name
25 )
26 // Creates table with a defined column name and type

···

4.2. Table.FromList: Multi Column

To create multi-column tables, the Table.FromList offers several options. You can either
provide a list or record within a list. Then, Power Query needs to know which values to
extract from these objects.

For records, you can use the Record.FieldValues splitter function to indicate this. Other
available options deserve a separate blog post.

1 Table.FromList(
2 { "1, Apple", "2, Prume" }
3 )
4 // Creates table without defined Names or Data Types
5 // By default the function splits lists by Comma
6
7
8 Table.FromList(
9 {
10 [ ProductKey = 1, Product= "Apple" ], // defines list
11 [ ProductKey = 2, Product= "Prume" ] // of records
12 },
13 Record.FieldValues, // function splits the records
14 {"ProductKey", "Name"} // list defines column names, no data types
15 )
16 // Creates table without defined Data Types.
17
18 Table.FromList(
19 {
20 [ ProductKey = 1, Product = "Apple" ],
21 [ ProductKey = 2, Product = "Prume" ]
22 },
23 Record.FieldValues,
24 type table[ ProductKey = Int64.Type, Name = Text.Type ]
25 )
26 // Creates table with defined column names and types

···

Now, onto some more interesting behaviour of Table.FromList. When you provide the
function with a list of records, the naming within those records does not make a difference.
To define the name, you use the 3rd argument instead.

1 Table.FromList(
2 {
3 [ x = 1, y = "Apple" ], // x and y not returned as column name
4 [ x = 2, y = "Prume" ] // x and y not returned as column name
5 },
6 Record.FieldValues,
7 { "ProductKey", "Product" } // defines column names
8 )
9 // Resulting table is identical to previous example
10 // Record names are defined in the third argument.
11 // Not in the records themselves (x & y in line 3-4)

···

And even though the column names in the records of argument 1 don’t impact the
resulting column names, the order of columns does influence the output.

This behaviour is different from the Table.FromRecords function. The following 2


examples do not provide the same results:

1 Table.FromList(
2 {
3 [ ProductKey = 1, Product = "Apple" ],
4 [ ProductKey = 2, Product = "Prume" ]
5 },
6 Record.FieldValues,
7 type table[ ProductKey = Int64.Type, Name = Text.Type ]
8 )
9
10
11 Table.FromList(
12 {
13 [ Product = "Apple", ProductKey = 1 ],
14 [ ProductKey = 2, Product = "Prume" ]
15 },
16 Record.FieldValues,
17 type table[ ProductKey = Int64.Type, Name = Text.Type ]
18 )

4.3. Table.FromColumns

Even though the function name suggests otherwise, the Table.FromColumns function also
turns lists into a table. With this function, each list represents a column. When one column
has more values than others, the missing values get the value ‘null’.

···

The syntax for the function is:

1 Table.FromColumns(
2 lists as list,
3 optional columns as any
4 ) as table

You can create a table with Table.FromColumn in the following way:

1 Table.FromColumns(
2 {
3 {1, 2}, // Values for the 1st column
4 {"Apple", "Prume"} // Values for the 2nd column
5 }
6 )
7 // Creates a table, column names are Column1 and Column 2
8
9
10 Table.FromColumns(
11 {
12 { 1, 2 },
13 { "Apple", "Prume" }

You might also like