Sub topic 7
Cleaning the sample file once for all files
This chapter assumes you can open the Power Query editor, connect to a folder, and have run Combine & Transform Data at least once. You should also know what a query and an applied step are.
Why you edit one file and fix them all
When Excel combines a folder, it builds a helper query named Transform Sample File. This query shows the first file in your list. Excel also builds a function query, usually named Transform File, that applies the sample file's steps to every file. The combined query calls that function once per file.
Because of this link, you clean one file and the same steps run on all of them. You do not edit the combined query to fix a per-file problem. Edit the sample.
Editing the sample file
- In the Power Query editor, open the Queries pane and find the group named Transform Sample File (or Helper Queries).
- Select the Transform Sample File query.
- Review the Applied Steps list on the right. You will see steps such as Source, Promoted Headers, and Changed Type.
- Add or change steps with the ribbon, as you would in any query.
- Select the combined query (named after your folder) and check the result.
Useful edits to make here:
- Remove columns you do not need.
- Filter out blank rows or repeated header rows.
- Trim spaces with Transform > Format > Trim.
- Rename columns that differ slightly between files.
- Replace error values or placeholders such as
N/A.
What a good edit looks like in M
After you remove a column and filter blanks, the sample query might contain steps like these. The first line of the function body in your workbook will differ, because Excel generates it from your files.
let
Source = Csv.Document(Parameter1, [Delimiter = ",", Encoding = 65001]),
Promoted = Table.PromoteHeaders(Source),
Removed = Table.RemoveColumns(Promoted, {"Notes"}),
NoBlanks = Table.SelectRows(Removed, each [Amount] <> null and [Amount] <> "")
in
NoBlanksHere Parameter1 is the generated parameter query that supplies the sample file's binary content. When the function runs, Power Query swaps in each file's content instead.
Common mistakes
- Editing the combined query instead of the sample. A step added there runs once on the stacked table. It does not run per file, so it cannot fix a problem such as a repeated header row inside each file.
- Hardcoding a value from the first file. A step that filters on a single date from the sample file will behave wrongly on other files.
- Removing a column by name that is missing elsewhere. If one file lacks that column, the refresh raises an error. Use
MissingField.IgnoreinTable.RemoveColumnsto tolerate this. - Setting types before cleaning. Convert types after you remove placeholder text, or the conversion produces errors.
Exercise
You find that every CSV in the folder ends with a "Total" row that you want to remove. Which query do you edit, and why?
Show answer
Edit Transform Sample File. Its steps run once per file through the Transform File function, so a step that removes the last row applies to each file's own total row. A step added to the combined query would remove only the last row of the whole stacked table.