Welcome to Excel Magic Trick number 1412. If you want to download this Excel file, the link is below the video, and you can get the starting file or the finished file so you can follow along. Today we're going to see how to use Power Query to merge two tables into one table for a PivotTable report.
Here are our two tables. If we didn't use Power Query, we could add an extra column to this table and use VLOOKUP to get the region into the transaction table, or we could use the Data Model and relationships. But here we want to see how to actually merge these two tables into a single table. In essence, the outcome will be this extra Region column, which will allow us to summarize the sales by region, and then we want to create a simple PivotTable.
Let's go over to the sheet 1412. In order to use Power Query, your Excel range has to be an official Excel table. I've already converted this to an Excel table by selecting a single cell and clicking the Table button or using Ctrl+T. Then I named it. I'll do that for this table too: Ctrl+T, Enter, and then name it. I go up to Design, Properties, Table Name, or Alt+JTA, and I'm going to call this D_Sales_Rep and press Enter. Now we have our two official Excel tables, and we can merge them.
I'm going to click a single cell in one of the tables. I'm in Excel 2016, and we're going to go to the Data ribbon tab. The latest update has completely changed how we connect and get data. Get & Transform is over here on the left, and Queries & Connections is like our Connections button or our Show Queries button. Whatever version you have, go to Get & Transform and click the button we want, From Table/Range. That brings the table into the Query Editor. We can see over here that it changed the types. Looking at the types, decimal, text, date and time, I want to change these. When I click the icon, I'm going to select Currency, and it asks whether I want to replace the one it already did for me. It thought we wanted decimal, but we really wanted currency. You can see up here it says Type Number. So this is asking whether I want to replace that, and yes, I'll click Replace Current, and we can see that changed up there. Similarly, I'll click the icon and choose just Date instead of Date and Time. It asks the same question, and I'll click Replace Current.
Here's something new with this update. When we come up to Close & Load, we're used to clicking Close & Load To and getting a dialog box, but they have changed it. The old Import Data dialog had only "Create a connection" and "Add to the Data Model," which we're not going to use in this video. What we want is "Only Create a Connection." In essence, Microsoft has merged the Import Data and our old Load To dialog boxes into this new dialog box. I'll click OK. Now we can see our Queries & Connections pane opening over here. If we had connections, we'd go back over there to External Sources.
Let's bring our second table in. I'll click a single cell in the range, from table/range. The data types are fine. I'll click Close & Load, then Close & Load To, choose Only Create a Connection, and click OK. Now we have our two queries.
Next we use the Merge feature. Come up to Get & Transform Data, Get Data down here, Combine Queries, and we want Merge. I'll select F_Sales, and the Sales Rep column is the one that connects these two tables, so I select Sales Rep. Come down and get my second table, the lookup or dimension table, and click on Sales Rep. We can see down here it's matched. We're going to use a Left Outer join, which means all the records from the first table and the matching ones from the second. Notice there are two columns here, so when the resultant table shows up, both columns will appear as a little table for each record. I'll click OK, and here's our new column. We can see the little tables. For each record there are the two fields from our D_Sales_Rep table.
Now I want to double-click this and not call it New Column. I want to call it Region, and press Enter. Next I'll click the expand button, and I definitely do not want all of them. First I'll uncheck "Use original column name as prefix," then uncheck Select All. I only want Region, because if I selected both we'd have duplicates, and we don't want that. Click OK. The data type is fine. I'll double-click this, press Backspace, Backspace, and Enter. Now we have the columns we need.
I'll click Close & Load To, and I'm not going to dump this new merged table into the spreadsheet, because then I'd have duplicate data. I'll choose Only Create a Connection. These two tables will still live as separate tables, but through Power Query and the connection we will have merged them, and we can use the result in a PivotTable report. Click OK. There's my merged query. I don't like that name, so I should have renamed it. Right-click, Properties, and at the top I'll call this one Sales_Table and press Enter.
Now I'll simply come off to the side, or wherever I want, and go to Insert, PivotTable, or use the keyboard shortcut Alt, N, V. Check this out: we're going to say use an external data source, choose Connection, and there is our Sales_Table query, which we are allowed to access for a PivotTable. Click Open, then OK. Now I'll close the Queries & Connections pane. I can drag Region down to Rows and Sales down to Values. Design, Report Layout, Show in Tabular Form. Right-click, Number Formatting, and choose something like Currency, then click OK. There we have it: we merged the two tables and created our regional sales report from that merged query.
That was a little exercise with Power Query, merging to create a single table as a connection that we can use in a PivotTable. We'll see you in the next video.
