Pivoting Text Data (advanced)
This page shows how to pivot text data that follows no pattern, by writing a small function in M.
Data with no repeating pattern
Section titled “Data with no repeating pattern”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.

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:

1. Load the data into Power Query
Section titled “1. Load the data into Power Query”Load the table into Power Query and name the query FlatTable. It looks the same as it does in Excel:

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 PivotColNamesThe code sets up the values that the Table.Pivot function will need:
ColumnForValuesis the name of the column that holds the values to show once the data is pivoted, here Person.ColumnToPivotis the name of the column whose values become the new column headings, here Vehicle.PivotColNamesis a list of the distinct values in that column, which are the headings to create: Van, Car and Truck.
3. Call Table.Pivot
Section titled “3. Call Table.Pivot”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.

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:
Record.SelectFieldskeeps only the fields named infieldnames.Record.ToListturns those fields into a list of lists.Table.FromColumnsturns the lists into the columns of a table, withfieldnamesas 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 TableinTableFromRecordOfListsThis 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 new column holds a Table object in each row:

Each table puts that date’s three lists side by side as columns, filling the shorter lists with null, which is the shape needed.
5. Remove columns and expand
Section titled “5. Remove columns and expand”- Select the Date and Values columns, right-click, and choose Remove Other Columns.

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

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

- Click Close & Load to return the pivoted data to Excel.
6. The complete M code
Section titled “6. The complete M code”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"