Sub topic 7
Cleaning the sample file once for all files
This chapter assumes you can already connect to a folder, filter the file list, and run Combine & Transform Data. It also assumes you have seen the helper queries Excel generates. Here you will learn where per-file cleanup belongs, how to edit it, and which edits cause trouble when files differ.
The idea: one cleanup, many files
When you combine files, Excel does not clean each file separately by hand. It builds a function from the steps in one query, Transform Sample File, and runs that function on the binary Content of every file in the list.
You therefore edit one file's worth of steps, and the same steps run for every file. Excel builds the sample from the first file in the list, but the steps are the real logic.
Where each kind of step belongs
Two places can hold cleanup steps, and they behave differently.
- Transform Sample File runs once per file, on that file's rows only.
- The combined query (named after the folder) runs once, on all rows from all files after they are appended.
| Kind of step | Put it in | Reason |
|---|---|---|
| Skip title rows above the header | Transform Sample File | Each file has its own title rows. |
| Promote headers | Transform Sample File | Each file has its own header row. |
| Remove footer or total rows | Transform Sample File | Each file has its own footer. |
| Trim whitespace, fix text case | Either | The result is the same. The sample file is slightly faster. |
| Drop columns you never need | Transform Sample File | Smaller tables are appended. |
| Set data types | Either (the next chapters cover this) | Setting types in the combined query is safer when files differ. |
| Remove duplicate rows across files | Combined query | Only the combined table sees all files. |
| Add a running total or rank | Combined query | It needs rows from every file. |
A common mistake is to clean the combined query and then wonder why a title row from every file survives. After the combine, those rows are scattered through the table, with no reliable way to tell them apart. Remove them at the source.
Editing Transform Sample File
- In the Power Query editor, find the group of helper queries in the Queries pane (for example Transform File from Sales).
- Select Transform Sample File. The preview shows the first file's data after the generated steps.
- Add, change, or delete steps with the ribbon, as you would in any query.
- Select the combined query (the one named after the folder) and check the preview. It should now reflect your change for all files.
You will not see a sample-specific step list in the combined query. Its steps are mostly Source, the call to the function, the expansion, and the type changes. The cleanup lives in the helper.
A worked example
Suppose each monthly CSV starts with two title lines, then the header row, then data rows. Some rows are blank, and the Region column has stray spaces. After your edits, Transform Sample File could look like this:
let
Source = Csv.Document(Parameter1, [Delimiter = ",", Columns = 5, Encoding = 65001, QuoteStyle = QuoteStyle.None]),
SkippedTitle = Table.Skip(Source, 2),
Promoted = Table.PromoteHeaders(SkippedTitle, [PromoteAllScalars = true]),
Trimmed = Table.TransformColumns(Promoted, {{"Region", Text.Trim, type text}}),
NoBlank = Table.SelectRows(Trimmed, each [Date] <> null and [Date] <> ""),
Kept = Table.SelectColumns(NoBlank, {"Date", "Region", "Amount"}, MissingField.UseNull)
in
KeptHere is what each step does:
Sourceparses the binary passed in asParameter1. The function supplies a different file'sContenton each call.SkippedTitledrops the first two rows (Table.Skip), andPromotedturns the next row into headers.Trimmedremoves leading and trailing spaces from Region.NoBlankkeeps rows that have a date.Keptselects the three columns you need.MissingField.UseNullfills a column with nulls instead of raising an error if a file lacks it.
The Columns = 5 argument in Source is what Excel wrote because the sample file had five columns. It fixes the expected column count. If your files can have different widths, check this value and consider removing it.
Choosing a representative sample
Excel builds the sample from the first row of the file list, after any filter you applied. Two problems follow.
- If the first file is atypical (an empty file, a test file, a file with an extra column), your steps are built around the wrong layout, and the preview in Transform Sample File looks wrong.
- If the first file changes later (a new file sorts first, or you delete the old one), the sample silently changes too, because
Sample Filealways points to the first item.
To pin the sample to a specific file, open the Sample File query and look at its navigation step. By default it takes Source{0}[Content], the first row. Replace it with a lookup by name:
let
Source = Folder.Files("C:\Data\Sales"),
Navigation1 = Source{[Name = "sales_2026-01.csv", #"Folder Path" = "C:\Data\Sales\"]}[Content]
in
Navigation1Using both Name and Folder Path identifies the file uniquely, because files with the same name can exist in different subfolders. The record keys must match the file exactly, including the trailing backslash in the folder path. If that file is later removed, the query fails with an error until you point it at another file. Weigh that against a first-row sample that is always present but may change.
Edits that break other files
The sample file is only one file. A step that works there can fail on others. Watch for these.
- Removing a column by name.
Table.RemoveColumnsraises an error if a file lacks that column. UseTable.SelectColumnswithMissingField.UseNull, as in the example, or passMissingField.Ignore. - Removing columns by position. If the column order differs between files, you will remove the wrong data without any error. Prefer names.
- Renaming a column that is not in every file. The rename step fails on files without it.
- Filtering by a value picked from the sample preview. If you tick a few values in the filter dropdown, Excel writes a rule matching only those values. Other files with different values lose rows. Write the condition you mean, such as
[Amount] <> null. - Hard-coded row counts.
Table.Skip(Source, 2)assumes every file has exactly two title lines. If one file has three, its header row becomes data and its columns are mislabeled. - Pivot or unpivot with fixed column lists. New columns in later files will not be handled.
Checking the result
After each edit, work through this short list:
- Open the combined query and confirm the column list matches what you intended.
- Filter the file-name column (covered in the next chapter) to a file other than the sample, and check that its rows look right.
- Look for error cells and for an unexpected row count. Column profiling (View > Column quality) shows error and empty percentages for each column.
- Close and load only after the preview is correct.
Changing the helper query does not require rebuilding anything. The next refresh applies the new steps to every file.
Exercise
Each monthly CSV in your folder has a three-line report header above the column headers and a "Total" row at the bottom. After using Combine & Transform Data, the combined table contains these lines from every file. Which query do you edit to fix this, which steps do you add, and why would the same fix not work if you applied it to the combined query?
Show answer
Edit Transform Sample File. Add a step that skips the first three rows (Table.Skip(Source, 3)), then a Table.PromoteHeaders step, then a filter that removes the Total row (for example, keep rows where [Date] is not null, or where the first column is not "Total"). These steps run once per file, so the title lines and total of every file are removed before the files are appended. In the combined query the title lines and totals from all files are mixed into one table, so there is no single row position to skip, and the headers would be duplicated in the data.