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.

Intermediate 18 min read

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

  1. Choose Data › Get Data › From File › From Folder.
  2. Browse to the folder and click Open (or paste the path and click OK).
  3. A preview lists the files with columns Content, Name, Extension, Date accessed, Date modified, Date created, Attributes and Folder Path. Subfolders are included.
  4. Click Combine › Combine & Transform Data.
  5. 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.
  6. Leave Skip files with errors unticked at first, so you notice problems. Click OK.
  7. The editor opens with a combined table and a new group of helper queries in the Queries pane.
ABCD
1NameExtensionDate modifiedFolder Path
2sales_2026-01.csv.csv2026-02-02C:\Reports\Sales\Monthly\
3sales_2026-02.csv.csv2026-03-02C:\Reports\Sales\Monthly\
4sales_2026-03.csv.csv2026-04-01C:\Reports\Sales\Monthly\
5readme.txt.txt2026-01-15C:\Reports\Sales\Monthly\

Before: the folder listing (Content and Attributes columns not shown). Note the stray readme.txt.

ABCDE
1Source.NameDateStoreProductAmount
2sales_2026-01.csv2026-01-03ATH01Router X299
3sales_2026-01.csv2026-01-03LIS02Switch 8P59
4sales_2026-02.csv2026-02-01ATH01Access Point145
5sales_2026-03.csv2026-03-02BOS01Router X299

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:

HelperWhat it is
Parameter1A parameter that holds the sample file's binary content
Sample FileA query that picks the first file in the folder
Transform Sample FileThe steps applied to one file: open, promote headers, and anything you add
Transform FileA 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.

Power Query M
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"
The main query: list the files, skip hidden ones, call the function on each file's Content, keep the name, expand. The expanded column names come from the sample file.

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:

  1. Click the Source step (Power Query asks to insert a step; click Insert).
  2. Filter Extension to keep only .csv (watch out: some systems write .CSV in capitals; use Text Filters › Equals after a lowercase step, or the code below).
  3. Filter Name with Text Filters › Does Not Begin With ~$.
Power Query M
= Table.SelectRows(Source, each Text.Lower([Extension]) = ".csv" and not Text.StartsWith([Name], "~$"))
A single robust filter for file type and temporary files.
Tip. You can also filter by 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:

  1. Select Source.Name and choose Add Column › Extract › Text Between Delimiters with _ and .. You get 2026-01.
  2. Add a custom column Date.FromText([Text Between Delimiters] & "-01") and set its type to Date.
  3. Rename it Month and 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.

Watch out. The generated Changed Type step at the end lists column names explicitly. If a future file renames a column, the refresh fails with The column '…' of the table wasn't found. That is actually helpful (you learn about the change), but it also means you must keep the files consistent or standardise names in Transform Sample File, for example with 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:

Power Query M
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
    Typed
Each file is opened with Excel.Workbook and the Sales table is taken from it; the expand lists the columns you expect.

Because 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.

Note. On a Mac, current Microsoft 365 builds can combine files from a local folder in the same way; the path looks like /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?

2. What is Source.Name?

3. Why filter out names beginning with ~$?

4. A new month's file renamed the column Amount to Net amount. What usually happens on refresh?

5. Which source should you use so colleagues can refresh a folder query?