Power Query query folding pushes data processing back to the source database instead of pulling all data into memory, improving performance and reducing load. It works only when each step in your transformation can be translated into SQL (or the native query language of your source). If folding breaks, Power Query falls back to local processing, which slows down refreshes and uses more system resources. To benefit from folding, you must use supported operations and avoid steps that break translation.
What You'll Need
- A data source that supports query folding (e.g., SQL Server, Azure SQL Database, Oracle, PostgreSQL, Snowflake, or other relational databases)
- Power Query Editor in Excel 2016 or later, Power BI Desktop, or Microsoft Fabric
- Access to the source database with read permissions
- Basic understanding of SQL or the source database’s query language
Step-by-Step
- Open Power Query Editor by selecting Data > Get Data > From Database > From SQL Server (or your specific source), then authenticate and load your table or query.
- Apply transformations such as filtering rows, selecting columns, or renaming fields. Avoid steps like custom column formulas using M functions that can’t be folded, such as Text.Contains with a variable pattern or List.Contains.
- After each transformation, check the Applied Steps pane on the right. Look for the query folding indicator — a small green icon with a database symbol appears next to steps that are folded. If a step lacks it, folding has been broken at that point.
- To verify folding is active, right-click the last step in the Applied Steps list and select View Native Query. If a SQL statement appears, folding is working. If you see "This step cannot be folded" or no query appears, folding is disabled.
- If folding breaks, undo the last step you added and test folding again. Common breaking steps include: using Table.AddColumn with complex M logic, merging queries from different sources, or applying custom functions that reference external data.
- Replace non-folding steps with folding-friendly alternatives. For example, instead of creating a custom column with if-then logic, use Filter Rows to isolate values before selecting columns.
- Use only core transformations: Filter, Sort, Remove Columns, Rename, Change Type, Group By (when aggregating in-source), and basic arithmetic in calculated columns if supported by the source.
- Once all steps are folded, click Close & Load to refresh the data. Monitor performance — if refreshes are fast and resource usage is low, folding succeeded.
- For advanced users, use the Power Query M function Expression.Evaluate to test folding programmatically, but this requires familiarity with M code and is not needed for most users.
Common Mistakes
- Adding a custom column with M functions like Text.Split, DateTime.ToText, or List.Generate — these cannot be translated to SQL and break folding instantly.
- Using parameters in filters that are resolved at runtime, such as referencing a cell in Excel or a variable from another query — these force local evaluation.
- Merging two queries where one is from a non-relational source (e.g., Excel file or web API) — folding stops at the merge point.
- Applying a sort after a filter that should be folded — sorting must happen in the source; if done locally, folding breaks.
- Assuming all database sources fold equally — some older versions of Oracle or PostgreSQL may have limited support for certain functions.
Tips
- Always apply filters early — filtering at the source reduces the volume of data transferred and improves folding success.
- Use the View Native Query option regularly during development to catch folding breaks as soon as they occur.
- Group By operations fold only when aggregations (Sum, Count, Min, Max) are used and the grouping fields are from the original table.
- When using date filters, avoid using M date functions like Date.AddDays — use the source’s native date functions by filtering with a date literal instead.
- Test folding on a small dataset first. If folding works on 100 rows but fails on 100,000, the issue may be timeout or complexity, not the step itself.
- Keep transformation steps minimal. More steps don’t mean better results — fewer, well-chosen steps increase folding chances.
Troubleshooting
- If View Native Query shows nothing, check that your source supports folding. Some cloud services or older SQL Server versions may have restrictions.
- If a step suddenly stops folding after a refresh, check for changes in the source schema, such as renamed columns or altered data types.
- Restart Power Query Editor or Power BI Desktop — sometimes the folding state becomes inconsistent and a restart resets the cache.
- Ensure you are not using any hidden steps. Right-click the query name and select Properties to review query settings. Disable "Enable load" on intermediate queries if they’re not needed.
- If you’re using a view or stored procedure as a source, check that it doesn’t contain non-foldable logic itself — folding only works if the entire chain is foldable.
- Rebuild the query from scratch if folding breaks persistently. Copying steps from a broken query often carries hidden non-foldable elements.
- Check your database’s collation or character encoding. Mismatches between Power Query and the database can cause folding to fail silently.
Frequently Asked Questions
Does query folding work with Excel files?
No. Excel files are processed locally in Power Query and do not support query folding. Folding only works with database sources that accept SQL.
Why does View Native Query show an empty box even after simple steps?
This usually means the source connection isn’t properly configured for SQL translation. Verify you selected a relational database source, not a file or web source. Also, check that you’re not using a query that’s already been expanded or merged with a non-foldable source.
Can I use parameters and still get query folding?
Yes, but only if the parameter is used in a filter that translates directly to SQL, like a date range or ID list. Parameters used in custom logic (e.g., to decide which column to select) break folding.
Does query folding improve data refresh speed?
Yes, significantly. Folding reduces data transfer volume and shifts processing to the database, which is optimized for large-scale filtering and aggregation. Local processing is slower and uses more RAM.
What happens if I turn off query folding?
You can’t manually turn it off — it’s automatic. But if you add a non-foldable step, Power Query automatically switches to local processing. The result is slower refreshes and higher memory usage.
Can I force query folding with M code?
No. You cannot force folding through M code. Folding is determined automatically based on whether each step can be translated into native SQL. Writing optimized M code helps avoid breaking folding, but doesn’t enable it where it’s impossible.