Combining workbooks (with a function)
This method combines the same sheet from several workbooks, and suits cases where each file also has to be reshaped on the way in.
It has three parts:
- Load one of the workbooks into Power Query, and transform it into the shape you want.
- Turn that query into a custom function.
- Build a new query that lists the files to be combined and calls the function on each one.
1. Load and reshape one workbook
Section titled “1. Load and reshape one workbook”In this example there are three workbooks with the same layout but different names. Each holds a product number in the first column and a column of figures for each month:

- In a new, blank workbook, choose Get Data ➔ From File ➔ From Workbook, and select one of the source files.

- In the Navigator, select the sheet that holds the data, and click Transform Data.

- Apply whatever transformations the data needs.
Here, the month columns are unpivoted, which turns them into one row per product and month, with an Attribute column holding the month and a Value column holding the figure:

2. Turn the query into a custom function
Section titled “2. Turn the query into a custom function”So far the query works on one particular file. Turning it into a function lets it process any file with the same layout.
- Rename the query to a name that marks it as a function and is easy to remember, such as fnCombine.
Power Query is case-sensitive, so the function must be called later with exactly the same capitals.

- On the Home tab, open the Advanced Editor.
Making a function takes two changes to the code:
- Add a line before
letthat names the function’s parameters, here(myPath, myFile) =>. It means that the function takes a folder path and a file name and passes them to the steps that follow. - In the Source step, replace the hard-coded file path, including its double quotation marks, with the two parameters joined by an ampersand,
myPath&myFile, without quotation marks.
The code then looks like this:
(myPath, myFile) => let Source = Excel.Workbook(File.Contents(myPath&myFile), null, true), Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Product", Int64.Type}, {"Jan", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}, {"Apr", Int64.Type}, {"May", Int64.Type}, {"Jun", Int64.Type}, {"Jul", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product"}, "Attribute", "Value") in #"Unpivoted Other Columns"- Click Done, then choose Close & Load.
The Queries & Connections pane now shows the query with an fx symbol beside its name, which marks it as a function:

3. Test the function
Section titled “3. Test the function”It is worth testing the function before using it.
- Right-click the function in the Queries & Connections pane and choose Invoke. The function asks for its two parameters.
- Copy the folder path and the file name from File Explorer. The folder path must end with a backslash, which File Explorer leaves off when you copy the path, so add it by hand.

The function should process Multiple File Data 2.xlsx correctly, even though the query was first written for Multiple File Data 1.xlsx.
- Choose File ➔ Discard & Close, so that this test query is not saved.
4. Combine all the files
Section titled “4. Combine all the files”The last query lists the files to be combined and passes each one to the function in turn.
- Choose Get Data ➔ From File ➔ From Folder, browse to the folder that holds the files, and click OK, then Transform Data.
The query lists the files with many columns, but only two are needed, Folder Path and Name:

- Select the Folder Path and Name columns, right-click and choose Remove Other Columns.
The query now holds everything the function needs.
- On the Add Column tab, click Custom Column, and enter a formula that calls the function with the two columns as its parameters. The two columns are separated by a comma, not joined with an ampersand, because they are two separate parameters.
![The Custom Column dialog with the formula fnCombine([Folder Path], [Name])](/_astro/combining-workbooks-function-9.BbQKS887_2r6MqL.webp)
After you click OK, the new column holds the result of fnCombine for each file, as a table.
- Remove the Name and Folder Path columns, which are no longer needed.

- Click the expand button in the Custom column’s header, keep all the columns ticked, and clear Use original column name as prefix.
After you click OK, the data from all the files is appended into one table:

- Finally, click Close & Load to load the result to Excel.
This example follows Matt Allington’s post Combine Excel Workbooks with Power Query – Method 1 on Excelerator BI.