Mismatched Tables
The problem
Section titled “The problem”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:

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.
Step 1: build a conversion table
Section titled “Step 1: build a conversion table”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.

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.
- Select a cell in the table, then choose From Table/Range on the Data tab.
- Name the query
Conversion_Table. - Choose Close & Load To, then Only Create Connection.

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:
- On the Data tab, choose Get Data, then From File, then From Folder, and select the folder.
- Power Query lists the files it found. Open the Combine drop-down and choose Combine & Transform Data.

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

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.
- In the Queries pane, select
Conversion_Table. - On the Transform tab, choose Transpose. The
SourceandTargetcolumns become two rows. - On the Home tab, choose Use First Row as Headers. The source names become the column headers.
- 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.

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:
Table.Column(Conversion_Table, _)looks for a column of that name in the transposed conversion table and returns its values as a list.{0}takes the first item in that list, which is the target name.- If the header is not in the conversion table, the lookup raises an error, so
try … otherwise _catches it and keeps the original header.

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.

Keeping it working
Section titled “Keeping it working”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.