Sub topic 10
Why files must share a structure
This chapter assumes you can open Power Query from Data > Get Data and that you have run Combine & Transform Data on a folder at least once. It also assumes you know the sample file, parameter and function queries that command creates. Here you learn why the combine step only works when the files look alike, which differences break it, and how to detect or tolerate them.
One recipe applied to every file
When you combine a folder, Power Query does not look at each file and decide what to do. It records the steps you apply to one sample file, turns them into a function, and calls that function once per row of the file list. Every file gets the same recipe.
That recipe was written against one file. It refers to that file's column names, its header row and its delimiter. A file that differs from the sample in any of those respects can fail, load wrong data, or load correct data into the wrong place.
What "same structure" means
Files do not need identical data. They need identical layout. Check these properties.
- Column names. M compares names exactly, including case and trailing spaces.
Amount,amountandAmountare three different columns. - Header position. The recipe usually promotes the first row to headers. A file with a title line above the headers gets that title as its header.
- Parsing settings. A CSV file needs the same delimiter and encoding as the sample. A semicolon-separated file parsed as comma-separated collapses into one column.
- Sheet or table name. For workbooks, the recipe selects an item such as a sheet called
Budget. A workbook without that item fails. - Data types. A column that holds numbers in one file and text such as
n/ain another produces cell errors when you apply a numeric type.
Column order matters less than people expect. The expand step works by name, so a file with Region and Date swapped still lines up. The risk is files whose headers differ, not files whose columns are reordered.
What actually happens when a file differs
The result depends on how the difference relates to the recipe. These outcomes are the ones you will meet most often.
| Difference in a file | Typical result | Visible? |
|---|---|---|
| A column the recipe uses is missing | Error such as "The column 'Amount' of the table wasn't found" | Yes, on the file's row |
| A column was renamed | Same error for the old name, and the new column is ignored | Yes |
| An extra column was added | The extra column is not in the expand list, so it is dropped | No |
| Title rows above the headers | Wrong headers, then missing-column errors | Usually |
| Different delimiter | One wide column, then missing-column errors | Usually |
| Text in a numeric column | `Error` values in those cells after the type step | Only in affected cells |
The silent case is the extra column. The generated expand step contains a fixed list of column names taken from the sample at the time you built the query. A new column in later files never appears in the output, and nothing warns you. If new columns matter, you have to edit the query.
Detecting structure problems before combining
Instead of waiting for an error, you can build a diagnostic query. It parses each CSV file, lists the expected columns it lacks, and returns only the files that fail the check.
let
Expected = {"Date", "Region", "Amount"},
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Parsed = Table.AddColumn(CsvOnly, "Data", each Table.PromoteHeaders(Csv.Document([Content], [Delimiter = ",", Encoding = 65001]))),
Found = Table.AddColumn(Parsed, "Found", each Table.ColumnNames([Data])),
Gaps = Table.AddColumn(Found, "Missing", each Text.Combine(List.Difference(Expected, [Found]), ", ")),
Bad = Table.SelectRows(Gaps, each [Missing] <> ""),
Report = Table.SelectColumns(Bad, {"Name", "Missing"})
in
ReportList.Difference(Expected, [Found]) returns the expected names that are absent from the file. Text.Combine turns that list into text so the result is readable in the grid. If Report is empty, every CSV file has all three columns. Otherwise it lists each offending file and what it lacks.
Load this query as a connection only, and keep it beside your main query. After a new batch of files arrives, refresh it first.
Making the combine tolerant
Sometimes you cannot control the source. A few files omit a column or arrive in a different order, and you want the load to continue. The tolerant version below selects the expected columns inside each file's own step. The MissingField.UseNull option fills a missing column with null instead of raising an error.
let
Expected = {"Date", "Region", "Amount"},
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Parsed = Table.AddColumn(CsvOnly, "Data", each Table.SelectColumns(Table.PromoteHeaders(Csv.Document([Content], [Delimiter = ",", Encoding = 65001])), Expected, MissingField.UseNull)),
Kept = Table.SelectColumns(Parsed, {"Name", "Data"}),
Expanded = Table.ExpandTableColumn(Kept, "Data", Expected),
Typed = Table.TransformColumnTypes(Expanded, {{"Date", type date}, {"Region", type text}, {"Amount", type number}})
in
TypedTwo things change compared with the generated approach. Column selection happens per file, so each file is reshaped to the same three columns before expanding. A missing column becomes nulls, so you can find the problem in the data instead of losing the whole refresh. The trade-off is that a file with a renamed Amount column now loads with every amount null, and no error tells you. Pair the tolerant query with the diagnostic query above, or add a check that counts null values.
Excel workbooks add one more variable
For .xlsx files, the structure also includes which sheets or tables exist. The recipe from the spine picks an item by name:
Excel.Workbook([Content], true, true){[Item = "Budget", Kind = "Sheet"]}[Data]If a workbook has no sheet called Budget, that lookup raises an error for that file. Tables are more stable than sheets. A named Excel table keeps its identity when rows are added, whereas a sheet's used range changes with stray cells, and values far below the data are read as extra rows. When you control the source workbooks, ask for each report to be an Excel table with a fixed name, and select Kind = "Table" instead.
A practical rule set
- Agree on column names with the people who produce the files, and write them down.
- Filter the file list before combining so temporary files such as
~$Budget.xlsxnever reach the recipe. - Build the sample from a typical file, not the smallest or the oldest.
- Run the diagnostic query after each new batch of files, before you trust the combined table.
- Apply data types as the last step, after every file has been reshaped to the same columns.
Exercise
A monthly sales folder has worked for a year. This month the combined table refreshes with no error, but the new Discount column that finance added to the March file is missing from the output, and March rows show correct Amount values. Why did Power Query not raise an error, and what would you change?
Show answer
The generated expand step contains a fixed list of column names taken from the sample file when the query was built. Discount is not in that list, so it is dropped silently, while the listed columns exist in March and load normally. To keep the column, edit the query so the expand step includes Discount (for example by adding it to the Expected list in a tolerant query), which fills it with null for older files that lack it. Also run a diagnostic query to flag files whose columns differ from the expected list.