Skip to content

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:

  1. Load one of the workbooks into Power Query, and transform it into the shape you want.
  2. Turn that query into a custom function.
  3. Build a new query that lists the files to be combined and calls the function on each one.

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:

The workbook Multiple File Data 1 open in Excel, with a Product column and a column for each month from Jan to Dec

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

Excel’s Get Data menu open at From File, with From Workbook highlighted

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

The Navigator showing Multiple File Data 1.xlsx with Sheet1 selected and a preview of its 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:

The unpivoted query, with Product, Attribute and Value columns, and twelve rows for each product

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.

The Query Settings pane with the query named fnCombine, and Applied Steps from Source to Unpivoted Other Columns

  • On the Home tab, open the Advanced Editor.

Making a function takes two changes to the code:

  • Add a line before let that 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:

The Queries & Connections pane showing fnCombine with an fx symbol, as a connection only

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 Enter Parameters dialog with a folder path ending in a backslash for myPath and Multiple File Data 2.xlsx for myFile

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.

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:

The folder query listing the three workbooks, with Content, Name, Extension, Date accessed, Date modified, Date created and Attributes columns

  • 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])

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.

The Custom column with a Table on each of three rows, and its expand menu open with Product, Attribute and Value ticked

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

The combined query with Product, Attribute and Value columns

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