Skip to content

Pivoting Text Data

This page shows how to pivot data whose values are text, so that each record ends up on a row of its own.

Data often has to be reshaped before it can be analysed. Here the table on the left must become the table on the right.

In the left-hand table, column A holds the field name and column B the value, and each block of four rows makes up one record. Older systems often export data like this, and the format usually cannot be changed at the source.

On the left, a two-column table of stacked employee records, with each block of four rows giving Employee, Employee Number, Start Date and Salary, two blocks outlined in red; on the right, the same data with one row per employee

Selecting Column 1 in Power Query and clicking Pivot Column, with the default settings, gives this:

A single row with columns Employee, Employee Number, Start Date and Salary, each holding the value 6

Pivot Column produces one row for each distinct combination of the remaining columns. Once Column 1 and Column 2 are used up there are no columns left to tell one record from another, so every value for each field falls into the same cell, and the default aggregation counts them, giving 6 for each field.

Each record is four consecutive rows, so the fix is to give every block of four rows its own ID number. The pivot then has a column that separates one record from the next.

  • Click anywhere in the table, then on Excel’s Data tab click From Table/Range.
  • In the Power Query editor, go to the Add Column tab, click Index Column, and choose From 0.

The query with Column 1, Column 2 and a new Index column numbered from 0 to 23

  • Select the Index column, then on the Transform tab click Standard, and choose Integer-Divide.

The Transform tab’s Standard menu open, with Integer-Divide highlighted among Add, Multiply, Subtract, Divide, Modulo, Percentage and Percent Of

  • Enter 4, and click OK.

The Integer-Divide dialog with 4 entered as the value

Integer division drops the remainder, so indexes 0 to 3 become 0, indexes 4 to 7 become 1, and so on. Each record’s four rows now share one number:

The query with the Index column reading 0 for the first four rows, 1 for the next four, and up to 5 for the last four

  • Select Column 1, then click Pivot Column on the Transform tab.
  • Choose Column 2 as the Values Column.
  • Under Advanced options, set Aggregate Value Function to Don’t Aggregate.

The Pivot Column dialog with Column 2 as the Values Column and Don’t Aggregate chosen under Advanced options

The data now pivots correctly, with one row per index number:

The pivoted query with columns Index, Employee, Employee Number, Start Date and Salary, and six rows

  • Remove the Index column, which is no longer needed.
  • Close & Load the query to Excel.

After some formatting, the table has the layout that was wanted:

The loaded Excel table with Employee, Employee Number, Start Date and Salary columns, with dates and salaries formatted

This example follows Matt Allington’s post Pivoting Text Data Using Power Query on Excelerator BI.