Sub topic 2
What Excel Creates for You: Sample File, Parameter, and Function
Assume you already know Excel tables, the Data ribbon, and basic CSV or .xlsx files. You also need Excel 2016 or Microsoft 365 with Power Query.
The helper queries
When you choose Combine & Transform Data for a folder, Excel builds several queries:
| Query | Purpose |
|---|---|
| Sample File | Holds one file used to define the transformation steps |
| Parameter | Stores the selected sample file |
| Transform File | A function that applies those steps to each file |
| Combined query | Filters files, calls the function, and appends the results |
A function is reusable M code that accepts an input and returns an output. Here, the input is one file's binary Content, and the output is a table.
(file as binary) as table =>
let
Source = Csv.Document(
file,
[Delimiter = ",", Encoding = 65001, QuoteStyle = QuoteStyle.Csv]
),
Headers = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
Typed = Table.TransformColumnTypes(
Headers,
{{"Date", type date}, {"Region", type text}, {"Amount", type number}}
)
in
TypedThe combined query usually keeps the folder's metadata, invokes this function for every selected file, and expands the returned tables.
Clean the sample file once
Open Transform Sample File and perform shared cleanup there. For example, remove an export title row, promote headers, rename columns, or set types. Excel applies those steps through the function, so every file receives the same transformation.
Keep the source file name
The combined query normally retains the Name column from the folder listing. Keep it before expanding the transformed table. This column gives each row its provenance, meaning the source from which the row came.
If you remove Name, add it back before expansion or create a custom column that passes the name into the result. File names help trace errors and audit monthly imports.
Set types after combining
Set Date to type date, numeric measures such as Amount to type number, and labels such as Region to type text. Apply types in the sample-file transformation when every file uses the same columns. If a type depends on the combined result, apply it after expansion.
Exercise
Where should you remove a leading title row that appears in every input file?
Show answer
Remove it in Transform Sample File so the generated function applies the step to every file.