A recurring task in my previous life as an accountant was to analyse actual vs budget figures.
However the figures were often in different worksheets, different workbooks or even different systems. And the task of consolidating all the data into one table that was ready to analyse with PivotTables was not straight forward.
Not anymore. It’s a doddle now that Excel has Power Query*. So let’s take a look at how to combine Excel worksheets with Power Query.
*Power Query is a free add-in built by Microsoft for Excel. It’s available for all Desktop versions of Excel 2010, 2013 and 2016. Note: for Office 365 users with Excel 2013, Power Query is only available in Office 365 ProPlus, but with Office 365 Excel 2016, Power Query is available to all users.Download the Workbook
Enter your email address below to download the sample workbook.
Watch the Video
Change settings to HD for best viewing.
![Subscribe YouTube](https://d13ot9o61jdzpp.cloudfront.net/images/subscribe.png)
Combine Excel Worksheets with Power Query - Written Tutorial
For this example the data is nicely formatted in Excel Tables in one Excel workbook, with separate sheets for the Actual and Budget figures:
Note: your data might not be so well behaved, but don’t worry. Power Query can get data from almost anywhere, including multiple files, folders, systems etc. More on that later.
Load to Power Query
Next we need to load the two Tables into Power Query. We can use the ‘From Table’ tool on the Power Query tab of the Ribbon to load one Table at a time:
After clicking the ‘From Table’ button the Query Editor opens and you can see a preview of your data:
In the Query Settings change the Name of your Query to something meaningful. I’ll call mine ‘Actuals’:
Click the ‘Close & Load’ down arrow and select ‘Close & Load To’:
And in the ‘Load To’ dialog box select ‘Only Create Connection’:
Repeat for the Budget sheet so you now have 2 queries in your workbook. You can see mine in the Query pane on the right-hand side of the screenshot below:
Append the Queries
Next we need to append the queries together.
On the Power Query tab select ‘Append’:
In the Append dialog box select the two query tables and click OK:
Note: You could Merge the queries (as opposed to Appending). However if you have budget figures without corresponding actuals and vice versa then you must use append, otherwise you’ll lose some data.
The Query Editor will open and you can see a preview of the appended queries. Notice how the Budget column contains a load of null values? This is because the Actuals are at the top, but if I were to scroll down I’d see the budget values.
So far this isn’t a lot better than copying and pasting the two tables under one another. Ideally we’d like the corresponding budget figures to be on the same row as the actuals.
Group Rows
We can use the ‘Group By’ tool to do that. On the Transform tab of the ribbon you’ll find Group By button:
Clicking it opens the Group By dialog box (image below) where you can:
- Add the columns you want to group together (tip: select the columns before clicking ‘Group By’ to save a step).
- Tell Power Query how you want the other columns summarised.
Use the +/- buttons to add and remove columns.
Now you can see a preview of your grouped data:
Notice how the second row has a null budget value? This is because we have an actual for Black Tea in January, but no budget.
Rename Query
I’ll give the query a more meaningful name by typing over the default in the ‘Name’ field in the Query Settings pane of the Query Editor:
Set Data Types
Now we can set the data types before we put the data into an Excel Table or Power Pivot. This is particularly important if you’re putting the data into Power Pivot, as the data types will flow through.
On the Home tab you’ll find the ‘Data Type’ menu. Simply select each column in turn and set the type from the list:
In the Query Settings pane under ‘Applied Steps’ we can see all the steps we’ve taken so far to:
- Append the queries
- Group the rows and
- Set the data types
These steps are saved with the query. And this means if the data in our original tables changes, or we get new data, like Actual figures for the previous month, then we can simply refresh the query and Power Query will get the data. It runs it through all the steps in the query without us having to do a thing!
Close & Load
We’re ready to Close & Load our data to an Excel Table, and or Power Pivot. I’m going to put it straight into a Table in the current file on a new worksheet so at the ‘Load To’ dialog box I’ve selected ‘Table’ and ‘New Worksheet’:
You can see I’ve got a new sheet (Sheet2) with my appended data, and in the Queries Pane I have a 3rd query (Actuals vs Budget) in the file:
Now you can go ahead and analyse the data in PivotTables etc.
Athough there were a lot of steps in this tutorial, I'll think you'll agree it's pretty easy to combine Excel Worksheets with Power Query.
Refreshing the Queries
If any data changes in our Actual or Budget tables we can press CTRL+ALT+F5 to refresh everything, or right-click a cell in the Actuals vs Budget query table and select ‘Refresh’:
Data Sources
In this example the data was conveniently housed in one Excel workbook and nicely formatted in Excel Tables. If your data isn’t so well behaved, fear not, Power Query can get data from almost anywhere, including multiple different sources:
Learn Power Query
I can’t think of a job involving Excel where you wouldn’t benefit from Power Query. It is the most exciting Excel tool since PivotTables, not just because it’s so powerful, but also because it’s useful for everyone.
In my Power Query course I’ll get you up to speed fast, so please take a moment to click here and check it out and see another video of Power Query in action.
Clay
Is there a way to standardize this? Basically I have files that have the exact same amount of columns, but the last column will change every year, i.e. 2014 “column name,” 2015 “column name,” etc. I would like to make it possible for someone to drop a formatted excel file and have this append change it automatically. Just a drag, drop and refresh process if possible. Thanks
Mynda Treacy
Hi Clay,
Yes, that’s the beauty of Power Query. You just set up the query and then refresh to get new data. There are some things you need to watch out for to ensure new columns get picked up, e.g. remove the ‘Changed Type’ step that Power Query automatically inserts for you.
I cover these things in my Power Query course.
Mynda
Frank
merge does the job without grouping
Mynda Treacy
True, but the risk with Merge is as per my note:
“Note: you could Merge the queries (as opposed to Appending). However if you have budget figures without corresponding actuals and vice versa then you must use append, otherwise you’ll lose some data.”
Renato
Ciao Mynda, many thanks for your precious and very useful suggestions!
Let me suggest your foolwers a further step in the process.
I prefer to normalize the database with the “UNPIVOT” function of Powerquery.
This will help manage data at different “granularity”.
For instance, BDG data might have been calculated at Cathegory level instead of Product level.
Ciao a tutti from Venice (Italy)
Renato
Mynda Treacy
Hi Renato,
Thanks for sharing your ideas. You could take it a step further but with this format you can easily add a Calculated Field for the Budget vs Actual variance.
Mynda