transcript

excelisfun - Create Excel Master Table from Sub-Tables Using Power Query. Excel Magic Trick 1834

Thumbnail of the video “excelisfun - Create Excel Master Table from Sub-Tables Using Power Query. Excel Magic Trick 1834”
Download Markdown
Community upload. AI output has not been independently verified.

Introduction

We are tracking rental fees for play fields in Oakland, and we have one, two, three Excel tables. We want to consolidate them, or append them, stacking them one on top of the other on the master sheet. In the olden days, before we had Power Query, we had to do this with array formulas, and guess what? I have lots of videos on that, and you can search my channel if you want to do it the hard way. But since Excel 2013 we have Power Query, and we have one, two, three Excel tables, and we're going to use the Excel.CurrentWorkbook function.

Now, what this function will do is import all of the tables and also all of the other objects. So we have a lookup table, and I definitely do not want it in this workbook, so we'll have to somehow exclude that. We also have a defined name. In addition, there will be one other easy-to-deal-with problem: if we consolidate all of that data on this sheet right here, Power Query exports it as an Excel table. So later, when you refresh, it will try to refresh itself, meaning the records will double. Again, there's a simple solution for that.

Create Master Table from Sub Tables using Power Query Excel.CurrentWorkbook Function. Learn about pitfalls.

I'm going to use Alt+F12 to open up the Power Query Editor, and here it is. Instead of doing a new blank query and typing the code, I've got to show you a great trick when you're using Excel.CurrentWorkbook. Notice we have a table here, and we could go up to Data, Get & Transform, and if I click From Table/Range, it uses the Excel.CurrentWorkbook function. You can click that button, but I'm using the keyboard. You've got to find the right-click key on your keyboard: right-click, G. That's the fast way to import an Excel table.

Next, I change the type and look it up in the formula bar. Excel.CurrentWorkbook is looking up, amongst all the objects in this Excel workbook, that particular table. So instead of you using that, I'm just going to delete the lookup part. Delete, and Enter, and now we have just Excel.CurrentWorkbook, which is programmed to import. If I click here, there's the table right here. We want to exclude this and that defined name. Normally, when I use Excel.CurrentWorkbook, I make sure I only have tables in the workbook I'm using, but in this case I need these two objects, so no problem. I come up to the drop-down, and when I uncheck both of these, we're going to look up in the formula bar at the code it writes. Click OK.

Table.SelectRows is like the Filter function in Excel, and it says I'm going to exclude from the Name column the code and lookup. Notice it says "not," which means if we add any more field tables later, those will be imported and incorporated. Now we have two steps, and we want to consolidate. This is amazing, and guess what? We have the actual table name, which we want in each row of our resulting table. So when I click the Expand button, I definitely do not want to use the original column name, Content. I just want all of these columns.

One other thing: when you're doing this, the structure of each table had better be the same. That means they have the same number of columns, and the field names are all the same. Click OK. That is the thing of beauty.

Now I'm going to close this, and we can see we have all of our fields. I'm going to select the first one, shift-click the last one, go up to Transform, and Detect Data Types. It actually didn't work, but it added a step, so now I'm just going to change the ones I want. Click the data type icon, and I want Date. It's going to ask me if I want to replace, which means it'll amend the code for that step. Replace Start Time and Shift End Time: right-click, Change Type, and that should be Time.

Now let's name this, because this name is going to be really important. I'm going to call it Master Fee Table. Normally I just filter this name out, because, like I said, when we export this to the worksheet it becomes an Excel table. But I do want to show you what happens and how you can tell that there's a table you don't want before you get into trouble. So we go to Home, Close & Load, Close & Load To, Existing Worksheet, B2, click OK, and go over to Master. That's how you can tell there's a ghost row. I'm going to show you this: right-click, Refresh, and everything doubles. So we don't want that.

Recursion Problem, How to Solve.

So we go back up to our query and double-click. You could add an extra step here by going to the Name column and just filtering it out, but I'm going to go back to Filter Rows, and I can see the code up here. All I need is Name, and that I'm going to copy, just because I don't like typing the "and" operator. In Power Query, the and operator is lowercase. Control+V, click over here, double-click, copy, click up in the formula bar, double-click, Control+V, and Enter.

Now, if I go to the last step, I no longer have it. When I close and load, I no longer have the duplicate records, and there is no ghost row.

PivotTable Report from Master Table

Now let's make a PivotTable. Select a single cell, Insert PivotTable, From Table/Range, or use the keyboard: Alt, N, V, T. Existing worksheet, J2, click OK. Rename B, add some number formatting, click OK.

Now we're going to notice ABC amount. Because I'm going to add a new record, and this is an Excel table, I type 1/9, and when I hit Tab the formatting carries down. This is for ABC, they rented it from 2 PM to 6 PM. That formatting is wrong, so I go up to the drop-down. In general, I'm not quite sure how that happened. 250. When I come over to Master, I don't see it here, but right-click, Refresh, and sure enough, there it is. PivotTable: right-click, Refresh, and bam, everything updates.

Load Directly to Pivot Table to avoid recursion and double refresh.

Now here's your bonus. The goal of this video was to create a master table, but what if you didn't need the master table? You just wanted to analyze one, two, three tables in a PivotTable. Well, watch this. I'm going to highlight everything, all of this, Home, Clear All. That clears everything: the data, the query, the PivotTable, the formatting. Or you can use the keyboard, Alt, E, A, A. It says I'm going to delete some queries and other things, so I'm going to click Yes.

Now look what happens up here: connection only. Right-click, Load To. Now it's just keeping the query, but it hasn't loaded it anywhere. Instead of loading it as an Excel table, I'm going to load it as a PivotTable. I don't want it in A1; I want it in B2. Click OK. Now I have Renter, Fee, some number formatting, and there's an added benefit to this. If I come over and add a new record, 1/10, Control+apostrophe to copy the thing directly above, Tab. I'm going to do the same thing: Control+apostrophe, Tab, Control+apostrophe. The benefit of going over to this Master sheet is that now I don't have to right-click the table and the PivotTable. I just right-click, Refresh, and bam, everything's updating.

Summary

All right, that's a little bit about how to use Excel.CurrentWorkbook, an amazing Power Query function to consolidate multiple tables either into a single table, then a PivotTable report, or just into a PivotTable.

Closing, Video Links

All right, we'll see you next Excel magic trick. Foreign.