Sub topic 4
Choosing which files to combine
This chapter assumes you can open the Power Query editor, you know how to connect to a folder with Data > Get Data > From File > From Folder, and you have seen the file list that connector returns (Content, Name, Extension, Date modified, Folder Path). Here you will learn how to filter that list so only the files you intend to combine reach the combine step.
Why filtering comes first
The From Folder connector returns every file in the folder and in all of its subfolders. It does not care whether a file is a report, a backup copy, or a temporary file that Excel created. If an unwanted file stays in the list, the combine step tries to parse it like the others. The result is usually an error, extra rows, or columns that do not match.
You filter the file list before you combine. The combine step then works only on rows you chose. The order matters, because the sample file that Excel builds is taken from the first row of the list. If that row is a stray file, the whole combined query is built on the wrong layout.
Filtering by extension
The Extension column holds the file type, including the leading dot, such as .csv or .xlsx. Use it to drop everything that is not the type you want.
- In the Power Query editor, click the filter arrow on the Extension column header.
- Clear Select All, then tick only
.csv(or the type you need). - Click OK.
Power Query writes a Table.SelectRows step. You can see it in the formula bar.
Two cautions apply. First, the filter list shows only values it has loaded in a preview, so a rare extension may be missing from the checklist. Second, comparisons are case sensitive. A file named REPORT.CSV has the extension .CSV, and [Extension] = ".csv" does not match it. If your folder mixes cases, normalize the value first with Text.Lower.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each Text.Lower([Extension]) = ".csv")
in
CsvOnlyRemoving temporary files
When you open an Excel workbook, Excel creates a hidden lock file in the same folder. Its name starts with ~$, and it has the same extension as the real workbook. Filtering on .xlsx alone therefore keeps these lock files, and the combine step fails on them because they are not valid workbooks.
Filter on the Name column as well. In the Name filter menu, choose Text Filters > Does Not Begin With, and enter ~$. The M code below applies both conditions in one step.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Kept = Table.SelectColumns(CsvOnly, {"Name", "Content"})
in
KeptLock files appear only while a workbook is open. A query that works today can fail tomorrow because a colleague had a file open when you refreshed. Adding the ~$ filter up front removes that risk.
Filtering by name pattern
Names often carry meaning. A folder may hold Sales_2026-01.csv, Sales_2026-02.csv and Notes.csv. You can keep the files that follow your naming convention with the Text Filters options on the Name column: Begins With, Ends With, Contains, and their negations.
Use the narrowest rule that describes the files you want. Begins With "Sales_" is safer than Does Not Contain "Notes", because a positive rule excludes new unknown files by default, while a negative rule lets them through.
let
Source = Folder.Files("C:\Data\Sales"),
Selected = Table.SelectRows(
Source,
each [Extension] = ".csv"
and Text.StartsWith([Name], "Sales_")
and not Text.StartsWith([Name], "~$")
)
in
SelectedFiltering by folder path
Because the connector includes subfolders, a folder such as C:\Data\Sales\Archive is read too. The Folder Path column holds the full path of the folder that contains each file, ending with a backslash. To keep only the top-level folder, filter for rows where Folder Path equals the path you connected to.
let
Source = Folder.Files("C:\Data\Sales"),
TopLevel = Table.SelectRows(Source, each [Folder Path] = "C:\Data\Sales\"),
CsvOnly = Table.SelectRows(TopLevel, each [Extension] = ".csv")
in
CsvOnlyTo exclude one subfolder but keep the others, use Text.Contains:
let
Source = Folder.Files("C:\Data\Sales"),
NoArchive = Table.SelectRows(Source, each not Text.Contains([Folder Path], "\Archive\"))
in
NoArchiveNote the trailing backslash in the first example. If you compare to "C:\Data\Sales" without it, no row matches and the table comes back empty.
Filtering by date modified
The Date modified column lets you keep only recent files. Open its filter menu and choose Date/Time Filters, such as After, Is Latest, or In the Previous. For example, you can combine only files changed in the last 30 days.
let
Source = Folder.Files("C:\Data\Sales"),
Recent = Table.SelectRows(
Source,
each [Extension] = ".csv"
and [Date modified] >= Date.AddDays(DateTime.LocalNow(), -30)
)
in
RecentBe careful with this choice. Date modified changes whenever someone saves or copies a file, not only when its data changes. Copying an old export to a new location gives it a new date. If the month in the file name is what matters, filter on the name and not on the date.
Check the result before combining
After you filter, look at the preview and confirm three things:
- The row count matches the number of files you expect.
- The first row is a file with the correct layout, since it becomes the sample file.
- No
~$names, no archive paths and no unexpected extensions remain.
You can also sort the list by Name so the first row is predictable. Sorting does not change which files are combined, but it makes the sample file stable from one refresh to the next.
| Goal | Column | Filter |
|---|---|---|
| Only CSV files | Extension | Equals `.csv` |
| Skip Excel lock files | Name | Does Not Begin With `~$` |
| Follow a naming convention | Name | Begins With `Sales_` |
| Ignore subfolders | Folder Path | Equals the connected path |
| Only recent files | Date modified | In the Previous 30 days |
Common errors at this stage
- The file list is empty after filtering. Check for a case mismatch in Extension, or a missing trailing backslash in Folder Path.
- An old file keeps appearing. It may sit in a subfolder. Filter on Folder Path.
- A combine error names a file you did not expect. Open the list and look for
~$names or a different extension. - Filters hide a new file type. A negative rule lets unknown files through. Prefer positive rules.
Exercise
A folder contains Jan.xlsx, Feb.xlsx, ~$Feb.xlsx and a subfolder Old that holds Dec.xlsx. You want only Jan and Feb. Write one Table.SelectRows step on Source = Folder.Files("C:\Data\Budgets") that keeps exactly those two files.
Show answer
Filter on extension, lock-file prefix and folder path together:
Table.SelectRows(Source, each [Extension] = ".xlsx" and not Text.StartsWith([Name], "~$") and [Folder Path] = "C:\Data\Budgets\")
The Extension test removes other types, the Name test removes ~$Feb.xlsx, and the Folder Path test removes Dec.xlsx in the Old subfolder.
Exercise
Your query filters with [Extension] = ".csv", but a new file named EXPORT.CSV is not being combined. Why, and what is a fix?
Show answer
Text comparison in M is case sensitive, and the file's Extension is .CSV. Use Text.Lower([Extension]) = ".csv" so every case matches.