Sub topic 15
Query folding and keeping cleaning fast
This chapter assumes you can build a query in the Power Query Editor, read the Applied Steps pane, and connect to a database such as SQL Server. You should also know that each step is written in M.
What query folding is
Query folding means Power Query translates your steps into one native query, such as SQL, and sends it to the source. The source does the filtering, column removal, and grouping. Power Query then receives only the result.
Without folding, Power Query downloads the data and applies every step on your machine. That is slow for large tables and uses a lot of memory.
Folding applies mainly to relational sources such as SQL Server. CSV and Excel files cannot fold, because a file has no engine that can run a query.
Checking whether a step folds
- Right-click a step in Applied Steps.
- Look for View Native Query.
- If the option is available, the step folds. If it is greyed out, the step runs locally.
Folding stops at the first step that cannot fold. Every step after it also runs locally, even if that later step could have folded on its own.
Steps that commonly break folding
- Text transformations such as
Text.Proper(the editor's capitalize-each-word command). - Adding an index column in many sources.
Table.Buffer, which loads a table into memory.- Merging queries that come from different sources.
- Custom M functions that the source cannot translate.
Support varies by connector. Always confirm with View Native Query instead of assuming.
Ordering steps to keep cleaning fast
Put steps that fold first and steps that do not fold last. Filter rows and remove columns early, so less data moves across the connection.
let
Source = Sql.Database("localhost", "SalesDb"),
Orders = Source{[Schema = "dbo", Item = "Orders"]}[Data],
RecentOrders = Table.SelectRows(Orders, each [OrderDate] >= #date(2026, 1, 1)),
KeptColumns = Table.SelectColumns(RecentOrders, {"OrderID", "Customer", "Amount"}),
CleanedNames = Table.TransformColumns(KeptColumns, {{"Customer", Text.Proper, type text}})
in
CleanedNamesThe filter and column selection fold into one SQL query. Text.Proper runs locally, but it works on the already reduced rows. If you moved it before the filter, the filter would also run locally.
Common mistakes
- Cleaning text before filtering. This breaks folding early and forces a full download.
- Assuming files fold. For CSV and Excel, reduce the data early anyway, but do not expect a native query.
- Leaving
Table.Bufferin a query. It is occasionally useful, but it ends folding at that point. - Never checking. A query can fold today and stop folding after you add one step.
Exercise
Your query has these steps in order: Source, Capitalize Customer (Text.Proper), Filter Rows, Remove Columns. View Native Query is greyed out on Filter Rows. What should you change, and why?
Show answer
Move the filter and column removal before the Text.Proper step. Folding stops at the first step that cannot fold, so the later filter runs locally. With the filter first, it folds into the source query and the capitalization then runs only on the reduced rows.