Sub topic 18
Understanding query folding and performance
This chapter assumes you can build queries in the Power Query Editor, read Applied Steps, and make small edits in the formula bar. It covers query folding, then parameters.
What query folding is
Query folding means Power Query translates your steps into a single native query, such as SQL, and the source runs it. The source filters, groups, and joins the data. Only the result travels to Excel.
Without folding, Power Query downloads all the rows and applies each step on your computer. This is slow for large tables.
Which sources fold
Relational databases such as SQL Server usually fold. Excel tables, CSV files, and folders do not, because those files have no query engine. For these sources, folding is not a concern, but you can still reduce work by removing columns and rows early.
Checking and protecting folding
In a database query, right-click a step in Applied Steps. If View Native Query is available, that step folds. If it is greyed out, the step does not fold.
Steps that commonly break folding include adding an index column, custom M functions, and some text or date operations. Once one step breaks folding, every later step runs locally.
Put foldable steps first: filter rows, remove columns, group, and merge. Put steps that do not fold last.
let
Source = Sql.Database("myserver", "SalesDb"),
Sales = Source{[Schema="dbo", Item="Sales"]}[Data],
Filtered = Table.SelectRows(Sales, each [Amount] >= 100),
Slim = Table.SelectColumns(Filtered, {"OrderID", "Region", "Amount"})
in
SlimBoth steps here can fold into one SELECT ... WHERE statement.
Parameters
A parameter is a named value that queries can use. You change the value in one place instead of editing M code in several queries.
Create one with Home > Manage Parameters > New Parameter. Give it a name, a type, and a current value. For example, create MinAmount as a Number with the value 100. The editor stores it as a query:
100 meta [IsParameterQuery=true, Type="Number", IsParameterQueryRequired=true]Use it by name in any other query:
let
Source = Excel.CurrentWorkbook(){[Name="SalesClean"]}[Content],
Big = Table.SelectRows(Source, each [Amount] >= MinAmount)
in
BigCommon uses are a folder path, a date cutoff, or a threshold. A parameter holding a folder path lets you point the same queries at a new folder.
Common errors
- Wrong parameter type. Comparing a Date column to a Text parameter gives an error. Match the types.
- Late filtering. A filter placed after a non-folding step runs locally on every row.
- Hard-coded values. Paths and dates typed into steps are easy to forget when the source changes.
Exercise
A SQL query has these Applied Steps in order: Source, Added Index, Filtered Rows (Region = "West"). The filter step's View Native Query is greyed out. What is the likely cause, and how do you fix it?
Show answer
Added Index does not fold, so every later step, including the filter, runs locally. Move Filtered Rows before Added Index so the filter folds, then add the index.