Power Query: Find the Folder
6 March 2019
Welcome to our Power Query blog. This week, I look at an example which uses some of the M functionality for dealing with files and folders.
Last week, I created blank queries and used M code to extract data from a folder in Power Query. In Power Query: One Folder, One Query, I created a query that contained similar data from a folder, which I could then combine. This time, I am going to use a similar method and take a different path through the options so that I can view the subfolders in a folder.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I begin by creating a new query ‘From Folder’ by choosing this option from the ‘From File’ dropdown in the ‘New Query’ section of the ‘Get & Transform’ functionality on the ‘Data’ tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
Power Query prompts me for the location of the folder and I choose to browse:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
I find the folder which contains my ‘PQ Blogs’ folder.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
In the preview, I can see a list of documents and workbooks, but no folders. Unlike the aim in Power Query: One Folder, One Query, this time I want to see all file types in the folder. I don’t want to combine the data; I want to edit it so that I can look for specific data in the folder.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
Now I have chosen to edit the data, I can see the M code behind the query.
= Folder.Files("C:\Users\kathr\OneDrive\Documents\PQ_StandardExpenses")
This is the same code used in last week’s blog. In this case, I want to use Folder.Contents() instead of Folder.Files(), because I want to see the subfolders, and not just the files in the subfolders.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
The first row shows me the ‘PQ Blog’ folder. The Content column indicates that this is a table, and I can see the contents of that table (the files) by clicking on ‘Table’:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
I want to just show Content which contains a table, which will allow me to only view folders.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
However, I can’t do this via a filter since ‘Table’ is an entity and not a value – instead I need some M code to create a new column which does contain values.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
The M code I have used is
let
Source = Folder.Contents("C:\Users\kathr\OneDrive\Documents\PQ_StandardExpenses"),
FileType =Table.AddColumn(Source,"Folder",each Value.Is([Content],type table))
in
FileType
This creates a Boolean – a column which is TRUE or FALSE – in this case, it is TRUE if Content contains a table. I will look at the function Value.Is() in more detail in next week’s blog.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image11.png/22c6daeb82d7d69ac88f878227e04b28.jpg)
I can then filter on Folder:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image12.png/a1537847463e660a31158c8032525438.jpg)
and I am left with my filtered result, viz.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/118/image13.png/917da985be13220165c8d2823e95344f.jpg)
Come back next time for more ways to use Power Query!