Sub topic 8
Keeping track of which file each row came from
Once you combine a folder of files, every row sits in one big table. The question "which file did this row come from?" is easy to answer while the data is still split into files. It gets hard after the merge, unless you keep the file name on purpose. This chapter shows how to keep that information, how to clean it, and what can go wrong.
This chapter assumes you can open the Power Query editor, you know how to run Combine & Transform Data, and you have seen the file list that From Folder returns (Content, Name, Extension, Date modified, Folder Path).
Why provenance matters
Provenance means where a piece of data came from. For combined files, it is usually the file name, and sometimes the folder or the modified date. You need it for these tasks:
- Finding the source of a bad row. If one amount looks wrong, you can open the exact file and fix it.
- Deriving values that exist only in the file name, such as a month (
Sales_2026-03.csv) or a region (North.csv). - Spotting duplicates. The same data loaded from two files, such as
Sales_March.csvandSales_March (1).csv, is easy to see when the file name is present. - Auditing. A reviewer can trace a total back to the files that produced it.
What the Combine step does by default
When you choose Combine & Transform Data, Excel creates the helper queries (sample file, parameter, function) and a combined query. In the combined query, Excel adds a column named Source.Name as the first column. It holds the file name for each row.
You can see it in the Applied Steps of the combined query. The function result is expanded, and the Name column from the file list is kept next to it.
Here is the idea in the by-hand form from the spine. The Name column is kept in Kept, so it survives the expansion:
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv"),
Parsed = Table.AddColumn(CsvOnly, "Data", each Table.PromoteHeaders(Csv.Document([Content], [Delimiter = ",", Encoding = 65001]))),
Kept = Table.SelectColumns(Parsed, {"Name", "Data"}),
Expanded = Table.ExpandTableColumn(Kept, "Data", {"Date", "Region", "Amount"}),
Typed = Table.TransformColumnTypes(Expanded, {{"Date", type date}, {"Region", type text}, {"Amount", type number}})
in
TypedThe key line is Table.SelectColumns(Parsed, {"Name", "Data"}). It drops Content (the binary data) and every other file-list column, but keeps Name. If you removed Name here, the file name would be gone for good, because after Table.ExpandTableColumn the rows of all files are mixed together.
The same rule applies to the GUI. If you remove the Source.Name column during cleanup, you cannot get it back from the data itself. You would have to go back to the step before the removal.
Choosing which file-list columns to keep
Name is the most common choice, but the file list has other columns that help in some cases.
| Column | What it holds | Use it when |
|---|---|---|
| Name | File name with extension | You need the file or want to parse a month or region from it |
| Folder Path | Full path to the folder | Files come from subfolders and the folder name carries meaning |
| Date modified | Last write time | You want to keep the latest version of a file or detect stale data |
| Date created | Creation time | You compare when files arrived |
To keep more than one, list them in the Table.SelectColumns step:
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", "Folder Path", "Date modified", "Data"}),
Expanded = Table.ExpandTableColumn(Kept, "Data", {"Date", "Region", "Amount"})
in
ExpandedEach kept column repeats on every row from that file. Keep only what you will use. Extra columns make the table wider and the refresh slower to read.
Renaming and placing the column
The default name Source.Name contains a dot, which is legal in Power Query but awkward in formulas elsewhere. A clearer name is easy to apply:
- In the combined query, right-click the Source.Name column header.
- Choose Rename and type a name such as
SourceFile. - Optionally drag the column to where you want it, or use Transform > Move.
Renaming happens after the combine, so it does not affect the helper queries. Do not rename columns inside the sample-file transformation to change the file name column. The file name is added by the combined query, not by the sample file.
Turning the file name into useful columns
Often the file name holds the value you actually need. Suppose files are named Sales_2026-03.csv. You can split the name into parts without touching 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", "Data"}),
Expanded = Table.ExpandTableColumn(Kept, "Data", {"Date", "Region", "Amount"}),
NoExt = Table.AddColumn(Expanded, "FileBase", each Text.BeforeDelimiter([Name], ".", {0, RelativePosition.FromEnd}), type text),
Period = Table.AddColumn(NoExt, "Period", each Text.AfterDelimiter([FileBase], "_"), type text)
in
PeriodHere Text.BeforeDelimiter with {0, RelativePosition.FromEnd} cuts at the last dot, so a name such as Sales_2026-03.v2.csv still loses only .csv. Text.AfterDelimiter then takes everything after the first underscore, giving 2026-03.
In the GUI you can get the same result with Transform > Extract or Split Column > By Delimiter, applied to the file name column of the combined query.
Where the file name comes from in the helper queries
The default combine produces a function query, usually called Transform File. It takes one file's binary content and returns a table. The combined query calls it for each row of the file list, then expands the result. The Name column is carried alongside, which is why it appears as Source.Name.
This design has a consequence. Edits to Transform Sample File change what the function returns for every file, but they cannot see the file name. For file-name logic, always work in the combined query.
Common problems
The file name column disappears
You or a colleague removed it, or removed all columns except a chosen few. Open the Applied Steps list, click the step before the removal, and check whether Source.Name is present. Delete the step that removed it, or rebuild it by keeping Name in a Table.SelectColumns step.
Names include temporary files
Excel creates ~$Report.xlsx while a workbook is open. If you did not filter these out, they appear as extra sources and often cause errors. Filter them before combining, as in the Text.StartsWith([Name], "~$") test above.
The same name in different subfolders
Folder.Files includes subfolders. Two files named Sales.csv in North and South have the same Name but different Folder Path. If you keep only Name, you cannot tell them apart. Keep Folder Path too, or build a combined key:
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv"),
WithKey = Table.AddColumn(CsvOnly, "FileKey", each [Folder Path] & [Name], type text)
in
WithKeyThe file name column has the wrong type
New columns created from text are usually typed as text, but if you rely on automatic detection, a name like 2026 could be typed as a number. Set the type explicitly, as in the type text argument above.
Exercises
Exercise
You combined a folder of monthly CSV files, then removed every column except Date, Region and Amount. Months later you need to know which file a suspicious row came from. Why can you not find it, and where in the query do you restore it?
Show answer
Removing Source.Name (the file name) discards the only link between each row and its file, because after the expand step all files' rows are mixed. Open Applied Steps, find the step that removed the column, and either delete that step or insert a step before it that keeps the Name column. Rebuilding it from the data is not possible.
Exercise
Files are named Budget_North.xlsx, Budget_South.xlsx. Write the M step that adds a Region column from the file name column Name, assuming the combined table is in a step called Expanded.
Show answer
Region = Table.AddColumn(Expanded, "Region", each Text.BeforeDelimiter(Text.AfterDelimiter([Name], "_"), ".", {0, RelativePosition.FromEnd}), type text). The inner call takes the text after the underscore (North.xlsx), and the outer call keeps the text before the last dot (North).
Exercise
Two subfolders each contain a file called Sales.csv. After combining, rows from both files show the same file name. What is the fix?
Show answer
Keep the Folder Path column as well as Name, or create a key column such as [Folder Path] & [Name]. Folder.Files returns files from subfolders, and only the combination of path and name is unique.