I’ve always been a big fan of the Import from Folder feature in Power Query. So much so, that I wrote my first post on the topic way back in February of 2015. It’s still one of most popular posts on Excel Unplugged and has been commented on 57 times and counting. I also did a follow up post in May of 2016 about Importing from multiple Excel files in one folder, and this one also quickly took it’s place among the 10 most read posts on Excel Unplugged. Just like with the first post, this one also got commented on quite a lot.
Since consolidating many files from a folder into one consolidated range is obviously a hot topic, but even more so because I keep getting questions on the same snags of the process, I decided to write a post on what I do to make the process “bulletproof”. This is paramount when you plan to load the result to the Power Pivot Data Model where a failed query could result in a corrupted Data Model.
Imagine you get monthly reports in Excel format. To consolidate them, you plan to create a Power Query function as I described in the articles mentioned above and then apply that function to every file in a folder that you would then monthly add files to. This all sounds good in theory but in practice many things can happen in a folder. Someone might put their favourite song by the Beatles in that folder, or maybe a picture of themselves on vacation… Imagine applying your function to any of those. So, bellow is our folder, with the monthly reports but notice the irregularities.
1. The three non-excel files
2. The XLSX (capital letters) extension of the February report
When we do a Get Data/From File/From Folder and select the folder above, we get something like this
Of course, we choose Edit and enter the Query Editor. We focus our attention on the Extension column.
Now I’m guessing a large percentage of users would just go wild with the filters, but the smart move would be to first select the Extension column, then choose Transform/Format/UPPERCASE (could have gone for the lowercase too). With this we solve the XLSX vs. xlsx problem. The reason this is a problem that needs solving, is because Power Query is case sensitive and treats XLSX and xlsx as two different extensions. Now we know that whatever the case was, now all extensions are uppercase.
Now we are ready to do some filtering. And here again, things could get tricky. One might only deselect the Select All and then check the XLSX extension but that could be perceived wrongly by Power Query, so the right way is Filter/Text Filters/Equals…
We write .XLSX. Be careful of the dot preceding the extension.
And here we go. The list of our Reports. We could also Filter the Name column and say we only want files whose name contains Report and get rid of other unwanted Excel files.
Now we are at the “Applying the function to all remaining files in the Folder” step. And since the Function is nothing more than a series of transformation steps that worked on one file, there is no guarantee that they would work on other files in a folder. All automation projects tend to become a hunt for sporadic changes in file contents. I had quite a few great VBA projects fail over one invisible space added to the name of the Sheet or something even more bizarre. So if we have a function there is a chance that it might fail to do the desired transformations on one file or the other. Let’s simulate this and show how to handle this sort of errors.
To keep things simple, we will have an anomaly of one report that will have a data on a Sheet named differently than in all the other reports. Since our function will be written to work with the regular reports it will return an error when applied to the “black sheep” file.
Just to keep things “organized” the “black sheep” will once again be the February Report. And to keep things simple, all our function is supposed to do is to get the value of cell A1 from sheet called Special Sheet from all Reports in the target folder. Invoking the function will get me this.
All A-Okay except for the February Report. Now there are two ways how to handle this. Number one is a Crude but Effective solution.
Warning: using this method will result in data loss! (Sounds almost like Power Query when you change a load destination ???? )
At this point we know that there are rows that result in an error. To make matters worse, this error is returned by one whole file. But at this point we can select the Column with errors and select Home/Remove Rows/Remove Errors.
And there it is…
No more errors and the data is ready to load. But again, this means that we lost the February data.
The lower case in the title is intentional and very important since Power Query is case sensitive.
This will be a different approach. Sort of a IFERROR from Excel. We know why the function we had failed. It was because of the Sheet name. So we create a copy of that function and change the name of the Sheet to whatever it is in the February file.
At this point we have two functions, each fitted to a specific case.
But now we want to use them both in the same step. This will require some custom code change. The right tool for this is the Advanced Editor. You can find the command on the Home tab, but it seems way cooler to select it from the View tab ????
Now we see the entire code of the Import From Folder query. The highlighted line in the image bellow is the one returning errors.
This is the code
#"Invoked Custom Function" = Table.AddColumn(#"Merged Columns", "fnGetReportSpecialSheet", each fnGetReportSpecialSheet([Merged]))
It basically tells Power Query to add a column by invoking the function fnGetReportSpecialSheet (not the best name for a function I know) on column Merged that has our Filepaths to all the files in the folder. Now we will customise this by changing the last part to
each try fnGetReportSpecialSheet([Merged]) otherwise fnGetReportSheet1([Merged]))
So try to do the first and if it produces an error, use the second function.
Pure Brilliance!
Let’s look at some crucial tools for creating polished ...
The Excel SEQUENCE function In this article, we’ll ...
Last year, Microsoft announced the introduction of a new group of ...
Today is most definitely one of the most exciting days of this ...