Sub topic 8
Keeping track of which file each row came from
This chapter assumes you can open the Power Query editor, connect to a folder, and use Combine & Transform Data. It also assumes you know what the sample file, parameter, and function queries are. Here you will make sure every row in the combined table records the file it came from.
Why provenance matters
When 12 monthly exports become one table, you lose the boundaries between them. You can no longer tell which rows came from 2026-03.csv. That makes it hard to check totals against a single file, find the file that holds a bad row, or derive a month from the file name.
What the combine step gives you
When you use Combine & Transform Data, Power Query adds a column called Source.Name. It holds the name of the file each row came from. It is usually the first column in the combined query. You can see it in the generated query's last steps, where the file name is kept next to the expanded data.
The Folder connector also returns Folder Path, Date modified, and other columns. The combine step does not carry these into the result unless you keep them yourself.
Keeping more than the name
If you combine files by hand, as in the earlier custom example, you choose which file columns to keep. This query keeps the name and the modified date, then expands the data.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Parsed = Table.AddColumn(CsvOnly, "Data", each Table.PromoteHeaders(Csv.Document([Content], [Delimiter = ",", Encoding = 65001]))),
Kept = Table.SelectColumns(Parsed, {"Name", "Date modified", "Data"}),
Expanded = Table.ExpandTableColumn(Kept, "Data", {"Date", "Region", "Amount"}),
Typed = Table.TransformColumnTypes(Expanded, {{"Date", type date}, {"Region", type text}, {"Amount", type number}, {"Date modified", type datetime}})
in
TypedTable.SelectColumns lists Name, Date modified, and Data. Those three columns go forward, and the expand step turns Data into the real columns. Each row of every file now also carries the file name and its modified time.
Using the file name as data
A file name often holds useful information. If files are named Sales_2026-03.csv, you can extract the month from the name. In the Power Query editor, select the Source.Name column and use Transform > Extract > Text Between Delimiters. You can also add a column in M.
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv"),
WithPeriod = Table.AddColumn(CsvOnly, "Period", each Text.BetweenDelimiters([Name], "_", "."), type text)
in
WithPeriodFor Sales_2026-03.csv, Period is 2026-03. Add this step before you expand the data, so it is computed once per file and not once per row.
Where the name is added
The order of steps decides what you can use. The following diagram shows the flow.
The name must stay in the query until the expand step. If you select only Content before you expand, the name is gone and you cannot recover it later.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No file name column in the result | Name was removed before the expand step | Keep `Name` in the select-columns step |
| Name column appears twice | A data column in the files is also called `Name` | Rename the file column before you expand |
| Period shows an error | A file name does not follow the pattern | Filter such files out or handle the error |
| Modified date looks wrong | File was copied, which can reset the date | Use the name or a data column instead |
Exercise
A colleague deletes every column except Content in the folder list, then parses and expands the files. Later they want to know which file a bad row came from. Why can they not find out, and where should they change the query?
Show answer
Only Content was kept, so the file name was dropped before the expand step and the rows have no record of their file. They should keep Name in the select-columns step, alongside the parsed data column, and then expand.
Exercise
Files are named Budget_2026-03.csv. Write the M expression, used inside Table.AddColumn, that returns 2026-03 from the Name column.
Show answer
Text.BetweenDelimiters([Name], "_", ".")