Troubleshooting “Query Folding” breaks: Why your SQL is running slow in Excel.

A “Query Folding” break occurs when Power Query encounters a transformation step in your data pipeline that it cannot translate into a native SQL statement. When folding is active, the remote database server performs all filtering, joining, and sorting before transmitting the final, lightweight result to your workstation. When folding breaks, Power Query must download the entire, uncompressed database table across the local network into your computer’s RAM to process all remaining transformations locally, causing Excel to run slowly, freeze, or time out.

Quick Answer

To fix a broken query folding pipeline in Excel, inspect your Applied Steps in Power Query by right-clicking each step and verifying that View Native Query is active. Reorder your steps so that all row filters, column removals, and aggregations run at the very beginning of the query, pushing any non-foldable operations (such as custom index columns or local text parsing) to the very end.


How Query Folding Works

When you connect Excel to Microsoft SQL Server, Oracle, or PostgreSQL, Power Query acts as a query compiler. As you click buttons in the user interface—such as filtering out inactive records or selecting specific columns—Power Query writes an internal representation in M-code.

The engine then analyzes this pipeline and translates it into a single SQL statement:

-- What Power Query generates behind the scenes when folding works:
SELECT [CustomerID], [OrderDate], [TotalDue]
FROM [Sales].[Orders]
WHERE [OrderDate] >= '2025-01-01' AND [Status] = 1;

Because the database engine executes the query:

  • The server utilizes indexed columns.
  • Only the exact requested rows and columns cross the corporate network.
  • Local CPU and memory consumption in Excel remain negligible.

When a step breaks folding, the translation stops completely at that step. Everything downstream must be computed by the local Power Query Mashup Engine on your desktop.

For an overview of how failed folding leads to local memory crashes, see Troubleshooting “Out of Memory” during the SQL “Fold-to-Data-Source” phase..


Common Causes of Folding Breaks

Not all transformation operations have a direct SQL equivalent. The following actions commonly break query folding:

  • Adding an Index Column: SQL tables are unordered sets by definition. Adding a sequential 1, 2, 3 row index forces Power Query to download all rows to determine local ordering.
  • Custom Text Transformations: Functions like Text.Middle or complex regular expression parsing often fail to translate to standard T-SQL expressions.
  • Cross-Source Merges: Merging a remote SQL table with a local Excel table or CSV file breaks folding immediately.
  • Incompatible Data Type Changes: Changing date formats using non-standard regional culture parameters forces client-side conversion.
  • Using Non-Foldable M Functions: Incorporating Table.Buffer isolates the table in local memory and blocks all downstream steps from generating SQL.

Step-by-Step Diagnostic and Fix Procedure

Step 1: Identify the Exact Step Where Folding Breaks

  1. Open your workbook and launch the Power Query Editor.
  2. Go to the Applied Steps pane on the right.
  3. Start at the top step (Source) and right-click it.
  4. Observe the View Native Query menu option:
    • If View Native Query is clickable (black text), the step is successfully folding into SQL.
    • Click it to view the exact T-SQL query that Power Query will send to the server.
  5. Move downward through your applied steps one by one, right-clicking each step.
  6. The first step where View Native Query appears greyed out marks the exact point where folding broke.

Step 2: Reorder Transformation Steps

To minimize the data transferred across the network, execute all server-compatible operations before any non-foldable steps.

  1. Locate any row filtering (Table.SelectRows) or column removal (Table.SelectColumns) steps situated below the breaking step.
  2. Drag and drop those steps upward in the Applied Steps list, positioning them directly beneath the initial Navigation step.
  3. Check View Native Query on the filtered step. It will now fold to the server.
  4. Leave complex calculations, index column additions, and local formatting operations at the bottom of the list.

Step 3: Replace Client-Side Steps with Foldable Alternatives

Often, a slight change in how you apply a transformation allows the engine to maintain folding:

  • Instead of extracting text characters using custom string logic, use the native Split Column by Delimiter function, which translates cleanly to SQL string methods.
  • Instead of calculating dates locally, use standard date arithmetic supported by the database engine (such as year or month extraction).
  • If you must combine SQL data with a static list from an Excel tab, upload the static list into a database staging table rather than merging it inside Power Query.

For complex multi-table joins that continue to strain system memory, see Handling “Evaluation ran out of memory” when joining massive SQL tables..


Diagnostic Next Steps: When Folding Cannot Be Restored

If your reporting requirements demand operations that cannot be folded within the user interface:

  • Create a Database View: Ask your database administrator to create a dedicated view on the SQL server that handles the non-foldable calculation. You can then connect Excel directly to the view.
  • Write a Native SQL Statement: Define your own SQL query inside the connection options so the server handles all filtering before Power Query receives the stream. If this causes execution delays over high-latency networks, refer to Solving “Query Timeout” by adjusting the CommandTimeout parameter in M-code..
  • Manage Network Latency Over VPNs: If queries remain slow despite maintaining proper folding, network packet fragmentation on remote connections may be throttling your transfer speeds. See Troubleshooting VPN-related connection drops during Power Query refreshes..