Sub topic 6
What Excel creates for you: sample file, parameter and function
This chapter covers what happens after you click Combine & Transform Data on a folder. It assumes you can connect to a folder, read the file list (Content, Name, Extension, Date modified, Folder Path), and filter it. It also assumes you know the Power Query editor's Queries pane and Applied Steps list. Editing the sample query is covered in the next chapter. Here the goal is to recognize each generated object and know what depends on what.
What you get after combining
Before the command, your folder query is a table of file records. Each row has a Content column holding the file as binary data, which is raw bytes that Power Query has not yet parsed. Combining means parsing every binary and stacking the results. Excel does this with a pattern: a sample, a parameter, and a function that applies the sample's steps to each file.
After you confirm the Combine Files dialog, the Queries pane shows these items:
| Object | Kind | Purpose |
|---|---|---|
| `Sales` (named after the folder) | Query | The final combined table. This is the one you load. |
| `Sample File` | Query, binary value | Picks one file from the folder to use as the example. |
| `Parameter1` | Parameter, type Binary | A stand-in for "the file being processed". |
| `Transform Sample File` | Query, table | Parses and cleans the sample file, using `Parameter1`. |
| `Transform File` | Function | Takes a binary and returns a cleaned table. |
The last four are placed in a group named Helper Queries. Your main query stays outside the group. Exact names can vary slightly between Excel versions, and a second combine in the same workbook gets numbered names such as Parameter2.
How the pieces connect
Read the diagram as two separate flows. The left side is an editing aid. It gives you one real file to look at while you design the cleanup steps. The right side is what produces your data. The main query never reads the sample. It only calls the function.
The Sample File query
Sample File selects one file's binary content from the file list. The generated code is typically:
let
Source = Folder.Files("C:\Data\Sales"),
Navigation1 = Source{0}[Content]
in
Navigation1Source{0} is the first row of the list, and [Content] is that row's binary field. In the Combine Files dialog you can choose a different file from the Sample File dropdown. The default is the first file.
The first row is not special beyond being first. If the folder's first file is unrepresentative, for example a file with extra header lines, your design steps are built on a misleading example. Pick a typical file in the dialog instead.
The Parameter1 query
Parameter1 is a parameter of type Binary. Its current value is the sample file. A parameter is a query that holds a single value and is marked with metadata so the editor treats it as an input rather than a result.
Its purpose is to let Transform Sample File be written as if there were one concrete file, while the same steps can later become a function over any file. In the steps of Transform Sample File, you will see Parameter1 where a file path would normally appear.
The Transform Sample File query
This query parses the sample binary and applies cleanup. For a CSV folder it usually looks like this:
let
Source = Csv.Document(Parameter1, [Delimiter = ",", Columns = 3, Encoding = 65001, QuoteStyle = QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
#"Promoted Headers"Two details matter. Columns = 3 records how many columns Excel detected in the sample, and Encoding = 65001 means UTF-8. If other files use a different delimiter or encoding, they are parsed with the sample's settings, not their own.
For .xlsx folders, the dialog first asks you to choose a sheet or table from the sample workbook. The query then contains an Excel.Workbook(Parameter1, true, true) step followed by a navigation step to that item. Every other workbook is expected to contain an item with the same name and kind.
The Transform File function
Transform File is the function that does the real work for each file. It has one argument, Parameter1, declared as binary. Its body is the same sequence of steps as Transform Sample File:
(Parameter1 as binary) =>
let
Source = Csv.Document(Parameter1, [Delimiter = ",", Columns = 3, Encoding = 65001, QuoteStyle = QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
#"Promoted Headers"The function and the sample query are linked. When you edit steps in Transform Sample File, Excel updates the function body to match. This is why a change made once applies to every file. You normally should not edit the function by hand, because the next change in the sample query overwrites it.
What the main query does
The main query starts from the file list again and then adds steps. Excel generates names like these, though your step names may differ:
let
Source = Folder.Files("C:\Data\Sales"),
#"Filtered Hidden Files1" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true),
#"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
#"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
#"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File"}),
#"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", {"Date", "Region", "Amount"})
in
#"Expanded Table Column1"Here is what each step does:
- Filtered Hidden Files1 drops files whose Hidden attribute is true. It does not drop Office lock files such as
~$Jan.xlsxunless they are also hidden, so you still filter by name yourself. - Invoke Custom Function1 adds a column whose value in each row is the table returned by
Transform Filefor that row'sContent. - Renamed Columns1 renames
NametoSource.Name. This is the generated way of keeping the file name, covered in a later chapter. - Removed Other Columns1 keeps only the file name and the nested tables.
- Expanded Table Column1 turns each nested table into rows. The column list in this step is a fixed list of names taken from the sample.
Because the expand step uses fixed names, a file with a renamed column produces nulls or an error for that column. This is why the sample and the other files must share a structure.
Where your own filter belongs
You can add a filter step to the main query, either before or after the function call. Filtering before Invoke Custom Function1 is better, because the function then runs only on files you intend to keep. A filter on [Extension] placed after the call still parses every file first, including ones you will discard. Parsing a non-CSV file as CSV may produce an error row that survives into your result.
Common situations
- The sample file is later removed from the folder.
Sample Fileand the queries built on it show an error in the editor. The main query can still refresh, because it calls the function and does not read the sample. To edit again, pick a file that exists, for example by changingSource{0}to a row you know is present. - You combine a second folder. Excel creates another set of helper queries, with numbered names. The two sets are independent.
- You want fewer helper objects. Writing your own combine query, covered later in this tutorial, avoids all of them. The trade-off is that you lose the sample-based editing interface.
- The sample columns differ from the other files. The function still runs, but the fixed
Columnscount and the expand list come from the sample.
Exercise
A colleague deletes the file that Sample File pointed at, then asks why Transform Sample File shows an error while the main Sales query still refreshes. Explain both observations, and say how you would restore the ability to edit the cleanup steps.
Show answer
Sample File reads Source{0}[Content], or a file selected by name, from the folder. With that file gone, the sample query fails, and so do Parameter1 and Transform Sample File, which depend on it. The main query does not read the sample. It calls the Transform File function on each file that currently exists in the folder, so it still refreshes. To restore editing, change the Sample File query so it selects a file that exists, for example a representative file from the list, and then check that Transform Sample File previews data again.