Skip to content

Pivoting Text Data (advanced)

This page shows how to pivot text data that follows no pattern, by writing a small function in M.

The data below records vehicle hire: each row gives a date, a type of vehicle and the person who hired it. Unlike the stacked records on the Pivoting text data page, there is no fixed pattern, such as every three rows making a record. The number of hires per date, and the mix of vehicles, varies at random.

An Excel table with columns Date, Vehicle and Person, listing hires of vans, cars and trucks on dates from 10/01/2018

The aim is to pivot the data so that each date shows who hired a van, who hired a car and who hired a truck, like this:

A table with columns Date, Van, Car and Truck, where a date with several hires takes several rows and unused cells are blank

Load the table into Power Query and name the query FlatTable. It looks the same as it does in Excel:

The FlatTable query in the Power Query editor, with Date, Vehicle and Person columns

2. Set up the values that Table.Pivot needs

Section titled “2. Set up the values that Table.Pivot needs”

Create a new blank query, open the Advanced Editor, and enter this code:

let
Source = FlatTable,
ColumnForValues = "Person",
ColumnToPivot = "Vehicle",
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColumnToPivot)))
in
PivotColNames

The code sets up the values that the Table.Pivot function will need:

  • ColumnForValues is the name of the column that holds the values to show once the data is pivoted, here Person.
  • ColumnToPivot is the name of the column whose values become the new column headings, here Vehicle.
  • PivotColNames is a list of the distinct values in that column, which are the headings to create: Van, Car and Truck.

Add a line that calls Table.Pivot, so that the code reads:

let
Source = FlatTable,
ColumnForValues = "Person",
ColumnToPivot = "Vehicle",
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColumnToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColumnToPivot, ColumnForValues, (_) => _)
in
#"Pivoted Column"

The fifth argument of Table.Pivot is the function that combines the values falling into each cell. Here it is (_) => _, which returns its input unchanged, so each cell holds a list of all its values. Selecting a cell shows the list behind it, such as the three people who hired a car on 11/01/2018.

That list is the difficulty with pivoting text. Numbers in the same cell can be added up into one value, but several names cannot be combined that way.

The pivoted query with Date, Van, Car and Truck columns, each cell holding a List, and a preview of one Car list containing three names

4. Write a function to turn each row into a table

Section titled “4. Write a function to turn each row into a table”

The next step defines a function, TableFromRecordOfLists, inside the query. It takes one row of the table as a record, called rec, and the list of column headings to use, called fieldnames, and works through these steps:

  1. Record.SelectFields keeps only the fields named in fieldnames.
  2. Record.ToList turns those fields into a list of lists.
  3. Table.FromColumns turns the lists into the columns of a table, with fieldnames as its headings.

Turning the lists into a table is what lets them expand into rows later. With the function added, the code reads:

let
Source = FlatTable,
ColumnForValues = "Person",
ColumnToPivot = "Vehicle",
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColumnToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColumnToPivot, ColumnForValues, (_) => _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table
in
TableFromRecordOfLists

This version returns the function itself, which shows that it is defined correctly. Before carrying on, change the line after in back to #"Pivoted Column", so that the query returns the pivoted table again.

Then call the function from a custom column named Values, with this formula:

The Custom Column dialog creating Values with the formula TableFromRecordOfLists(_,PivotColNames)

The new column holds a Table object in each row:

The query with Date, Van, Car and Truck list columns and a new Values column of tables, with a preview showing Van, Car and Truck columns where blank places are null

Each table puts that date’s three lists side by side as columns, filling the shorter lists with null, which is the shape needed.

  • Select the Date and Values columns, right-click, and choose Remove Other Columns.

The query with only Date and Values columns

  • Click the expand icon on the Values column and expand all its fields.

The expand menu on the Values column with Van, Car and Truck ticked

The data is now pivoted, with one row for each hire slot on each date:

The expanded query with Date, Van, Car and Truck columns, where 11/01/2018 takes three rows and the empty slots show null

  • Click Close & Load to return the pivoted data to Excel.

The finished query is below. It could itself be turned into a general function that works on any table of this shape.

let
Source = FlatTable,
ColumnForValues = "Person",
ColumnToPivot = "Vehicle",
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColumnToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColumnToPivot, ColumnForValues, (_) => _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"