Sub topic 10
Why Files Must Share a Structure
This chapter assumes you can connect to a folder, filter the file list, and run Combine & Transform Data. It explains why the combined query depends on every file having the same layout, and what breaks when one doesn't.
How the combine step uses the sample file
Power Query does not inspect each file separately. It builds the transformation steps once, against the sample file, and then applies those steps to every file through the generated function query. Those steps are recorded by position and by name. For example, "promote the first row to headers" and "change the type of the column Amount" assume that the first row holds headers and that a column called Amount exists.
When a file does not match, the same steps run against a different layout. The result is an error, a missing column, or data in the wrong place.
What counts as the same structure
Files share a structure when these things match:
- The header names, spelled and capitalized identically.
- The data types each column holds.
- The sheet or table name, for Excel files.
- The delimiter and encoding, for CSV files.
Column order matters less. The combine step appends tables by column name, so a file with the same columns in a different order still lines up. Column names are the part that must match.
Common mismatches and their symptoms
| Difference in a file | What you see |
|---|---|
| A header is renamed, such as `Amount` to `Total` | The column `Amount` is null for that file, and `Total` appears as an extra column. |
| A column is missing | An error such as "The column 'Amount' of the table wasn't found" when a later step refers to it. |
| Extra title rows above the header | The wrong row is promoted to headers, so column names become data. |
| A different sheet name in Excel files | An error such as "The key didn't match any rows in the table". |
| Text in a numeric column | `DataFormat.Error` cells after the type change. |
A quick check for mismatched files
You can compare each file's columns before you combine. This query lists the column names of every CSV file so that you can spot the file that differs.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Columns = Table.AddColumn(CsvOnly, "Columns", each Text.Combine(Table.ColumnNames(Table.PromoteHeaders(Csv.Document([Content], [Delimiter = ",", Encoding = 65001]))), ", ")),
Result = Table.SelectColumns(Columns, {"Name", "Columns"})
in
ResultEach row shows one file name and its column list. A row whose list differs from the others points to the file to fix.
Fixing the problem
- Find the file that differs, using the query above or by opening the files.
- Correct the source file if it is wrong, such as a typo in a header.
- If the difference is legitimate, handle it in the Transform Sample File query. For example, rename the variant column with
Table.RenameColumnsand theMissingField.Ignoreoption so the step doesn't fail when the old name is absent. - Move files that do not belong out of the folder, or filter them out of the file list.
Exercise
Last month's CSV file has the header Total where the other files have Amount. After you refresh, no error appears, but the Amount column has blanks for that month. Why, and what is one fix?
Show answer
The steps from the sample file refer to the column by name. Because Total does not match Amount, the rows from that file get null in Amount, and Total is added as an extra column. Fix the header in the source file, or rename Total to Amount in the Transform Sample File query with Table.RenameColumns and MissingField.Ignore.