Power Query: Aggregating Aggravating Worksheets
26 April 2017
Welcome to our Power Query blog. Today I look at combining data from tables in several Excel worksheets.
I begin in a workbook that contains three worksheets. Each of the worksheets has been populated with a table of expenses information:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image1.png/459a3248c388d864471e56aa539b2082.jpg)
The initial process in order to start getting data from these worksheets is to go to the ‘POWER QUERY’ tab and create a new blank query, as shown below:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image2.png/69c2b86f2c925712e53b753e8eea90d3.jpg)
In the Power Query Editor, in the formula bar at the top of the screen, I enter the following formula:
=Excel.CurrentWorkbook()
which will list tables (and would list named ranges and workbook connections) that I have in my workbook as follows:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image3.png/19e426b40883d0ea17af790c121ac6ad.jpg)
To keep it simple, my table names are the same as the worksheet names (note that this function will not extract data that is not in a table or named range). At this point, if I had other ranges or tables that I wanted to exclude from my query I could apply filters. The tables listed would include any connections from the workbook, and so this filter will be useful later. For now, I want all of my data, so the next step is to look at what the ‘Expand’ option next to the Content column title does:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image4.png/3a5483b8cae7b55bef9a889dc01cf20f.jpg)
I am given the option of selecting all of the columns from my tables. I don’t need to use the original column name as a prefix, so I uncheck that option. (There is also an option to ‘Aggregate’ instead of ‘Expand’, which is not appropriate for today’s example but this does deserve a blog entry of its own.) I choose to expand my data thus:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image5.png/0fe577580e2ed5b24f95188be0f06ce2.jpg)
Now this data looks ready to load, so that’s what I will try next. All seems well to begin with, until I scroll down and find some unexpected data:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image6.png/2cbae9e920b2655dcb5cf4dd1d84f440.jpg)
In order to see where this has come from, I go back to my query and check out my source step.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image7.png/8b4906486d6b534c7499ad926557085e.jpg)
My query is a connection from the workbook, so it is included in the list of tables! Clearly, this is confusing my output, so I need to get rid of it from the source step. I can filter at this point and remove everything with the name ‘Query1’, but I will have to revise this if I change the name of my query. To show what I mean, I rename my query to ‘Expenses’ and review the options available in the filter of the ‘Source’ step:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image8.png/d7c6bd0e9d35aacf16fe62b11083eded.jpg)
I filter out the Query1 and Expenses tables, and I am left with my expense data which can be uploaded:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image9.png/718e381de244b9f5fd299b5a8fa0a7bf.jpg)
Having chosen which ‘Name’ column to keep, my data appears on a separate worksheet to my other tables:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2017/power-query/pq-21-image10.png/2a816010af08395ac78e8b6cc60e43cd.jpg)
And so, I have my complete expenses list, with rather less rows uploaded this time. Next time I’ll have a look at how to pull in data from worksheets in other workbooks.
Want to read more about Power Query? A complete list of all our Power Query blogs can be found here. Come back next time for more ways to use Power Query!