Forum Discussion
Query Folding Details for Power BI
- Anonymous1 year ago
Hi Sudip_J ,
Thanks for reaching out to the Microsoft fabric community forum.Here are the links to assist you in understanding and implementing query folding in Power BI when connecting to SQL Server and MySQL data sources:
1. Query Folding Basics
-
https://learn.microsoft.com/en-us/power-query/query-folding-basics
-
Provides an overview of query folding, its benefits, and how Power Query translates steps into native queries.
2. Power Query M Language Specification
-
https://learn.microsoft.com/en-us/powerquery-m/power-query-m-language-specification
-
Offers detailed information on the M language used in Power Query for data transformations.
3. Connect to MySQL Database from Power Query Desktop
-
https://learn.microsoft.com/en-us/power-query/connectors/mysql-database
-
Step-by-step guide on connecting Power BI to a MySQL database using Power Query.(learn.microsoft.com)
4. Connect to SQL Server Database from Power Query Desktop
-
https://learn.microsoft.com/en-us/power-query/connectors/sql-server
-
Instructions on establishing a connection between Power BI and SQL Server databases.
5. Power Query Documentation Hub
-
Central hub for all Power Query documentation, including connectors, transformations, and performance optimization.
If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it.
Best Regards,
Sreeteja.
Community Support Team -
Hi Sudip_J
Query folding is a critical optimization technique in Power BI that pushes data transformation logic back to the source database rather than processing it locally. Here's a detailed explanation of your queries:
(i) Connecting Power Query to SQL Server/MySQL and Transformation Process
Connection Setup:
In Power BI Desktop, select "Get Data" ā "SQL Server" or "MySQL"
Enter server name, database, and credentials
Power Query establishes a connection using appropriate drivers
Initial Query:
When you select a table, Power Query generates a basic M query like:
= Sql.Database("server", "database", [Query="SELECT * FROM Sales"])Or for MySQL:
= MySQL.Database("server", "database", [Query="SELECT * FROM Sales"])
(ii) Transformation Process at Native Data Source
Query folding occurs when Power BI can translate your Power Query (M) transformations into native SQL queries:
Supported Operations that typically fold:
Filtering rows (Table.SelectRows)
Selecting columns (Table.SelectColumns)
Sorting (Table.Sort)
Grouping (Table.Group)
Joins (Table.Join)
Basic calculations (simple arithmetic in Table.AddColumn)
Example:
= Table.SelectRows( Table.SelectColumns(Sales, {"OrderID", "Amount"}), each [Amount] > 1000 )Folds to SQL:
SELECT OrderID, Amount FROM Sales WHERE Amount > 1000
(iii) How Transformations Push Back to Native Source
Verification Process:
Right-click a step in Power Query Editor ā Check "View Native Query"
If enabled, the step is folded; if grayed out, folding was broken
Common Reasons Folding Breaks:
Using non-foldable functions (e.g., Text.Combine)
Complex custom columns
Certain data type conversions
Merges/joins with incompatible data types
Using Table.Buffer or other caching functions
Optimization Tips:
Apply filters early in the query
Perform column selection before complex operations
Use SQL views or stored procedures for complex logic
(iv) Optimizing Dashboard Performance
Query folding improves performance by:
Reducing Data Transfer:
Only the final result set is transferred to Power BI
Avoids bringing entire tables for local processing
Leveraging Source Engine:
Utilizes SQL Server's optimized query processor
Benefits from database indexes and statistics
Monitoring Techniques:
Use Performance Analyzer in Power BI Desktop
Check SQL Server Profiler to see actual queries executed
Review query execution plans in SQL Server
Advanced Techniques:
Use "Import" mode with query folding for scheduled refreshes
Consider DirectQuery for very large datasets (keeps all processing on server)
Implement incremental refresh with folded filters
To maximize query folding benefits, structure your transformations to keep as much logic as possible in foldable operations before applying any non-foldable steps.
If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" ā Iād truly appreciate it! Thank you.