Microsoft Learn - MSDN Tutorials

Advanced Query Folding in Power BI

This tutorial delves into the advanced aspects of query folding in Power BI. Understanding and leveraging query folding is crucial for optimizing the performance of your Power BI data transformation processes, especially when dealing with large datasets and complex data sources.

What is Query Folding?

Query folding, also known as query propagation, is the capability of the Power Query engine to translate M language transformations into a single query that is sent to the data source. When query folding occurs, the data source executes the transformations locally, which is significantly more efficient than pulling all data into Power BI and then transforming it.

Why is Query Folding Important?

Understanding the Mechanics

The Power Query engine attempts to fold transformations as far back as possible towards the data source. However, not all transformations are foldable. When a non-foldable step is encountered, the query folding process stops at that point. Subsequent transformations will then be performed by the Power Query engine locally.

Foldable vs. Non-Foldable Transformations

Generally, data source native operations like filtering, sorting, grouping, merging, and simple column manipulations are foldable, especially when interacting with relational databases (e.g., SQL Server, Oracle) or cloud data warehouses.

Transformations that are typically not foldable include:

How to Identify and Verify Query Folding

You can check for query folding directly within the Power Query Editor:

  1. Open your query in the Power Query Editor.
  2. In the Applied Steps pane, right-click on a step.
  3. If the "View Native Query" option is available and clickable, it indicates that query folding is happening up to that step.

Example: Demonstrating Query Folding

Consider a scenario where you have a SQL Server table named SalesData and you want to filter it by a specific year and then select a few columns.

Initial Query (M Code):

let
    Source = Sql.Database("YourServer", "YourDatabase"),
    SalesData = Source{[Schema="dbo",Item="SalesData"]}[Data],
    FilteredRows = Table.SelectRows(SalesData, each [OrderYear] = 2023),
    SelectedColumns = Table.SelectColumns(FilteredRows,{"OrderID", "Product", "Amount"})
in
    SelectedColumns

In this example, both Table.SelectRows (filtering) and Table.SelectColumns (column selection) are typically foldable operations for SQL Server. If you right-click on the SelectedColumns step, you should see "View Native Query" enabled, showing the equivalent SQL statement.

Potential Native Query (SQL):

SELECT [OrderID], [Product], [Amount]
FROM [dbo].[SalesData]
WHERE [OrderYear] = 2023

This SQL query directly reflects the transformations applied in Power BI, demonstrating effective query folding.

Strategies for Maximizing Query Folding

Common Pitfalls and Troubleshooting

Conclusion

Mastering query folding is a key skill for any Power BI developer aiming for efficient and scalable data solutions. By understanding which transformations fold and how to encourage folding, you can significantly improve report performance and reduce processing times.