Sub topic 11
Replacing hard-coded paths with parameters
This chapter assumes you can open Power Query Editor, read the Applied Steps pane, and have already connected to an Excel workbook or a CSV file. You should know that each query is a let ... in expression. Here you replace file paths and site URLs written inside queries with parameters, so you can change a location in one place.
Why hard-coded paths cause trouble
When you connect to a file, Power Query writes the location into the first step as a text literal:
Source = Csv.Document(File.Contents("C:\Data\orders.csv"), [Delimiter = ",", Encoding = 65001])That literal causes problems in common situations:
- A colleague opens your .pbix file and stores the data in
D:\Reports\Data. The query fails with a "could not find a part of the path" error. - You move from a test folder to a production folder. You must edit every query that uses the old path.
- A SharePoint site URL changes, or you want to point the same report at a different site.
- Several queries read from the same folder. Each one holds its own copy of the path, and one copy is easy to miss.
A parameter is a named value stored in the report that queries can read. You change the value once and every query that uses it picks up the change on the next refresh.
What a parameter is in Power Query
A parameter is a special query that returns a single value instead of a table. It has these properties:
| Property | Meaning |
|---|---|
| Name | The identifier you use in other queries, such as `FolderPath`. Names are case-sensitive. |
| Description | Optional text that explains what to enter. |
| Required | If set, the value cannot be empty. |
| Type | The data type, such as Text, Decimal Number, Date, or Any. |
| Suggested Values | Any value, a list of values, or a query that returns a list. |
| Current Value | The value used when queries run. |
For file locations, use the Text type. A text parameter accepts any string, including a path with spaces or a URL.
Creating a parameter
- In Power BI Desktop, select Transform data to open Power Query Editor.
- On the Home tab, select Manage Parameters, then New Parameter.
- Enter the name
FolderPath. - Set Type to Text.
- Set Suggested Values to Any value.
- In Current Value, enter
C:\Data\. Include the final backslash. - Select OK.
The parameter appears in the Queries pane with a distinct icon. Select it to see its current value in the editor, and change it there at any time.
The Advanced Editor shows what Power Query stored:
"C:\Data\" meta [IsParameterQuery = true, Type = "Text", IsParameterQueryRequired = true]The meta record is metadata. It marks the query as a parameter and lets the interface show it in the parameter dialog. The value is an ordinary text literal.
Using the parameter in a query
Open the query in the Advanced Editor and replace the literal with the parameter name. You can also select the step in Applied Steps and edit it in the formula bar. The example below builds the path with the & operator, which joins two pieces of text:
let
Source = Csv.Document(File.Contents(FolderPath & "orders.csv"), [Delimiter = ",", Encoding = 65001]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
PromotedFolderPath holds the folder and "orders.csv" holds the file name. Because the folder value ends with a backslash, the result is C:\Data\orders.csv.
Keeping the trailing separator in the parameter is a convention, and it is fragile. If someone enters C:\Data without the backslash, the result is C:\Dataorders.csv and the query fails. You can make the query tolerate both forms:
let
Folder = if Text.EndsWith(FolderPath, "\") then FolderPath else FolderPath & "\",
Source = Csv.Document(File.Contents(Folder & "orders.csv"), [Delimiter = ",", Encoding = 65001]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
PromotedThe Folder step adds the backslash only when it is missing. Later steps then use Folder, so they work with either input.
Parameterizing a SharePoint site URL
The same approach works for SharePoint Online. Create a text parameter named SiteUrl with the value https://contoso.sharepoint.com/sites/Sales. Then pass it to the connector:
let
Source = SharePoint.Tables(SiteUrl, [ApiVersion = 15]),
Tasks = Source{[Title = "Tasks"]}[Items]
in
TasksUse the site root URL, with no /Shared Documents or page names. The SharePoint connectors accept the site address and find the libraries and lists themselves.
For file queries the same parameter also feeds SharePoint.Files. Here the folder path is built from the parameter:
let
Source = SharePoint.Files(SiteUrl, [ApiVersion = 15]),
FileContent = Source{[Name = "Sales.xlsx", #"Folder Path" = SiteUrl & "/Shared Documents/"]}[Content],
Workbook = Excel.Workbook(FileContent, true, true),
SalesTable = Workbook{[Item = "Sales", Kind = "Table"]}[Data]
in
SalesTableThe Folder Path value must match the stored value exactly, including the trailing slash. If the lookup returns an error such as "The key didn't match any rows in the table", compare the folder path column in the Source step with the string you built.
Where parameters can and cannot go
Parameters work well inside connector arguments. A few details matter:
- Dynamic data sources. If a path is built from parameters in a way the engine can analyze, the Power BI service can still refresh the query. Building a URL from several values passed to
Web.Contentsin a way the engine cannot analyze can produce a "dynamic data source" error. For file and SharePoint connectors, a parameter used as the argument works. - Parameter type. A Text parameter used in a number position fails. Convert it with
Number.FromorText.Fromwhere needed. - Query folding. File sources do not fold, so parameters have no folding impact here.
- Shared parameters. One parameter can feed many queries. If you use the same
FolderPathin five queries, all five change together.
Using a parameter to switch environments
A common pattern is one parameter per environment-specific value. For example:
| Parameter | Test value | Production value |
|---|---|---|
| `FolderPath` | `C:\Data\Test\` | `\\fileserver\reports\` |
| `SiteUrl` | `https://contoso.sharepoint.com/sites/SalesTest` | `https://contoso.sharepoint.com/sites/Sales` |
Switch the Current Value and refresh. Check that the file layout is identical in both places, because the query steps still expect the same columns and types.
Changing parameter values after publishing
Parameters are not limited to Desktop. After you publish the report, you can change values in the Power BI service:
- Open the workspace and find the semantic model.
- Open Settings.
- Expand Parameters.
- Edit the value and select Apply.
The next refresh uses the new value. This lets you repoint a published model without reopening the .pbix file. It only works for parameters the service recognizes, and only when the changed path is reachable by the data source setup for that model. A local C: path still needs a gateway, as covered in the refresh chapter. The service also does not accept a parameter whose type is Any, so use Text for paths.
Flow of a parameterized query
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Expression.Error: The name 'FolderPath' wasn't recognized" | The parameter is misspelled, or it was renamed or deleted. | Match the name exactly, including case. |
| "Could not find file" with a doubled or missing slash | The trailing separator is missing or repeated in the parameter. | Normalize the value with `Text.EndsWith` as shown above. |
| "Credentials are required to connect" after changing a value | The new path is a new data source. | Open **Data source settings** and sign in for the new path. |
| Refresh works in Desktop but fails in the service | The path is local and no gateway is set up. | Use a SharePoint location or add a gateway. |
| Parameter cannot be edited in the service | The parameter type is Any, or the query was not recognized. | Set the type to Text and republish. |
Good habits
- Use clear names such as
FolderPath,SiteUrl, andFileName. - Fill in the description so a colleague knows the expected format.
- Keep one parameter per concept instead of one long path.
- Avoid putting secrets, such as passwords, in a parameter. Parameter values are saved in the report and are visible to anyone who can edit it.
- Test by changing the value, refreshing, and confirming that row counts match expectations.
Exercise
Your query reads File.Contents(FolderPath & "orders.csv"). A teammate sets FolderPath to C:\Data (no trailing backslash) and the refresh fails. What is the cause, and what is one way to fix the query so it works with either input?
Show answer
The path becomes C:\Dataorders.csv, which does not exist. Add a step that appends a backslash only when it is missing, for example Folder = if Text.EndsWith(FolderPath, "\") then FolderPath else FolderPath & "\", and use Folder & "orders.csv" in File.Contents.
Exercise
You change the SiteUrl parameter to point at a different SharePoint site. The queries load, but Desktop asks you to sign in again. Why, and where do you manage the saved sign-ins?
Show answer
Credentials are stored per data source, and a different site URL is a different data source. Manage them in File > Options and settings > Data source settings, where you can edit permissions for the new site.