Combining multiple sheets across workbooks
This page combines data held on several sheets in each of several workbooks into a single table.
The method needs all the workbooks to be in one folder, and the data on every sheet to be in the same layout. Beyond that, the workbooks can differ:
- They can have different numbers of sheets.
- They can contain sheets that are not needed, which the query leaves out.
Overview
Section titled “Overview”The method has two parts:
- Build two custom functions:
- one that lists the sheets to be used from a single workbook
- one that reads and transforms the data on a single sheet
- Call both functions from a query that lists the workbooks in the folder, which pulls in the data from every sheet of every file.
A function that lists the sheets in a workbook
Section titled “A function that lists the sheets in a workbook”This function takes a folder path and a file name, and returns the names of the sheets to be used in that workbook. Building it takes these steps:
- Connect to one of the workbooks, which serves as the sample file for the function.
- Click Transform Data to open the Power Query Editor, which lists every sheet in the workbook.
- Filter the list to keep only the sheets that are needed.
- Replace the file path with two parameters, a folder path and a file name, so that the query can become a function.
- Turn the query into a function called fnAllSheetNames.
1. Load the sample file
Section titled “1. Load the sample file”- Choose Get Data ➔ From File ➔ From Workbook.
- Select one of the workbooks. In this example there is one workbook per year:

- In the Navigator, click Transform Data.
The query lists the workbook’s sheets, one per row:

- Filter the Name column to remove the sheets that are not needed. Here the filter keeps the names that do not begin with Sheet:

- Remove every column except the first.
- Rename that column SheetNames, which leaves this:

2. Set up the parameters
Section titled “2. Set up the parameters”- In Applied Steps, click the gear icon beside the Source step.
- Choose Advanced, which splits the file path into parts. Open the type menu beside the first part, and choose New Parameter:

- Create a parameter called FilePath, and enter the workbook’s folder path as its Current Value:

- Back in the source dialog, open the type menu beside the second part, and choose New Parameter again:

- Create a second parameter called FileName, and enter the workbook’s file name as its Current Value:

3. Turn the query into a function
Section titled “3. Turn the query into a function”- In the Queries pane, right-click the query and choose Create Function:

- Name the function fnAllSheetNames. Power Query creates the function and groups it with the query and its parameters:

- Finally, rename the query 2018 xlsx to SingleWorkBook.

The SingleWorkBook query stays in place, because editing it changes the function built from it.
A function that transforms a single sheet
Section titled “A function that transforms a single sheet”This function takes a folder path, a file name and a sheet name, and returns that sheet’s data in the shape required. Building it takes these steps:
- Duplicate the query built from the sample file.
- Make it read one sheet of the workbook.
- Add a third parameter, SheetName.
- Change the query so that it reads the sheet named by the parameter, which means it then depends only on the three parameters.
- Turn the query into a function called fnProcessSheets.
This function is the one that processes each sheet.
1. Create a SingleSheet query
Section titled “1. Create a SingleSheet query”- Duplicate the SingleWorkBook query, and rename the copy SingleSheet.

- Delete the last two steps of the query, so that it shows the filtered list of sheets again:

2. Create a sheet parameter
Section titled “2. Create a sheet parameter”- On the Home tab, click Manage Parameters.
- Create a third parameter called SheetName, with a Current Value of Jun.

- Click OK.
- Add a third step to the query with this formula, which picks the sheet named by the parameter from the filtered list:
= #"Filtered Rows"{[Item=SheetName,Kind="Sheet"]}[Data]- Name the step SheetData. A step called SheetName would refer to itself rather than to the parameter, although the screenshot shows that older name.

3. Transform the sheet
Section titled “3. Transform the sheet”- Apply whatever transformations each sheet needs.
- In this example, the only one needed is Use First Row as Headers.
4. Turn the query into a function
Section titled “4. Turn the query into a function”- Right-click the SingleSheet query and choose Create Function.
- Name the function fnProcessSheets. Power Query creates it in a group of its own:

Combine the data with the two functions
Section titled “Combine the data with the two functions”The last query brings the two functions together:
- Connect to the folder that holds the workbooks.
- Click Transform Data to open the list of files in the Power Query Editor.
- Keep only the column of file names and the column of folder paths, and remove all the others.
- Add a column with Invoke Custom Function, which calls fnAllSheetNames with those two columns as its parameters.
The query then lists every sheet to be used from every file in the folder.
To pull in the data from those sheets:
- Add another column with Invoke Custom Function, which calls fnProcessSheets with its three parameters.
The query then holds the data from every relevant sheet of every workbook in the folder.
1. Create the final query
Section titled “1. Create the final query”- On the Home tab, choose New Source ➔ File ➔ Folder.

- Browse to the folder that holds the workbooks.
- In the window that follows, click Transform Data.
The query lists the files in the folder:

- Remove every column except Name and Folder Path, because these are the only ones the functions need as parameters.
- Rename the query Orders.
2. Invoke the sheet-listing function
Section titled “2. Invoke the sheet-listing function”- On the Add Column tab, click Invoke Custom Function.
- Choose fnAllSheetNames as the function, pass Folder Path as FilePath and Name as FileName, and click OK.

- Click the expand button in the new column’s header, and expand SheetNames:

The query now lists every sheet name in every file, beside the file’s name and folder path:

3. Invoke the sheet-processing function
Section titled “3. Invoke the sheet-processing function”- Click Invoke Custom Function again.
- Choose fnProcessSheets as the function, pass Folder Path as FilePath, Name as FileName and SheetNames as SheetName, and click OK.

letSource = (FilePath, FileName, SheetName) => let Source = Excel.Workbook(File.Contents(FilePath & FileName), null, true), SheetData = Source{[Item=SheetName,Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(SheetData, [PromoteAllScalars=true]) in #"Promoted Headers"in Source- Click the expand button in the new column’s header, and keep all the columns ticked:

Once the three parameter columns, Name, Folder Path and SheetNames, are removed, all the data sits in one table:

4. The finished query
Section titled “4. The finished query”For this example, the finished query’s code looks like this:
let Source = Folder.Files(FilePath), #"Removed Other Columns" = Table.SelectColumns(Source,{"Name", "Folder Path"}), #"Invoked Custom Function" = Table.AddColumn(#"Removed Other Columns", "fnAllSheetNames", each fnAllSheetNames([Folder Path], [Name])), #"Expanded fnAllSheetNames" = Table.ExpandTableColumn(#"Invoked Custom Function", "fnAllSheetNames", {"SheetNames"}, {"SheetNames"}), #"Invoked Custom Function1" = Table.AddColumn(#"Expanded fnAllSheetNames", "fnProcessSheets", each fnProcessSheets([Folder Path], [Name], [SheetNames])), #"Expanded fnProcessSheets" = Table.ExpandTableColumn(#"Invoked Custom Function1", "fnProcessSheets", {"Order No", "Order Qty", "Unit Price", "Total"}, {"Order No", "Order Qty", "Unit Price", "Total"}), #"Removed Columns" = Table.RemoveColumns(#"Expanded fnProcessSheets",{"Name", "Folder Path", "SheetNames"})in #"Removed Columns"The Source step reuses the FilePath parameter as the folder to read, which works here because the sample workbook sits in the same folder as the others.