Sub topic 13
Writing your own combine query
This chapter assumes you can open the Power Query editor, you know what Combine & Transform Data builds (sample file, parameter, function), and you can read basic M. Here you replace the generated helper queries with a single query that you control.
When to write your own
The generated queries work well for uniform CSV files. A hand-written query is useful in three cases:
- You want one query instead of four helper queries.
- You need a specific sheet from many workbooks.
- You need logic the sample-file approach can't express, such as a per-file filter or different parsing rules.
The pattern
Every custom combine query follows the same four steps.
- Read the file list with
Folder.Files. - Filter it to the files you want.
- Add a column that parses each file's binary
Contentinto a table. - Combine the nested tables.
Combining CSV files
Open Data > Get Data > Launch Power Query Editor, create a blank query, and open the Advanced Editor. Paste this code:
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv"),
Parsed = Table.AddColumn(CsvOnly, "Data", each Table.PromoteHeaders(Csv.Document([Content], [Delimiter = ",", Encoding = 65001]))),
Kept = Table.SelectColumns(Parsed, {"Name", "Data"}),
Expanded = Table.ExpandTableColumn(Kept, "Data", {"Date", "Region", "Amount"}),
Typed = Table.TransformColumnTypes(Expanded, {{"Date", type date}, {"Region", type text}, {"Amount", type number}})
in
TypedEach step does one job:
Csv.Documentreads the binaryContentas CSV.Encoding = 65001means UTF-8.Table.PromoteHeadersmakes the first row the column names. It runs inside theeach, so it applies to every file separately.Table.SelectColumnskeepsName, so each row records its source file.Table.ExpandTableColumnunnests the tables. You must list the column names yourself.Table.TransformColumnTypessets types last, on the combined table.
Combining one sheet from many workbooks
For Excel files, parse with Excel.Workbook instead. It returns a table of the workbook's sheets, tables, and named ranges. You pick the one you need.
let
Source = Folder.Files("C:\Data\Budgets"),
Workbooks = Table.SelectRows(Source, each [Extension] = ".xlsx" and not Text.StartsWith([Name], "~$")),
Parsed = Table.AddColumn(Workbooks, "Data", each Excel.Workbook([Content], true, true){[Item = "Budget", Kind = "Sheet"]}[Data]),
Combined = Table.Combine(Parsed[Data])
in
CombinedThe second argument, true, uses the first row as headers. The {[Item = "Budget", Kind = "Sheet"]} lookup selects the sheet named Budget. The [Data] part returns that sheet's table. Table.Combine then appends the tables, matching columns by name.
Two details matter here:
- The filter excludes
~$files. Excel creates these temporary files while a workbook is open, and they are not valid workbooks. - This version drops the file name, because
Table.Combine(Parsed[Data])uses only the nested tables. To keep it, replace the last step withTable.ExpandTableColumnas in the CSV example.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Column not found in the expand step | A file has a different header spelling |
| Garbled characters | Wrong `Encoding` value |
| Error on a `~$` file | Temporary file not filtered out |
| Rows from unexpected files | Subfolders are included by `Folder.Files` |
Exercise
In the Excel example, why is the filter step placed before the Table.AddColumn step, and what would go wrong if you removed the ~$ test?
Show answer
Filtering first means only valid workbooks are parsed. A ~$ temporary file is not a real workbook, so Excel.Workbook fails on it, and that error propagates through the Table.Combine step and breaks the refresh.