Power Query Beyond the Ribbon: Types

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.

151027b

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.

151027c

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!

Want to run your own advanced analyses, reports and dashboards using Microsoft Excel?