Sub topic 14
Organizing Queries: References, Staging Queries, and Parameters
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and have cleaned a table with the built-in transformations. As the number of queries grows, so does the cost of keeping them readable. Three tools help: reference queries, staging queries, and parameters.
Why organize queries
Each query in the Queries pane is a separate M script. If you copy the same cleaning steps into five queries, a change to the source means five edits. Organizing queries means each piece of logic lives in one place and other queries build on it.
Reference versus duplicate
Right-click a query and choose one of two options:
| Option | What it creates | Behavior |
|---|---|---|
| Duplicate | A full copy of the query's steps | Independent. Later edits to the original do not reach the copy. |
| Reference | A new query whose first step points at the original query's output | Dependent. Changes to the original flow into the new query. |
A reference query has a single source step that names the original query:
let
Source = SalesStaging
in
SourceYou then add steps to the reference query. The original query is not changed.
Staging queries
A staging query does the shared work: connect to the source, promote headers, set types, and clean text. Other queries reference it and add their own shaping.
Staging queries are usually not loaded into the model. Right-click the query and clear Enable load. The query name then appears in italics, and it still runs when the queries that reference it refresh. This keeps the model free of intermediate tables.
To keep the Queries pane tidy, select several queries, right-click, and choose Move to group. Name groups such as Staging and Output. Groups affect only the display, not how queries run.
Parameters
A parameter is a named value that queries can use. Use one for anything that changes between environments, such as a file path, a server name, or a cutoff date.
- In the editor, choose Manage Parameters > New Parameter.
- Give it a name, such as
FilePath, and a type, such as Text. - Enter the current value, for example
C:\Data\sales.csv.
Then use the parameter in place of the hard-coded value. The loading example from earlier chapters becomes:
let
Source = Csv.Document(File.Contents(FilePath), [Delimiter = ",", Encoding = 65001]),
PromotedHeaders = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
PromotedHeadersWhen the file moves, change the parameter value once. Every query that uses FilePath follows.
Common errors
- Editing the original breaks a reference. If you rename or delete a column in the staging query, a downstream step that uses that column fails with an error such as "The column 'X' of the table wasn't found." Fix the downstream step.
- Deleting a query that others reference. Power BI warns you about dependencies. Check Query Dependencies on the View tab first.
- Loading staging queries by accident. Duplicate rows in the model often come from loading both the staging query and its output.
- Wrong parameter type. A parameter typed as Text cannot be passed where a number is expected. Match the type to its use.
Exercise
You have a query named OrdersClean. You want a second table that summarizes orders by region, without repeating the cleaning steps and without changing OrdersClean. Should you use Duplicate or Reference, and should OrdersClean be loaded to the model?
Show answer
Use Reference. The new query starts from OrdersClean's output, so cleaning changes flow into it, and OrdersClean stays unchanged. Whether OrdersClean is loaded depends on whether you need it as its own table. If it is only a shared base, clear Enable load so that only the summary reaches the model.