kohera-logo-regular.svg

Power Query Beyond the Ribbon: Types

Some time ago I gave a session on SQL Server Days about Power Query. I really enjoyed speaking there. The title of the session was “Power Query Beyond The Ribbon”. I have to admit a major strength of Power Query is that you can do pretty much everything only by using the ribbon. However this doesn’t mean that learning something about M, the Power Query Formula Language, is a waste of time. With M you can solve some complex problems, automate parts or simplify your solution.

Before I move on, there is one thing you should always remember: M is case-sensitive! A bit of a pain, since we don’t have intellisense for the moment. Just be very meticulous.

 

Simple literals

The first time I wanted to do something with a script in Power Query, I needed to add a specific date. I searched the internet to find the proper function. I went through the complete Power Query Formula Categories and didn’t find a thing. An uploaded Powerpoint with some examples in it provided me the necessary information. To give you a little help I will give you a list of some literals you can use.

 

Complex types

It is also possible to instantiate more complex types with some simple code. ‘List’ and ‘Record’ were very intuitive to me, while ‘Table’ is a combination of some different elements.

 

To see the result of a list, you can test it by writing something like this in the Advanced Editor.

let
List = {1..10}
in
List

With this short example you can see the normal layout of Power Query code. We have the ‘let’-part were we put all our logic and we have the ‘in’-part to choose our output. This layout remains the same for more complex solutions. If I expand this first example, you will see what I mean. To see what happens on every line, just change the output at the bottom to the name of the step you want to see. I can already tell you that ‘TableFromRecords’ will return an error line, since not all elements of the record have a value. Although the result is similar, this is a different behavior than ‘TableFromList’ combined with ‘ExpandColumn’. Always check which solution is best for your case.

let
List = {1..10},
PersonRecord = [Name = “Ken” , Age = 36],
RecordWithList = [x = 1, y = {1..5}],
ListOfRecords = {[Name = “Ken” , Age = 36], [Name = “Nicole”]},
TableFromList = Table.FromList(ListOfRecords, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
ExpandColumn = Table.ExpandRecordColumn(TableFromList, “Column1”, {“Name”, “Age”}, {“Column1.Name”, “Column1.Age”}),
TableFromRecords = Table.FromRecords(ListOfRecords),
TableFromRecords2 = Table.FromRecords({[Name = “Ken” , Age = 36], [Name = “Nicole”, Age = null]}),
TableFromScratch = #table({“Name”, “Age”}, {{“Ken”, 36}, {“Nicole”, null}}),
ListValue5 = List {5},
ListOfRecordsValue0 = ListOfRecords{0}
in
List

 

Structured Types

The last type you can find in Power Query is a Structured Type. We have three different types: List Type, Record Type and Table Type. With Structured Types it is possible to split the definition of a List/Record/Table from the creation of the actual List/Record/Table. It was very hard to find information about them and how you could use it. Best way to show you what they are is give you an example. In the example we use all three types, create a table and then open record and list to show the content.

let
NameList = type {text},
PersonRecord = type [name = text, age = number, children = NameList ],
PersonTable = type table [Speaker = PersonRecord],
TableCreation = #table ( PersonTable, {{ [name = “Ken”, age = 37, children = {“Tess”, “Shrimp”}] }}) ,
ExpandedSpeaker = Table.ExpandRecordColumn(TableCreation, “Speaker”, {“name”, “age”, “children”}, {“Speaker.name”, “Speaker.age”, “Speaker.children”}),
ExpandedSpeakerChildren = Table.ExpandListColumn(ExpandedSpeaker, “Speaker.children”)
in
ExpandedSpeakerChildren

This is it for this blogpost. If you have any questions, just shoot!

Group of computer programmers working in the office. Focus is on blond woman showing something to her colleague on PC.
Updating your Azure SQL server OAuth2 credentials in Power BI via PowerShell for automation purposes
The better way to update OAuth2 credentials in Power BI is by automating the process of updating Azure SQL Server...
2401-under-memory-pressure-featured-image
Under (memory) pressure
A few weeks ago, a client asked me if they were experiencing memory pressure and how they could monitor it...
2402-fabric-lakehouse-featured-image
Managing files from other devices in a Fabric Lakehouse using the Python Azure SDK
In this blogpost, you’ll see how to manage files in OneLake programmatically using the Python Azure SDK. Very little coding...
2319-blog-database-specific-security-featured-image
Database specific security in SQL Server
There are many different ways to secure your database. In this blog post we will give most of them a...
kohera-2312-blog-sql-server-level-security-featured-image
SQL Server security made easy on the server level
In this blog, we’re going to look at the options we have for server level security. In SQL Server we...
blog-security_1
Microsoft SQL Server history
Since its inception in 1989, Microsoft SQL Server is a critical component of many organizations' data infrastructure. As data has...