Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

ibarrau

[PowerQuery] Decompress .zip Files

The first thing we need to know before writing the code is that zip extensions are more complex than gzip. The extension has different compression methods which will make it difficult to read from Power BI. Even though the key will be a Power Query function that I have found many years ago that works for 90% of the cases, I don't think it will always work. It hasn’t fail for me yet.

In order to read a file, we will add a custom function, because Power BI doesn't have a decompress zip function by default. We have to create it.

Let's check my GitHub to get the code.

https://github.com/ibarrau/PowerBi-code/blob/master/PowerQuery/ExtractZIP.pq

Then, inside Power BI Desktop, get data from a blank query to copy the code we have just seen in GitHub in order to create the function.

copying zippqfunctioncopying zippqfunction

As you can see, my custom function name will be DecompressZip(). Now that we have the custom function, we need to execute it for a file. We can use the same example of a gzip file with a small difference in the steps, running a function that contains a file as a parameter but without specifying a compression method.

The first step is reading the file. We can get data from a csv and pick up "all files" in the drop-down extension list or modify the code manually from blank query, if you prefer writing it. In any case, it should look like this:

 

= File.Contents(“C:\Datasets\Energy Data.zip”)

ibarrau_1-1598362842060.png

 

Second Step, add a custom step that will decompress the zip file from the previous step.

= DecompressZip(Origen)


finaldecompresszipfinaldecompresszip

This way, we obtain a similar result like connecting a folder in filesystem. It allows us to explore the content to combine files as one table or to just take one of them.

The next action you choose for Power Query is your own. Now you know how to read a .zip file. I hope it doesn't fail for you and you find it useful.

 

Original Spanish Post

Comments