Power Query course · Lesson 11 of 17
Combine files from a folder
Stack every CSV or Excel file in a folder into one table, understand the helper queries, clean each file, keep the file name and avoid common traps.
Combining a folder of files is the feature that converts most people to Power Query. Every month a new file lands in a folder: a sales export, a branch's expense report, a bank statement. You build one query that reads the whole folder, and from then on adding next month's data means saving the file in the folder and clicking Refresh. No copying, no pasting, no forgotten month.
The idea is the same as Append queries: stacking tables, but Power Query discovers the pieces itself. This lesson explains what the Combine button generates, because understanding the helper queries is the difference between a folder query that keeps working and one that breaks the first time a file looks slightly different.
Preparing the folder
- One folder per kind of data, for example
C:\Reports\Sales\Monthly. Do not mix sales exports with notes, screenshots or a summary workbook. - All files should have the same structure: same columns, same header row, same sheet or table name for Excel files.
- Name files consistently, ideally with a sortable date:
sales_2026-01.csv,sales_2026-02.csv. The name can become a column. - If colleagues will refresh too, use a SharePoint or Teams document library and From SharePoint Folder instead of a path on your own disk.
Step by step
- Choose Data › Get Data › From File › From Folder.
- Browse to the folder and click Open (or paste the path and click OK).
- A preview lists the files with columns
Content,Name,Extension,Date accessed,Date modified,Date created,AttributesandFolder Path. Subfolders are included. - Click Combine › Combine & Transform Data.
- The Combine Files dialog opens. Sample File defaults to First file. For CSV files check File Origin and Delimiter; for Excel files pick the sheet or table to use from each file, for example the table
Sales. - Leave Skip files with errors unticked at first, so you notice problems. Click OK.
- The editor opens with a combined table and a new group of helper queries in the Queries pane.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Name | Extension | Date modified | Folder Path |
| 2 | sales_2026-01.csv | .csv | 2026-02-02 | C:\Reports\Sales\Monthly\ |
| 3 | sales_2026-02.csv | .csv | 2026-03-02 | C:\Reports\Sales\Monthly\ |
| 4 | sales_2026-03.csv | .csv | 2026-04-01 | C:\Reports\Sales\Monthly\ |
| 5 | readme.txt | .txt | 2026-01-15 | C:\Reports\Sales\Monthly\ |
Before: the folder listing (Content and Attributes columns not shown). Note the stray readme.txt.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Source.Name | Date | Store | Product | Amount |
| 2 | sales_2026-01.csv | 2026-01-03 | ATH01 | Router X2 | 99 |
| 3 | sales_2026-01.csv | 2026-01-03 | LIS02 | Switch 8P | 59 |
| 4 | sales_2026-02.csv | 2026-02-01 | ATH01 | Access Point | 145 |
| 5 | sales_2026-03.csv | 2026-03-02 | BOS01 | Router X2 | 99 |
After: all files stacked, with Source.Name telling which file each row came from (readme.txt filtered out).
What Power Query generated
Look at the Queries pane. Next to your main query there is a folder called Transform File from Monthly (named after your query) containing Helper Queries:
| Helper | What it is |
|---|---|
| Parameter1 | A parameter that holds the sample file's binary content |
| Sample File | A query that picks the first file in the folder |
| Transform Sample File | The steps applied to one file: open, promote headers, and anything you add |
| Transform File | A function made from Transform Sample File, called once per file |
The key idea: Transform Sample File is the template. Whatever steps you add to it are turned into the Transform File function automatically and applied to every file. So if each file has two junk lines at the top or a Total row at the bottom, remove them in Transform Sample File, not in the main query.
let
Source = Folder.Files("C:\Reports\Sales\Monthly"),
#"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", Table.ColumnNames(#"Transform File"(#"Sample File"))),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Table Column1", {{"Source.Name", type text}, {"Date", type date}, {"Store", type text}, {"Product", type text}, {"Amount", type number}})
in
#"Changed Type"Filter the files first
The folder may contain files you do not want: a readme.txt, a summary workbook, or Excel's temporary lock files whose names start with ~$ and appear while someone has a file open. Add filters right after the Source step:
- Click the Source step (Power Query asks to insert a step; click Insert).
- Filter
Extensionto keep only.csv(watch out: some systems write.CSVin capitals; use Text Filters › Equals after a lowercase step, or the code below). - Filter
Namewith Text Filters › Does Not Begin With~$.
= Table.SelectRows(Source, each Text.Lower([Extension]) = ".csv" and not Text.StartsWith([Name], "~$"))Date modified or by name to load only this year's files, for example Text.Contains([Name], "2026"). Filtering the file list is far cheaper than loading every file and filtering the rows afterwards.Use the file name as data
Source.Name is often the only place the month is stored. Turn it into a proper date column in the main query:
- Select
Source.Nameand choose Add Column › Extract › Text Between Delimiters with_and.. You get2026-01. - Add a custom column
Date.FromText([Text Between Delimiters] & "-01")and set its type to Date. - Rename it
Monthand remove the helper columns.
Cleaning each file in Transform Sample File
Suppose each branch's Excel file starts with a title line and ends with a total line. Select Transform Sample File in the Queries pane and add Remove Top Rows 1, Use First Row as Headers and a filter that removes the Total row. Go back to the main query: every file now arrives clean. If the main query shows an error after you changed the sample, it usually comes from the Expanded Table Column1 or Changed Type step, which still lists the old column names; click those steps and fix the names.
Table.TransformColumnNames.A leaner alternative without helper queries
Once you understand the generated structure, you can write a compact version yourself. This is handy for Excel files whose table is always called Sales:
let
Source = Folder.Files("C:\Reports\Branches"),
XlsxOnly = Table.SelectRows(Source, each [Extension] = ".xlsx" and not Text.StartsWith([Name], "~$")),
AddData = Table.AddColumn(XlsxOnly, "Data", each Excel.Workbook([Content], true){[Item="Sales", Kind="Table"]}[Data]),
KeepCols = Table.SelectColumns(AddData, {"Name", "Data"}),
Expanded = Table.ExpandTableColumn(KeepCols, "Data", {"Date", "Branch", "Account", "Amount"}),
Typed = Table.TransformColumnTypes(Expanded, {{"Date", type date}, {"Amount", type number}})
in
TypedBecause the expand step lists the columns by name, files with extra columns still work (the extra columns are ignored) and files missing a column give null in that column rather than an error.
Errors in single files
If one file is corrupt or has a different structure, the cell for that file in the Transform File column becomes an error, and the whole refresh may fail. To find the culprit, go to the Invoke Custom Function1 step and use Home › Keep Rows › Keep Errors. Fix or move the file, then delete the temporary step. The Skip files with errors option in the Combine dialog hides such files instead, which is convenient but means data can go missing without anyone noticing.
/Users/maria/Reports/Sales. A query built on Windows with a C:\ path must have its source changed (Data Source Settings › Change Source) before it refreshes on a Mac. Excel for the web cannot read local folders at all; use SharePoint Folder for shared workbooks.Check yourself
1. Where do you add steps that must run on every file before stacking?
Transform Sample File is turned into the function applied to each file.
2. What is Source.Name?
The main query keeps the file name as Source.Name.
3. Why filter out names beginning with ~$?
Excel creates ~$ lock files while a workbook is open.
4. A new month's file renamed the column Amount to Net amount. What usually happens on refresh?
Steps that list columns by name fail or produce nulls when a name changes.
5. Which source should you use so colleagues can refresh a folder query?
A SharePoint path is the same for everyone with access.