Skip to content

Transforming Data into Tables

A summary report is laid out for a person to read. It groups rows under headings, spreads values across columns and adds totals, so a reader can take in the picture at a glance. The same layout is awkward to analyse, because the facts are split between row headings, column headings and the cells where they cross. Filtering by one company, charting one category or adding a new period all mean working against the layout.

A table that is ready for analysis has a simpler shape:

  • each column holds one kind of fact, such as a category, a person or an amount
  • each row holds one observation, which here is one revenue figure
  • there are no totals, because a PivotTable or a measure can work those out

This page reshapes a revenue report into that form. The report shows revenue by salesperson and client company down the side, and by parent category and category across the top:

An Excel report titled Sales by Sales Person, Client Company Parent Category and Category, with salespeople and companies down the left, parent categories Accessories and Bikes over their categories across the top, revenue in the cells and a Grand Total row at the bottom

The difficulty is that both axes have two levels of headings. The approach is to collapse each axis to a single column, unpivot, and then split the collapsed values apart again.

  1. On the Data tab, choose Get Data, then From File, then From Workbook, and select Data with incorrect design.xlsx.
  2. Load the workbook into the editor. It lists the sheet and a defined name, Revenues, which covers the report’s range.

The workbook in the editor with two rows, Sheet1 of kind Sheet and Revenues of kind DefinedName

  1. Click Table in the Data column of the Revenues row to open the report’s data.

The report in the editor as Column1 to Column6, where row 1 holds Accessories, row 2 holds the headings SalesPerson, CompanyName, Bike Racks, Bottles and Cages, Cleaners and Helmets, and row 17 is a jae0 Total subtotal

The editor shows something the formatted report hides: each salesperson’s block ends with a subtotal row, such as jae0 Total. These rows need removing later, along with the category subtotals that appear once the table is turned round.

The last column is the grand total. Rather than remove it by name, add a step that removes whichever column is last:

= Table.RemoveColumns(Revenues_DefinedName,{List.Last(Table.ColumnNames(Revenues_DefinedName))})

Table.ColumnNames lists the columns in order, List.Last picks the final one, and Table.RemoveColumns drops it. If a category is added to the report next month, the total column moves along one place, and the step still removes the right one.

The report after the grand total column has been removed, with the formula in the formula bar and columns from Column1 onwards

The grand total row is the last row. On the Home tab, choose Remove Rows, then Remove Bottom Rows, and remove 1 row.

The query after the Removed Bottom Rows step, whose formula is Table.RemoveLastN(Custom1,1)

Each salesperson’s name appears only on the first row of their block, so the rows beneath it are null. Select Column1, then choose Fill, then Down on the Transform tab. Every company row now carries its salesperson.

Column1 after Fill Down, with jae0 repeated on every company row down to the jae0 Total row

Step 5: merge the two row headings into one

Section titled “Step 5: merge the two row headings into one”

Select Column1 and Column2, then choose Merge Columns on the Transform tab, with Colon as the separator. The salesperson and company now sit in one column, as in jae0:Action Bicycle Specialists.

The Merged column, holding a lone colon on the first row, SalesPerson on the second, and values such as jae0 Bicycle Specialists below

On the Transform tab, choose Transpose. The column headings become rows, so the parent category and category now run down the first two columns, and each salesperson and company pair becomes a column.

The transposed table, with parent categories in Column1, categories in Column2 and values for jae0 Bicycle Specialists in Column3, where the parent category appears only on the first row of each group and rows such as Accessories Total appear after each group

The parent category now has the same gaps as the salesperson had before, so select Column1 and choose Fill, then Down again.

Column1 after Fill Down, with Accessories, Bikes and Clothing repeated on each of their rows, and a colon, SalesPerson and jae0 Bicycle Specialists on the first row

On the Home tab, choose Use First Row as Headers. The first two columns take the headers : and SalesPerson:CompanyName, and every other column is headed by a merged salesperson and company.

The table after promoting headers, with columns headed colon, SalesPerson, jae0 Bicycle Specialists, jae0 Discount Store and jae0 Bicycle Specialists

Select the first two columns, then open the Unpivot Columns drop-down on the Transform tab and choose Unpivot Other Columns. Each revenue figure becomes its own row, with the column it came from in Attribute and the figure in Value. Empty cells are dropped along the way, because Unpivot skips nulls.

The unpivoted table, with parent category, category, an Attribute column such as jae0 Bicycle Specialists or jae0 Total:, and a Value column

Unpivot Other Columns is the safer choice here, because it names only the columns to keep. New salespeople or companies in next month’s report are unpivoted without any change to the query.

Select Attribute, then choose Split Column, then By Delimiter on the Transform tab, and choose Colon as the delimiter. The salesperson and company return to separate columns.

The table after splitting, with Attribute.1 holding the salesperson, such as jae0 or jae0 Total, and Attribute.2 holding the company

Rename the columns in order:

  1. Parent Category
  2. Category
  3. Sales Person
  4. Company
  5. Revenue

Two kinds of subtotal are still in the data, and both need filtering out:

  • Salesperson subtotals have a Sales Person such as jae0 Total. Filter Sales Person with Text Filters, then Does Not Contain, and enter Total.
  • Category subtotals have a Parent Category such as Accessories Total. Apply the same filter to Parent Category.

The Filter Rows dialog keeping rows where Sales Person does not contain Total

Choose Close & Load. The report is now a flat table with one revenue figure per row, ready for a PivotTable or the Data Model.

The loaded table with Parent Category, Category, Sales Person, Company and Revenue columns, where the first header is misspelt Pareent Category in this capture, starting with Accessories, Bike Racks, jae0, Action Bicycle Specialists and 432.00

The same sequence works for any report with two levels of headings on each axis:

  1. Remove the grand total column and row.
  2. Fill down the outer row heading.
  3. Merge the row headings into one column.
  4. Transpose.
  5. Fill down the outer column heading.
  6. Use the first row as headers.
  7. Unpivot every column except the column headings.
  8. Split the merged column back into its parts.
  9. Rename the columns and filter out the subtotals.

The next page extends this to reports with more levels of headings.