Skip to content

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.

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.
  • Choose Get Data ➔ From File ➔ From Workbook.
  • Select one of the workbooks. In this example there is one workbook per year:

A folder holding three workbooks named 2018, 2019 and 2020

  • In the Navigator, click Transform Data.

The query lists the workbook’s sheets, one per row:

The Source step, with a row for each sheet from Jun to Sheet6 and columns Name, Data, Item, Kind and Hidden

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

The Filter Rows dialog keeping rows where Name does not begin with Sheet

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

A single SheetNames column listing Jun, May, Apr, Mar, Feb and Jan

  • 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:

The Excel source dialog in Advanced mode, with the type menu of the first file path part open at New Parameter

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

The Manage Parameters dialog creating FilePath, with the folder path as its current value

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

The source dialog with FilePath in the first part, and the type menu of the second part open at New Parameter

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

The Manage Parameters dialog creating FileName, with 2018.xlsx as its current value

  • In the Queries pane, right-click the query and choose Create Function:

The right-click menu on the 2018 xlsx query, with Create Function highlighted

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

The Queries pane with a fnAllSheetNames group holding the 2018 xlsx query, the FilePath and FileName parameters and the fnAllSheetNames function

  • Finally, rename the query 2018 xlsx to SingleWorkBook.

The fnAllSheetNames group with the query renamed SingleWorkBook

The SingleWorkBook query stays in place, because editing it changes the function built from it.

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.

  • Duplicate the SingleWorkBook query, and rename the copy SingleSheet.

The Queries pane with the new SingleSheet query in the fnAllSheetNames group

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

The filtered list of sheets from Jun to Jan, with the Name, Data, Item, Kind and Hidden columns

  • On the Home tab, click Manage Parameters.
  • Create a third parameter called SheetName, with a Current Value of Jun.

The Manage Parameters dialog creating SheetName, with Jun as its current value

  • 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.

The SingleSheet query’s Applied Steps, Source, Filtered Rows and SheetName

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

The Queries pane with a fnProcessSheets group holding the SingleSheet query, the SheetName parameter and the fnProcessSheets function

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.

  • On the Home tab, choose New Source ➔ File ➔ Folder.

The New Source menu open at File, with Folder highlighted

  • Browse to the folder that holds the workbooks.
  • In the window that follows, click Transform Data.

The query lists the files in the folder:

The folder query listing 2018.xlsx, 2019.xlsx and 2020.xlsx, with Content, Name, Extension, Date accessed and Date modified columns

  • Remove every column except Name and Folder Path, because these are the only ones the functions need as parameters.
  • Rename the query Orders.
  • 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.

The Invoke Custom Function dialog calling fnAllSheetNames, with Folder Path for FilePath and Name for FileName

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

The expand menu on the fnAllSheetNames column, with SheetNames ticked

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

The query with Name, Folder Path and SheetNames columns, listing the six month sheets for each of the three workbooks

  • 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.

The Invoke Custom Function dialog calling fnProcessSheets, with Folder Path, Name and SheetNames for its three parameters

let
Source = (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:

The expand menu on the fnProcessSheets column, with Order No, Order Qty, Unit Price and Total ticked

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

The combined Orders query with Order No, Order Qty, Unit Price and Total columns

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.