Skip to content

Mismatched Tables

Tables that hold the same kind of data rarely agree on what to call their columns. Each file has an owner, and each owner names things in their own way, so a product’s cost might be StandardCost in one file, Standard_Cost in the next and Prod_cost in a third.

When Power Query appends tables, it lines up columns by name. A column called Prod_cost and one called StandardCost are therefore treated as two different columns, and the combined table ends up with gaps and duplicates instead of one clean list.

The example here uses four product files in one folder, one each for accessories, bikes, clothing and components. They hold the same fields, but the headers differ from file to file:

A table listing the four product files down the side and each file’s column headers across, which shows the same field named Product, Product_name or Name, and StandardCost, Standard_Cost or Prod_cost, depending on the file

Renaming the columns by hand in each file would work once, but the next file to arrive with a new spelling would break the query again. A better approach is to keep the renaming rules in a table of their own, which Power Query reads every time it refreshes.

A conversion table has two columns. Source holds a header as it appears in a file, and Target holds the name it should become. Several source names can point at the same target.

Only the headers that need renaming go in the table. A header that is already correct, such as Color or Size in most of these files, can be left out, because the renaming step leaves any name it cannot find unchanged.

A two-column Excel table with Source and Target headers, which maps variants such as Product_name, ID and Prod_num onto Name and Product Number, and StandardCost, Standard_Cost and Prod_cost onto Standard Cost

Step 2: load the conversion table as a connection

Section titled “Step 2: load the conversion table as a connection”

The conversion table needs to be available to other queries, but it does not need to appear on a worksheet again.

  1. Select a cell in the table, then choose From Table/Range on the Data tab.
  2. Name the query Conversion_Table.
  3. Choose Close & Load To, then Only Create Connection.

The Import Data dialog with Only Create Connection selected, so the query is saved without loading its data to a worksheet

Step 3: combine the product files from their folder

Section titled “Step 3: combine the product files from their folder”

In the same workbook, point Power Query at the folder that holds the product files:

  1. On the Data tab, choose Get Data, then From File, then From Folder, and select the folder.
  2. Power Query lists the files it found. Open the Combine drop-down and choose Combine & Transform Data.

The folder preview listing Accessories.xlsx, Bikes.xlsx, Clothing.xlsx and Components.xlsx, with the Combine drop-down open on Combine & Transform Data

  1. In the Combine Files dialog, select Sheet1, which is the sheet each file keeps its data on, and click OK.

The Combine Files dialog with Sheet1 selected under the sample file, showing a preview of the accessories data with its Product, ID, Color and StandardCost columns

Power Query opens the editor with the combined query and a set of helper queries. One of these, Transform Sample File, holds the steps that are applied to every file before the files are appended. That is where the renaming will go.

Step 4: turn the conversion table on its side

Section titled “Step 4: turn the conversion table on its side”

The renaming step in the next section looks up each header as a column name, so the conversion table has to be reshaped first, with the source names across the top and the target names in the single row beneath.

  1. In the Queries pane, select Conversion_Table.
  2. On the Transform tab, choose Transpose. The Source and Target columns become two rows.
  3. On the Home tab, choose Use First Row as Headers. The source names become the column headers.
  4. Delete any Changed Type step that Power Query adds. That step names every column, so it would fail as soon as a new row was added to the conversion table.

The transposed conversion table, with Product, Product_name, ID, Product_Number and Prod_num as column headers and Name or Product Number in the one row beneath; Applied Steps lists Source, Transposed Table and Promoted Headers

Step 5: rename the headers in the sample file query

Section titled “Step 5: rename the headers in the sample file query”

Select the Transform Sample File query, click the fx button beside the formula bar to add a step, and enter this formula:

= Table.TransformColumnNames(#"Promoted Headers", each try Table.Column(Conversion_Table, _){0} otherwise _)

Table.TransformColumnNames runs the function after each once for every column header, where _ stands for the header being renamed. For each header, the function does the following:

  1. Table.Column(Conversion_Table, _) looks for a column of that name in the transposed conversion table and returns its values as a list.
  2. {0} takes the first item in that list, which is the target name.
  3. If the header is not in the conversion table, the lookup raises an error, so try … otherwise _ catches it and keeps the original header.

The Transform Sample File query after the new Custom1 step, with the accessories columns now headed Name, Product Number, Color, Standard Cost and List Price

Because every file passes through this query before the files are appended, every file now arrives with the same headers. Choose Close & Load to send the result to a worksheet, where the four files appear as one table with a Source.Name column showing which file each row came from.

The combined table on a worksheet, with rows from Accessories.xlsx followed by rows from Bikes.xlsx, all under the same Name, Product Number, Standard Cost, List Price and Category Name headers

The same query copes with any number of files, however differently their headers are spelt, as long as each variant appears in the conversion table. When a file arrives with a new spelling, add a row to the conversion table and refresh; the query itself does not change.

The one thing to watch is a header that is missing from the conversion table. The query does not fail in that case, because it keeps the unknown name, so the result gains an extra, mostly empty column instead. A quick look at the column list after each refresh is enough to catch it.