Power Query SQL Query Folding
Query Folding is the built-in capability of the Power Query (Mashup) engine to convert graphical transformation steps authored in M into a single, native SQL SELECT statement and push that computation directly to the remote database engine (Source-Side Pushdown).
1. Why Query Folding is Critical
Without query folding, Excel must pull entire unfiltered tables across the network into local RAM before applying filters or grouping.
TEXT
WITH QUERY FOLDING (OPTIMAL):
Power Query: Filter Country = 'USA' βββΊ Generated SQL: WHERE Country = 'USA' βββΊ SQL Server executes filter
Network transfer: 1,000 rows (only USA data)
WITHOUT QUERY FOLDING (PERFORMANCE DISASTER):
Power Query fails to fold βββΊ Generated SQL: SELECT * FROM LargeTable
Network transfer: 10,000,000 rows transferred across network!
Local Machine: Power Query strains CPU/RAM filtering rows locally in Excel!
2. Transformations That Typically Fold
When connected to Microsoft SQL Server, Power Query folds:
- Filtering:
Table.SelectRows$\to$ SQLWHEREclause. - Column Selection:
Table.SelectColumns$\to$ SQLSELECT col1, col2. - Sorting:
Table.Sort$\to$ SQLORDER BY. - Grouping & Aggregation:
Table.Group$\to$ SQLGROUP BY,SUM(),COUNT(). - Merging / Joins:
Table.NestedJoinon database tables $\to$ SQLINNER JOIN/LEFT OUTER JOIN. - Appends:
Table.Combine$\to$ SQLUNION ALL. - Adding Custom Columns (Simple Math): Basic arithmetic and standard string logic $\to$ SQL calculated expressions.
3. Operations That Break Query Folding
Any operation that lacks a direct 1-to-1 equivalent in T-SQL stops query folding permanently. All subsequent steps will execute locally:
- Custom M Functions: Invoking external M functions or non-standard library calls.
- Buffer Operations:
Table.Bufferexplicitly forces local caching. - Advanced Text Splitting: Regex or delimiter splitting by complex multi-character rules.
- Changing Types to Unmapped Types: Complex locale-dependent transformations.
- Non-Folding Preceding Step: Once a single step breaks folding, no subsequent downstream step can fold!
4. How to Inspect Query Folding in Excel
In Power Query Editor:
- Right-click any applied step in the Applied Steps pane.
- If View Native Query is enabled (clickable), the step is folding. Clicking it opens the exact T-SQL script sent to SQL Server.
- If View Native Query is disabled (grayed out), folding stopped at or before this step.