Sub topic 14
Making the folder path easy to change
This chapter assumes you can already combine files from a folder with Combine & Transform Data and that you can open the Power Query Editor and the Advanced Editor. Here you replace a hard-coded folder path with a parameter, so you can point the query at a different folder without editing M code.
Why the path is a problem
A query built with From Folder stores the path as text inside the Source step, for example C:\Data\Sales. The path has to change when you move the files, share the workbook with a colleague whose drive layout differs, or switch from a test folder to the real one. Editing the Source step by hand works, but it is easy to forget, and it is hard for other users who never open the editor.
A parameter is a named value that queries can read. You define the path once. Every query that needs it refers to the name.
Create a parameter
- In Excel, choose Data > Get Data > Launch Power Query Editor.
- In the editor, choose Home > Manage Parameters > New Parameter.
- Set the name to
FolderPath. - Set Type to Text.
- Set Current Value to your folder, for example
C:\Data\Sales. - Select OK.
A new query named FolderPath appears in the Queries pane. Its value is a plain text literal with metadata describing it as a parameter.
Use the parameter in the query
Open the query that calls Folder.Files. In the Advanced Editor, replace the quoted path with the parameter name. Do not put quotes around the name, because quotes would turn it back into literal text.
let
Source = Folder.Files(FolderPath),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv" and not Text.StartsWith([Name], "~$")),
Kept = Table.SelectColumns(CsvOnly, {"Name", "Content"})
in
KeptYou can also do this without code. Select the Source step, open the formula bar or the gear icon, and change the path argument. In the formula bar, type FolderPath in place of the quoted string.
Change the value later
To point the query at another folder, you have two options.
- In the Power Query Editor, select the
FolderPathquery and edit its Current Value in the Parameters settings. - Select the
FolderPathquery in the Queries pane and edit the value in the formula bar.
Then refresh. The file list, the combine step and the final table all rebuild from the new folder.
Let users change it from a worksheet
Parameters are edited inside the Power Query Editor. If workbook users should not open the editor, store the path in a worksheet cell instead.
- Put the path in a cell, for example
B1, and format the cell as a table with a header such asPath. - Name the table
Settings. - Create a query from the table: Data > From Table/Range.
- Rename the query
SettingsTable. - Change the folder query to read the path from it.
let
FolderPath = Excel.CurrentWorkbook(){[Name = "Settings"]}[Content]{0}[Path],
Source = Folder.Files(FolderPath),
CsvOnly = Table.SelectRows(Source, each [Extension] = ".csv")
in
CsvOnly{0} takes the first row and [Path] takes the value in the Path column. Users now edit the cell and select Data > Refresh All.
Common errors
| Symptom | Likely cause |
|---|---|
| `The import FolderPath matches no exports` | The name in the formula is misspelled, or the parameter query was deleted. M names are case sensitive. |
| `The folder path is not valid` | The path has a typo, or the folder does not exist for this user. |
| Path still points to the old folder | The query still has the quoted string. Check the Source step. |
| Privacy or credentials prompt appears | Excel treats the new folder as a new data source. Set the privacy level and continue. |
Exercise
A colleague opens your workbook and the folder query fails because the files are on drive D: on their computer. You do not want them to open the Power Query Editor. What is the simplest way to let them fix it?
Show answer
Store the folder path in a named worksheet table, such as Settings, and read it in the folder query with Excel.CurrentWorkbook. They type their own path in the cell and select Refresh All.