Sub topic 4
Choosing which files to combine
This chapter assumes you can open the Power Query editor, connect to a folder with From Folder, and read the file list it returns. You should also know the columns in that list: Content, Name, Extension, Date modified and Folder Path.
Why filter first
From Folder returns every file in the folder and all of its subfolders. That often includes files you do not want:
- Excel temporary files, which start with
~$and exist while a workbook is open - Files of another type, such as a
.txtnote or a.xlsxfile in a CSV folder - Archived files stored in a subfolder
- Old files that no longer belong in the report
If any of these reach the combine step, the query can fail or add bad rows. Filter the list before you combine.
Filter in the editor
You filter the file list like any other table. Open the filter arrow on a column and choose a filter.
- Open the filter arrow on Extension. Clear every box except
.csv. - Open the filter arrow on Name. Choose Text Filters > Does Not Begin With and enter
~$. - Optionally, filter Folder Path to exclude a subfolder, or filter Date modified to keep only recent files.
Each filter becomes a step in Applied Steps. Extension values include the dot and are case-sensitive, so .CSV and .csv are different values. If your folder mixes cases, use a text transform or a case-insensitive comparison, as shown below.
The same filters in M
This query lists the files and keeps only CSV files whose names do not start with ~$.
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
KeptTable.SelectRows keeps the rows where the condition is true. The each keyword defines a function that runs once per row, and [Extension] reads that column for the current row. Table.SelectColumns then keeps only the two columns needed later.
To ignore case, replace the extension test with Text.Lower([Extension]) = ".csv".
Excluding subfolders
Folder.Files always searches subfolders. To use only the top level, keep rows whose Folder Path equals the folder you connected to. The path ends with a backslash.
let
Source = Folder.Files("C:\Data\Sales"),
TopLevel = Table.SelectRows(Source, each [Folder Path] = "C:\Data\Sales\")
in
TopLevelWhere filtering fits
The order of steps matters. Filtering the file list comes first, and combining comes second.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Error mentioning a file you did not expect | A `~$` temporary file is in the list | Add the Name filter |
| Rows from old data | A subfolder or archive is included | Filter Folder Path |
| New files never appear | Extension filter is case-sensitive | Use `Text.Lower([Extension])` |
Exercise
A folder holds jan.csv, feb.csv, notes.txt and a temporary file ~$feb.xlsx. Write the condition for Table.SelectRows that keeps only the two CSV files.
Show answer
each [Extension] = ".csv". The .txt file and the .xlsx temporary file both fail the extension test, so the ~$ name test is not needed here. If you want protection against a temporary ~$ CSV file, add and not Text.StartsWith([Name], "~$").
Exercise
Why can Folder.Files("C:\Data\Sales") return a file stored in C:\Data\Sales\Archive, and how do you exclude it?
Show answer
Folder.Files searches subfolders as well as the folder itself. Filter Folder Path to equal C:\Data\Sales\, or filter out paths that contain Archive.