Welcome to Excel Magic Trick number 1,336. If you want to download this Excel file, Excel Magic Trick 1,336 Connection Only Start or the finished file or the zipped folder so you can follow along, click on the link below the video. We have to talk about Power Query importing Big Data text files, and we need to talk about whether we want to do Connection Only or dump the data into our Data Model before building our PivotTable.
Let's go look at the text files over here in Windows Explorer. There's the folder you should download, and notice it's from the previous Excel Magic Trick, so you have to go to the previous one and download 1335 text files. These are the files, and they're all text files with transactional data. We need to take every one of these text files and stack them on top of each other into a single proper data set. I want to open one of these files, so I'm going to double-click it. Each one of these files has Date, Website, Product, Quantity, Revenue, Discount, and a few other columns, but these are tab-delimited files. Because they're text files, it's pretty easy to import and append all of them into a single table using Power Query. Notice that the file size is huge, 46 megabytes and so on. We have 7 million transactions in these text files, so this constitutes Big Data we're trying to import into Excel.
Now let's go over to our Excel workbook. In 2016, we go to Data, and under Get & Transform, there is Power Query. In Excel 2013 or 2010, you actually have to download Power Query and install it as a separate tab. We're going to go to New Query, From File, and here is this awesome option, From Folder. We need to navigate, so I click Browse, and there is my folder. By pointing Power Query to that folder, it will try to import all files in that folder. Click OK, click OK, and we want to click Edit. This opens up our Power Query Editor. First, we're going to name this query "All Text Files in Proper Data Set" and press Enter.
Each one of these records is data about those files. We don't need any of it because they're text files. We have this Binary column, which we will combine into a single table in just a moment. But first, the Extension column, just in case we have other file types in the folder by mistake. We're going to use the filter here: Text Filters, Equals, and we want to say please only import files from that folder that have .txt. Click OK. Now the only column we need is Content, so right-click Content and Remove Other Columns. Because these are text files, not Excel files with objects, we have a double downward-pointing arrow, and when we click this, it will try to combine all of these.
There are a bunch of steps over here that it did, including Promote Headers, but we have to be careful because it only promoted the very first set of headers, or field names, at the top of the first text file. There are, for example, further down in the Website column, records that list "Website," and if it has "Website" in this row, it also has the other field names like Product and Quantity. So we want to make sure, and come to this column, click the filter, and come down to Load More because we have almost 7 million records we're trying to import. We cannot see a complete unique list, which is always displayed for the filter, so I want to click Load More. Sure enough, down below there is a rogue or mistaken field name. When I uncheck this, it will filter out all the records that contain "Website" in the Website column. Click OK.
For this particular PivotTable, we're only going to need Website, Product, and Quantity. Before I click on Quantity, I'm going to hold the Shift key and click on Quantity, right-click, and Remove Other Columns. This is important because I don't need all those columns, and this is 7 million records, so why not get rid of them if we're only going to use these in the PivotTable? It looks like this: we've got the right data types, text, text, and whole number. These are the little icons that show up at the top when it guessed right. You could also change it by coming up to Data Type.
Now we're going to Close & Load. I'm going to click Close & Load To, and I want to say only create a connection. This means we can build a PivotTable even though the data initially is not stored in this file. I'm going to click Load. Notice it immediately says "Connection Only." It didn't actually have to import it now, but later, when we make our PivotTable, it will. Very importantly, I'm going to press Ctrl+S, and now if you notice, the file name is "Connection Only." I want to hit the F12 key because we're going to create two different workbooks. This one is going to be called, instead of Connection Only, in our Save As file name textbox, I'm going to call this "Data Model," so now we're going to have two different files. Save.
Now I'm going to look over in Windows Explorer. Notice we have the same exact files, and there's the file size. Now I'm going to come back over here, close the Data Model, open up Connection Only, and click Enable Content. Data, Show Queries. We have a Connection Only query over here, and now we want to see how to build a PivotTable from Connection Only. I'm going to click in some cell and Insert PivotTable, or the keyboard shortcut Alt, N, V. The trick is we want to use an external data source and then choose our connection. There is our query, so I can select that and click Open. This is the Create PivotTable dialog box. What's going to happen with Connection Only: I'm going to click OK, and it's going to take a long time, because at this moment, and at any moment we do something in the PivotTable, it's going to have to refresh the data. So I'm going to click OK, and it is going to take a long time because this is a big data set. You already sort of see the moral of the story: Connection Only is good when you have small data sets, but when you have a big data set, the Data Model is going to turn out to be a lot faster.
Here's our PivotTable Field List. Before I even make the PivotTable, I'm going to press Ctrl+S to save. Now I want to look at Windows Explorer at the file size, and there it is. That is frightening. That's the PivotTable cache holding all of that data. Now let's build our PivotTable. I'm going to drag Product down to Rows, Website down to Columns, and Quantity down to Values. You can see each time it's actually taking its time. Watch this. Let's go up to Design, Report Layout, and Show in Tabular Form. Even that's taking a little while. Right-click, Number Formatting. I want Number, use a separator, no decimals. Click OK. Wow, even that's taken a long time. There's our finished report. I'm going to press Ctrl+S and go look at Windows Explorer. It's still about the same file size.
Now let's close this and open up our Data Model file. Remember, in this Data Model file, after we enable and show queries, it's Connection Only, but now I want to dump this into the Data Model. So I'm going to right-click, Load To, and now we open up our Load To dialog, only create a connection, add this to the Data Model, and click Load. What's happening here is it's loading into a columnar database, which is a very efficient means of storing Big Data with a small file size. Even though it's initially taking a lot of time to go through those 7 million records, when it gets into the Data Model, the actual file size and the building of PivotTables will be a lot faster.
All right, so there are our 7 million records. If we wanted to go to Data, Data Model, and I have Power Pivot, so I can go and look. You don't have to have Power Pivot built in to access the Data Model if you dumped a single table in there, but let's go look. I'm going to click there, and there it is. By the way, we imported this through Power Query. If you tried to delete this, it would say you can't. You'd actually have to delete it in Power Query. All right, I'm going to close this. I'm going to click in a single cell. Notice I didn't stay over there in the Power Pivot Data Model window. I'm going to just build the PivotTable right here and access the Data Model: Insert PivotTable, or the keyboard shortcut Alt, N, V. We're not going to use External, but notice that by default it senses that we have stuff in the Data Model, so that button is already selected. Existing sheet, A3, click OK. Now there it is. You can definitely tell that table is from the Data Model, because that little black line is an icon indicating that it's from the Data Model. Let's drag Product down to Rows and Website down to Columns. You can already see how much faster it is when we drag this down. Design, Report Layout, Show in Tabular Form, right-click, Number Format. You don't want to use Format Cells. Number formatting adds number formatting to the actual field, and there I'm going to say Number, separator, zero decimals, click OK.
The moral of the story is that for Big Data, we want to dump data into the Data Model, because it has an efficient columnar database that reduces file size and makes querying and building PivotTables much quicker. If we save and go over and look at the file size, the Data Model is a little over a megabyte, whereas Connection Only was 29. Connection Only is totally cool for small data sets, and one benefit is that you don't have to refresh both the PivotTable and the Power Query import. You just refresh the PivotTable, and it updates.
All right, we'll see you next trick.
