Query Folding in Power BI: How to Optimize Your Data
In this article
Query folding is the process where Power Query transformations are converted into a single query statement and pushed back to the source database for execution. Instead of downloading millions of rows of raw data into your local machine's memory and then filtering them, query folding tells the server (such as SQL Server) to do the filtering, joining, and aggregating first, sending only the final, refined result set to Power BI. This significantly reduces data transfer times, lowers the load on your local hardware, and is a prerequisite for using Incremental Refresh.
What You'll Need
- Power BI Desktop installed on your machine.
- A foldable data source, typically a relational database like SQL Server, Azure SQL, Oracle, or Teradata. (Note: Flat files like CSVs or Excel spreadsheets do not support query folding because they lack a query engine to process the logic).
- Appropriate read permissions for the source database to execute queries.
Step-by-Step
- Connect to your Data Source: Open Power BI Desktop and select Get Data. Choose a relational database (e.g., SQL Server). Enter your server and database credentials. When prompted, select the tables you need.
- Enter the Power Query Editor: Once the Navigator window appears, do not click Load. Instead, click Transform Data. This opens the Power Query Editor window where you can build your transformation steps.
- Apply Foldable Transformations: Begin applying your data cleaning steps. To maintain query folding, prioritize operations that the source database understands. Common foldable operations include:
- Filtering rows (e.g., Date is after 2023-01-01).
- Removing columns.
- Renaming columns.
- Simple joins (Merge Queries) between tables from the same data source.
- Grouping and aggregating (e.g., Sum of Sales by Region).
- Verify Folding Status: In the Applied Steps pane on the right side of the editor, right-click on the last step you have created. If the option "View Native Query" is clickable (enabled), query folding is occurring. The step is being translated into the source's native language (like T-SQL). If "View Native Query" is greyed out, folding has stopped at that step or a previous one.
- Analyze the Native Query: Click "View Native Query" to see the actual code being sent to the server. This is a great way to ensure the server is handling the heavy lifting and to check for any inefficient logic being generated.
- Reorder Steps to Preserve Folding: If you notice that folding stopped at a specific step, look at the steps preceding it. Move non-foldable operations (like complex custom functions or certain data type changes) to the very end of your Applied Steps list. This ensures that as much data as possible is filtered out by the server before Power BI takes over the processing.
- Apply and Close: Once you have verified that the maximum number of steps are folding, click Close & Apply in the top left corner to load the optimized data into your Power BI model.
Common Mistakes
One of the most frequent mistakes is placing a "non-foldable" step early in the sequence. For example, if you use a custom Power Query function or a complex text manipulation that the SQL server cannot translate, Power BI will stop folding at that exact point. Every step after that non-foldable operation will be performed locally on your computer, meaning Power BI must download the entire remaining dataset from the server to process it. This often leads to "Out of Memory" errors or extremely slow refresh times.
Another common error is merging tables from two different data sources. While merging two tables from the same SQL database usually folds, merging a SQL table with an Excel file will immediately break query folding for that operation and all subsequent steps. This is because there is no single engine that can query both a database and a flat file simultaneously.
Users also frequently mistake "View Native Query" being greyed out as a total failure of folding. In some cases, Power BI can still fold certain operations even if the native query isn't displayed, or it may be using a different mechanism. However, as a general rule of thumb, if it is greyed out, you should assume folding has stopped and investigate the step sequence.
Tips
To maximize performance, always apply your filters first. The "Filter Rows" step is the most powerful tool for query folding because it reduces the volume of data moving across the network. If you only need data from the last two years, filter the date column immediately after connecting to the source.
When performing joins, ensure that the tables are coming from the same server and database. If you are joining tables across different databases on the same server, folding may still work, but it depends on the permissions and the specific database engine. Whenever possible, create a View on the SQL server side. By connecting Power BI to a pre-defined SQL View, you shift the complexity of the logic to the database administrator and ensure that the initial data pull is as efficient as possible.
Be cautious with data type conversions. While simple changes (like changing a decimal to a whole number) usually fold, complex conversions or using "Change Type with Locale" can sometimes break the fold. Always check the native query after changing data types to be sure.
Troubleshooting
If you find that your query is not folding and your refreshes are taking hours, follow these steps in order:
First, identify the "Breaking Point." Go through your Applied Steps one by one, right-clicking each step and checking if "View Native Query" is enabled. The first step where the option becomes greyed out is the culprit. Examine that step to see if there is a foldable alternative. For example, instead of using a complex custom column with a conditional statement, see if you can use the built-in "Conditional Column" feature, which is more likely to fold.
Second, check your data source. If you are using an ODBC connection or a generic connector, query folding capabilities are often limited compared to the native SQL Server connector. If possible, switch to the official native connector for your specific database.
Third, simplify your logic. If you have a very complex set of transformations that cannot be folded, consider moving that logic upstream. Ask your database administrator to create a Table or a View that performs the necessary transformations. This removes the burden from Power BI entirely and guarantees that the data arrives in the model already cleaned and aggregated.
Finally, check for "Buffer" functions. Using functions like Table.Buffer or List.Buffer explicitly tells Power Query to load the data into memory and stop folding. Only use these if you have a specific performance reason to do so, and be aware that they will kill query folding for all subsequent steps.
Frequently Asked Questions
Does query folding work with Excel or CSV files?
No. Query folding requires a data source with a query engine (like a SQL database) that can interpret and execute the instructions sent by Power BI. Flat files are just raw data; they cannot "process" a query, so Power BI must load the file and perform all transformations locally.
Why is "View Native Query" greyed out even for simple steps?
This usually happens because a previous step in the sequence broke the fold. Once folding is broken, it cannot be restarted for any subsequent steps. Check the steps above the current one to find where the folding stopped.
Does query folding affect the Power BI service?
Yes, it is critical for the Power BI service. When you publish a report, the service uses the same logic. If folding is happening, the service sends the query to the database. If not, the On-premises Data Gateway must download the full dataset and process it in its own memory, which can lead to gateway timeouts and failed refreshes.
Can I use a custom SQL query to improve folding?
Actually, using a custom SQL statement (the "SQL Statement" box during connection) often disables query folding for any steps you add later in the Power Query Editor. It is generally better to connect to the table directly and use the Power Query UI, or connect to a View created on the server.
Is query folding required for Incremental Refresh?
Yes. Incremental Refresh relies on the ability to filter data by date on the server side. If the date filter does not fold, Power BI would have to download the entire table to determine which rows are new, which defeats the entire purpose of incremental loading.