Sub topic 2
Connecting to a folder
This chapter covers the first step of every folder-combine workflow: pointing Power Query at a folder and getting back a list of its files. It assumes you can use Excel tables and the Data ribbon, and that you know what CSV and .xlsx files are. You need Excel 2016 or later, or Microsoft 365, with Power Query built in. The chapter ends with the file list in the Power Query editor. Later chapters cover reading, filtering and combining it.
What the From Folder connector returns
A connector is the part of Power Query that knows how to read one kind of data source. The From Folder connector does not read the data inside your files. It returns a table with one row per file. Each row holds the file's name, its location and its dates. It also holds the file's raw contents as a binary value, which Power Query has not parsed yet.
This is why combining files takes several steps. First you get the list of files. Then you choose which files to keep. Last, you parse each file's contents and append the results into one table.
Steps to connect
- Open a new workbook, or the workbook that will hold the combined result.
- On the Data tab, choose Get Data, then From File, then From Folder.
- In the dialog that opens, click Browse and select the folder. You can also paste a path into the box.
- Click Open (or OK). Excel shows a preview window listing the files.
- Choose one of the buttons at the bottom of the preview.
The preview window offers these buttons:
| Button | What it does |
|---|---|
| Combine & Transform Data | Builds helper queries that parse and append every file, then opens the editor |
| Combine & Load | Does the same, then loads the result straight to a worksheet |
| Load | Loads the file list itself to a worksheet, with no combining |
| Transform Data | Opens the Power Query editor on the file list so you can work on it first |
For this chapter, choose Transform Data. It leaves you with the file list in the editor and no helper queries. You will use the combine buttons after you have filtered the list, which the next chapters cover.
On older builds of Excel 2016, the Get Data menu is under New Query instead. The sequence of choices is the same.
The M code behind the connection
When you click Transform Data, Power Query writes a query in a language called M. Open the Advanced Editor (Home tab, then Advanced Editor) to see it. The query for a folder looks like this:
let
Source = Folder.Files("C:\Data\Sales")
in
SourceFolder.Files takes a folder path as text and returns the file table. The query has one step, named Source. This step appears in the Applied Steps pane on the right of the editor. You can use the gear icon next to it to change the folder path later.
The spine's first example goes one step further and filters this list. The filter is covered in a later chapter, but here is a preview of what it looks like:
let
Source = Folder.Files("C:\Data\Sales"),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Kept = Table.SelectColumns(CsvOnly, {"Name", "Content"})
in
KeptEvery folder query starts with the connection step shown above. Everything after it is a transformation of that table.
Subfolders are included
Folder.Files is recursive. It returns files from the folder you chose and from every folder nested below it. This catches many people out. If your Sales folder has an Archive subfolder with old exports, those files will appear in the list and will be combined with the rest.
You can see where each file lives in the Folder Path column. Two files with the same name can appear in different subfolders, so the name alone does not identify a file.
There is a second function, Folder.Contents, that behaves differently. It lists only the immediate contents of one folder. Subfolders appear as rows with an empty Extension and a table in the Content column instead of binary data.
| Function | Includes subfolders' files | Lists subfolders as rows | Used by From Folder |
|---|---|---|---|
| `Folder.Files` | Yes | No | Yes |
| `Folder.Contents` | No | Yes | No |
If you want only the top-level files, you have two options. You can keep Folder.Files and filter on Folder Path. Or you can write the query with Folder.Contents and filter out the rows where Extension is empty. The first option is simpler to read and is the usual choice.
Paths: local, network and cloud
The path you give must be reachable from the machine running Excel. These cases work differently:
- Local paths, such as
C:\Data\Sales, work as shown. - Network shares work with a UNC path such as
\\server\share\Sales. A mapped drive letter such asZ:\Salesalso works on your machine. However, the letter may not exist on a colleague's machine, so the query fails for them. A UNC path is safer for shared workbooks. - OneDrive and SharePoint folders that sync to your computer have a local path, and From Folder reads that path. The local path usually contains your user name, so it differs between people. Parameterizing the folder path, covered in a later chapter, addresses this.
- SharePoint sites accessed online are not read with From Folder. They use a separate connector, From SharePoint Folder, which takes a site URL. It returns a similar table, and the techniques in this tutorial apply to it as well.
Power Query reads only what your Windows account can read. If you lack permission on part of the folder, the refresh fails with an access error rather than returning a partial list.
Privacy levels and credentials
The first time you connect to some sources, Excel may display a privacy level prompt. Power Query uses privacy levels (Public, Organizational, Private) to decide whether data from two sources may be mixed in one query. A plain folder query uses one source, so you usually do not see this prompt. It appears when a query combines the folder with another source, such as a web call or a database.
For a local folder, no password is needed. Network and SharePoint sources may ask you to sign in once. Excel stores the credential and reuses it on refresh.
Common problems at this stage
The following problems show up when connecting, before any combining happens.
- The preview is empty. The path is wrong, or the folder has no files. Check for a typo, and check that the folder is not empty.
- An access error appears. Your account cannot read the folder. Ask for read permission or choose another location.
- The list contains files you did not expect. Look at
Folder Pathfor subfolders, and atNamefor hidden or temporary files. Excel creates a temporary file with a name starting with~$while a workbook is open in the folder. It appears in the list and then breaks parsing later. - The connection works on your computer but fails on another. The path is probably specific to your machine, such as a mapped drive or a user profile folder.
Checking your connection
After the editor opens, confirm these things before moving on:
- The query name in the Queries pane on the left matches the folder name. You can rename it by double-clicking.
- The Applied Steps pane shows a single
Sourcestep. - The table has the columns
Content,Name,Extension,Date accessed,Date modified,Date created,AttributesandFolder Path. - The row count matches the number of files you expect, including those in subfolders.
The Content column shows Binary in each row. Clicking the word Binary opens that file's data as a table, which Power Query parses with a guess at the file type. Do not rely on this for the combined result. The next chapters explain the columns in more detail and show how the combine step handles the contents properly.
Exercise
Your folder C:\Data\Sales holds 12 CSV files, and an Archive subfolder inside it holds 30 more. You connect with From Folder and choose Transform Data. How many rows does the file list contain, and which function produced them? Which column would you check to tell the two groups apart?
Show answer
The list has 42 rows, because From Folder uses Folder.Files, which includes files in all subfolders. The Folder Path column tells the groups apart. Files in the Archive subfolder have a path ending in Archive\, and the other 12 have the path of the Sales folder itself.
Exercise
You build a query on Z:\Sales, a mapped network drive. A colleague opens the workbook and refresh fails. What is the likely cause, and what change makes the path work for both of you?
Show answer
The drive letter Z: is probably not mapped on your colleague's machine, or maps to a different location. Replace it with the UNC path to the share, such as \\server\share\Sales, by editing the Source step with the gear icon or in the Advanced Editor.