Sub topic 6
What Excel creates for you: sample file, parameter and function
This chapter assumes you can open the Power Query editor, connect to a folder with From Folder, and read the file list (Content, Name, Extension, Date modified, Folder Path). Here you will look at the extra queries Excel adds when you choose Combine & Transform Data.
What happens when you click Combine & Transform Data
Select Data > Get Data > From File > From Folder, pick a folder, and click Combine & Transform Data. In the dialog, choose a sample file and click OK. Excel then adds several queries and groups them in the Queries pane.
You get four pieces:
| Query | Type | Purpose |
|---|---|---|
| Sample File | Parameter-driven query | Holds the one file Excel uses as the model |
| Parameter1 | Parameter | Stores the sample file's binary content |
| Transform Sample File | Query | Cleans the sample file |
| Transform File | Function | Applies the same cleaning to any file |
| Your folder query (for example, Sales) | Query | Calls the function for each row and returns the combined table |
The names vary by Excel version. Query names such as Transform Sample File and Transform File are the usual ones.
How the pieces connect
The function is the center of the design. It takes one file's binary content and returns a table. The folder query calls it once for every row in the file list.
The chain works like this:
- Sample File picks the first file in the list (or the one you chose) and returns its binary content.
- Parameter1 holds that value. The function uses it as its input argument.
- Transform Sample File applies your cleaning steps to the parameter's file. It is a normal query, so you can click it and see the result.
- Transform File is the function. It is built from Transform Sample File, with the parameter as the input.
- The folder query adds a column that calls Transform File on each file's Content, then expands the results.
Why this design matters
The Transform Sample File query is the only place you need to edit. Because the function is derived from it, any step you add or change there applies to every file the next time the query runs. Editing the folder query alone only changes the combined table, not the way each file is parsed.
The generated queries are also linked. If you delete Parameter1 or Sample File, the function stops working, and the folder query shows an error. Delete the whole group together if you want to start over.
Seeing the link in the formula bar
Select the folder query and look at the step that adds the function call. In the formula bar it looks like this:
= Table.AddColumn(#"Filtered Rows", "Transform File", each #"Transform File"([Content]))Each row's [Content] is passed to #"Transform File", and the result is a nested table stored in the new column. Later steps expand that column into regular columns. The next chapters cover editing the sample query and reading the full M code.
Exercise
You add a step that removes the first two rows in the folder query, after the expand step. A colleague says you should have edited a different query so the change applies to each file before they are combined. Which query should be edited, and why?
Show answer
Edit Transform Sample File. The Transform File function is built from it, so a step added there runs on every file as it is parsed. A step added in the folder query only affects the combined table, and it removes the first two rows of the whole result rather than of each file.