Sub topic 10
Combining Many Files with the Same Layout into One Table
Many reporting folders hold one file per month, region, or export run. Every file has the same columns. This chapter shows how Power BI turns such a folder into one table, and how to build that query by hand when you need more control.
This chapter assumes you can open Power Query Editor and read the applied steps pane. It also assumes you know how the Text/CSV and Excel connectors work, and that you can reach a SharePoint site through SharePoint.Files. Combining files builds on all three.
How the folder listing works
A folder connector does not return your data. It returns a listing with one row per file. The main columns are:
| Column | Contents |
|---|---|
| Content | The file's bytes, as a binary value |
| Name | The file name, such as `orders-2026-01.csv` |
| Extension | The extension, including the dot, such as `.csv` |
| Date modified | The last change time |
| Folder Path | The full folder location, ending in a slash |
Combining means parsing each Content value with the right connector function and stacking the results. The two folder connectors you will meet are Folder.Files for local or network folders and SharePoint.Files for SharePoint Online. Both search subfolders as well. A file in Orders/Archive/ appears in the listing unless you filter it out.
What Combine & Transform generates
In the Navigator or the listing preview, Combine & Transform Data creates several queries for you:
- Sample File: a query that points at the first file in the listing.
- Parameter1: a parameter that stands in for one file's content.
- Transform Sample File: the steps you would apply to one file, such as promoting headers.
- Transform File: a function made from the previous two. It runs the same steps on any file.
- The main query: it calls the function on every row, then expands the results into columns.
<br>
The wizard is quick. The cost is that the real logic sits in helper queries, which some people find hard to follow. Edit the Transform Sample File query to change how every file is read. Editing the main query changes only how the results are combined.
Writing the combine by hand
For a folder of CSV files on SharePoint Online, you can build the same result in one query. This version filters to the right folder and extension, parses each file, and keeps the file name as a column.
let
Source = SharePoint.Files("https://contoso.sharepoint.com/sites/Sales", [ApiVersion = 15]),
InFolder = Table.SelectRows(
Source,
each [Folder Path] = "https://contoso.sharepoint.com/sites/Sales/Shared Documents/Orders/"
and [Extension] = ".csv"
and not Text.StartsWith([Name], "~$")
),
Parsed = Table.AddColumn(
InFolder,
"Data",
each Table.PromoteHeaders(
Csv.Document([Content], [Delimiter = ",", Encoding = 65001, QuoteStyle = QuoteStyle.Csv]),
[PromoteAllScalars = true]
)
),
Named = Table.SelectColumns(Parsed, {"Name", "Data"}),
Renamed = Table.RenameColumns(Named, {{"Name", "Source.Name"}}),
Expanded = Table.ExpandTableColumn(Renamed, "Data", {"OrderID", "OrderDate", "Amount"}),
Typed = Table.TransformColumnTypes(
Expanded,
{{"OrderID", Int64.Type}, {"OrderDate", type date}, {"Amount", type number}}
)
in
TypedThree details matter here:
- Headers are promoted per file, before the tables are stacked. If you stack first, the header row of every file after the first becomes a data row.
- Types are set once, after expanding. Setting them inside each file works too, but then you repeat the step for every column change.
- Expansion works by column name. The order of columns in each file does not matter.
For a local folder, replace the Source and InFolder steps with Folder.Files("C:\Data\Orders") and a filter on [Extension]. The rest of the query stays the same.
Excel files: write a function
Excel needs one more choice for each file: which table or sheet to read. Put that choice in a function query. Name this query fnLoadSales:
let
LoadSales = (content as binary) as table =>
let
Workbook = Excel.Workbook(content, true, true),
SalesTable = Workbook{[Item = "Sales", Kind = "Table"]}[Data]
in
SalesTable
in
LoadSalesThen call it from the main query:
let
Source = SharePoint.Files("https://contoso.sharepoint.com/sites/Sales", [ApiVersion = 15]),
InFolder = Table.SelectRows(
Source,
each [Folder Path] = "https://contoso.sharepoint.com/sites/Sales/Shared Documents/Regions/"
and [Extension] = ".xlsx"
and not Text.StartsWith([Name], "~$")
),
Loaded = Table.AddColumn(InFolder, "Data", each fnLoadSales([Content])),
Combined = Table.Combine(Loaded[Data])
in
CombinedTable.Combine takes a list of tables and joins them by column name. If a column is missing from one table, that table gets null in that column. This is more forgiving than Table.ExpandTableColumn, which raises an error when a listed column is absent.
Reading a table by name works only when every workbook has an Excel table with that name. If some files only have a sheet, use Kind = "Sheet" and promote the headers yourself.
Edge cases that break combined queries
Combined queries often work on the first day and fail later. These are the usual causes:
| Symptom | Cause | Fix |
|---|---|---|
| Error naming a missing column | A new file lacks a column, or its header was renamed | Use `Table.SelectColumns` with `MissingField.UseNull`, or fix the source file |
| Rows with values like "OrderID" in the data | Headers promoted after stacking | Promote headers inside the per-file step |
| Query fails after someone deletes a file | The sample file query points at a file that is gone | Point the sample at a file that is stable, or build the query by hand |
| An error in a file you never see | A temporary lock file such as `~$orders.xlsx`, or a non-data file in the folder | Filter by `Extension` and by name |
| Duplicate rows | The same file appears in a subfolder such as `Archive` | Filter on `Folder Path` |
| Wrong values in some files | Different delimiter or encoding in one file | Standardize the exports, or filter that file and give it its own query |
| Numbers become errors after typing | Locale differences, such as a comma decimal separator | Use `Table.TransformColumnTypes` with a culture argument |
To tolerate a missing column, add a step like this before you expand or combine:
Table.SelectColumns(Table, {"OrderID", "OrderDate", "Amount"}, MissingField.UseNull)<br>
Folders, credentials, and refresh
A combined query touches every file, so refresh time grows with the number of files. One query that reads 200 CSV files makes 200 file reads on every refresh. If most files never change, move old files to an archive folder outside the query's path, or save a historical snapshot as one file.
Credentials are set per data source, not per file. A SharePoint folder needs one credential entry for the site. All the files below it share it. Combining files from two different sources in one query, such as a SharePoint folder and a local folder, can raise a privacy-level error that mentions Formula.Firewall. Set a privacy level for each source, or load each source in its own query and combine them in a third query.
For scheduled refresh, SharePoint Online files can refresh in the Power BI service without a gateway, because the service can reach SharePoint Online directly. A local folder like C:\Data\Orders cannot be reached from the service, so you need an on-premises data gateway that can read that path. If you want the path to change between environments, use a parameter for the folder path, as covered in the parameters chapter.
Checking your result
After combining, check three things before you build visuals:
- Count the rows per
Source.Name, and confirm that every expected file appears. - Check that no column has an unexpected error or null count. Column quality in Power Query Editor shows this.
- Confirm the column types. A column that is still
Anymeans the type step did not run or was removed.
Exercise
A folder on SharePoint Online holds 12 monthly CSV files. After you combine them, the table has 12 extra rows where the OrderID column contains the text "OrderID". What went wrong, and where in the query do you fix it?
Show answer
The headers were promoted only once, after the tables were stacked. Only the first file's header row became column names, and the other 11 header rows stayed as data rows. Fix it by promoting headers inside the per-file step, with Table.PromoteHeaders applied to each file's parsed table before the tables are combined. The data rows with "OrderID" then disappear because each file's header row is consumed.
Exercise
Your combined query reads C:\Data\Orders and refreshes fine in Power BI Desktop. After you publish it, scheduled refresh in the service fails. Name the missing piece, and say what would change if the same files were moved to a SharePoint Online document library.
Show answer
The service cannot reach a local path, so you need an on-premises data gateway with access to that folder, and the data source needs credentials in the gateway settings. With the files in SharePoint Online, you would switch to SharePoint.Files, and the service could refresh directly without a gateway. You would still need valid SharePoint credentials for the data source.