Power Query Get Files from a Folder

If you receive your data in separate daily, weekly or monthly files then before you can even begin analyzing the data, you need to consolidate it into one table.

In the past we may have written a Macro to automate this process, but if you didn’t have Macro/VBA skills then you were stuck doing it manually.

Nowadays we can automate this process with Power Query, and the best part is you don’t have to be a programmer to use it because there is a nice ribbon interface, so it’s point and click. And when you get the next period’s file you simply refresh Power Query and it grabs the new data and adds it to the consolidated table for you.

Once we’ve consolidated our files with Power Query we can then load the data into an Excel worksheet, or the Power Pivot data model in Excel or Power BI.

Let’s look at an example. I promise there is no programming required!

Download the Files

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.

Please enter a valid email address.

Download the Excel Workbook. Note: This is a zip file including an Excel workbook with step by step instructions and the csv files you can use to practice this technique. It does not include the query demonstrated in this post because those files are on my PC.

Power Query Get Files from a Folder - The Data

In the folder below, you can see I have 7 CSV files (tip: you can also get Excel files and Text files from a folder):

Important Point: The folder should only contain files that you want to consolidate. That said, you can pick and choose the files you want to import, it’s just safer and cleaner to keep the folder dedicated to your source data files. Trust me.

In this example each CSV file contains 3 columns of data, as you can see below in the January 2017 file:

Important Point: My files have 3 columns of data, but you can import more. However, each file must contain the same number of columns, with the same names in the same order. If your data is an export from another system, then this should be the case anyway.

Power Query Get Files from a Folder – Excel Steps

The Power Query location is slightly different in Excel 2010 and 2013 compared to Excel 2016, plus Microsoft are updating Excel 2016 all the time. I’ll point out menu differences where relevant, but be prepared for them to change in the future. In fact, some versions of Excel 2016 will appear slightly different to my screenshots, particularly if you’re not on an Office 365 license!

Step 1: Start the Query process.

Excel 2016: Data tab of the ribbon > Get Data > From File > From Folder:

Excel 2010/2013: Power Query tab of the ribbon > From File > From Folder:

Power BI Desktop: Home tab of the ribbon > Get Data > More > File > Folder:

Note: From here on the process is the same in all versions of Excel & Power BI.

Step 2: Specify the Folder

In the ‘Folder’ dialog box browse to your folder location, or paste it in and click OK:

Step 3: Select Files

At the next dialog box you’ll see a list of the files in the folder. At this stage you can choose to Load right away, or Edit, or Combine & Edit, Combine & Load, or Combine & Load to (which allows you to specify where you want the data loaded).

I always choose ‘Edit’, which is the same as Combine & Edit, but only requires one click. It’s the small wins that count 😉

I like to edit my query to make sure the data types have been set correctly, plus it gives me an opportunity to clean the data or add additional columns, and give the query a sensible name before loading it:

Step 4: Expand Content

Clicking Edit in step 3 launches the Power Query Editor window and displays a list of the items in the folder:

Tip: Notice there are some useful columns included in this view that you might want to make use of. More on that another time.

In this window you can filter out any files you don’t need. I want them all so I’ll click the double down arrow on the Content column to extract the data from each of the CSV files:

Step 5: Select Settings

Clicking the double down arrow on the Content column will launch the Combine Files dialog box where you can see a preview of the data and:

Specify which file you want Power Query to use as the example file.

Choose the File Origin; this helps Power Query understand what format the dates and currency values will be in etc.

Choose the Delimiter

And whether the first 200 rows will be an adequate sample size

Once you’re happy with the settings, go ahead and click OK

Step 6: Combine Files

Power Query gets the data from the files you selected in step 4 and consolidates them into one table:

On the left (image above) is a list of queries that were automatically generated by Power Query when you clicked the Ok button in step 5. There are two items in this list that are important to you:

This is the sample file query. It’s this query that Power Query uses as a template for consolidating all of the files into the final query. This is the query to modify with any changes you want applied to all of the files in the folder.

This is the final query that consolidates the files into one table. This is the query currently displayed in the preview. Modify this query with any final changes or additional columns you want to add etc. prior to loading the data into a worksheet or the Power Pivot data model. E.g. you might want to delete the Source.Name column if you don’t need it, plus it’s always a good idea to make sure the data types for the columns are correct.

On the right-hand side of the Query Editor window (image below) you’ll see the Query settings for the selected query:

I recommend you replace the default query name to something more useful. Just type it in the Name field and press ENTER. I’ll call mine ‘consolidated_data’.

This is a list of the steps that have been applied to the data to reach the point you see in the preview.

Step 7: Load Data

When you’re happy with the data in the final query you can load the data.

Power BI Desktop: Home tab > Close & Apply:

This will add the data to the Power BI Data Model (Power Pivot).

In Excel: Home tab > Close & Load:

Close & Load will load the data to the default location, which is usually an Excel Table in a new worksheet in the Excel file containing the query.

If you want to choose a different location or load it to the Power Pivot data model then choose ‘Close & Load To…’. This opens the Import Data dialog box (image below) with the defaults selected, which you can edit:

I’ll click OK and you can see the data has been inserted into a new sheet; ‘Sheet1’ and formatted in an Excel table ready for me to use:

Notice that the data goes up to July 2017.

Updating the Query with New Data

As you can see in the image above, the data we currently have is for the period January to July 2017. When August’s CSV file gets added to the folder:

Simply right-click any cell in the Table > Refresh:

Power Query goes to the folder, gets the new file (actually it gets all files again), runs them through the steps you set up to clean and transform the data and then adds it to your Excel Table:

I know, it sounds too easy, but honestly that’s all you need to do.

Note: If your data has been loaded to the Power Pivot data model then you can also right-click the query in the Queries & Connections pane in the right-hand side of your Excel window > Refresh (if the Queries & Connections pane isn’t visible you can enable it on the Data tab of the Ribbon):

Or click the Refresh All button on the Data tab of the ribbon (note; this will refresh everything!):

HI Mynda,
I have a problem… my problem is not about combining data from csv. files. My problem is about using the combined data in Pivot Table. The problem arises when I create a Pivot Table that connects to the combined Data (>1M rows of data). I’d got an error message saying something like “Problem in obtaining data”. I am using Excel 2010 (32 bit) with Power Query installed. Not sure if you have encountered this kind of problem? Appreciate your advice.

I have to go through combine and edit if I want to retain my file name – I then use power query to pull the first part of my file name as part of my data. If I go straight to edit, I lose the file (source) name. It’s not a big deal, but do you have any idea why that happens? I have excel 2016

Resources

Affiliate Program

Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.

✕

Hang On, Don't Go Just Yet.

As a thank you for visiting how would you like a10% Discount Code to use with any of my courses?