Stop 05 · M Language & Advanced Power QueryTopics · 6/8

Query Folding

Part of the Power BI Roadmap

In short

A refresh that takes forty minutes can often drop to two, just by keeping Power Query's work inside the database. That trick is called query folding.

Query folding means Power Query translates your steps into a query that runs in the source database, which is much faster. Some steps break folding for everything after them.

The jaadu happily receives one neat plate through a restaurant hatch while next door an exhausted helper drags home huge sacks of raw vegetables.

Think of it like this

Ordering a cooked dish vs carrying home the kitchen

You can ask a restaurant for one plate of paneer tikka, or ask them to send all their raw ingredients so you cook at home. Folding is ordering the dish: the database does the filtering and grouping and sends back only the small result.

Watch it happen

Folded vs not folded

Steps fold

  • Steps become one SQL query
  • Database filters and groups
  • Only needed rows travel
  • Fast refresh, less memory

Folding breaks

  • All rows are downloaded first
  • Power Query does the work
  • Every later step stays local
  • Slow refresh on big tables

Put foldable steps like filters, column removal and grouping first, and push steps that break folding to the end.

The big ideas

What you need to know

Needs a foldable source

SQL Server, PostgreSQL and similar databases fold. Flat files like CSV and Excel have nothing to fold to.

Check it

Right-click a step. If View Native Query is enabled, that step folded. Newer versions also show folding indicators beside steps.

Some steps break it

Adding an index column or using functions the database doesn't understand usually stops folding for that step and all after it.

Order matters

Filter rows and remove columns early, while folding still works. Do complex custom steps last.

See it in code

Walk through the code

powerquery
1let2    Source = Sql.Database("sql-prod.retail.local", "RetailDW"),3    Sales = Source{[Schema = "dbo", Item = "FactSales"]}[Data],4    Recent = Table.SelectRows(Sales, each [OrderDate] >= #date(2025, 4, 1)),5    Slim = Table.SelectColumns(Recent, {"OrderDate", "StoreKey", "ProductKey", "Amount"}),6    ByStore = Table.Group(Slim, {"OrderDate", "StoreKey"}, {{"Revenue", each List.Sum([Amount]), type number}}),7    Indexed = Table.AddIndexColumn(ByStore, "RowNo", 1, 1)8in9    Indexed

1/4Connect to the database and navigate to the FactSales table. Nothing is downloaded yet.

Steps 2 to 6 fold into one SQL query with WHERE and GROUP BY. The index column on line 7 usually does not fold.

Save yourself the pain

Common mistakes

Oops

Adding an index or custom text step first, then filtering millions of rows.

Do this instead

Filter and remove columns first while folding works, then add the non-foldable step on a small result.

Oops

Expecting a CSV source to fold.

Do this instead

Files can't run queries, so there's nothing to fold to. Folding matters for databases and some other connectors.

Your mission

Spot where folding stops

~25 min

Quick check

Your third step breaks folding. What happens to steps four and five?

Go deeper

  1. 1Right-click a step and use View Native Query to check folding.
  2. 2Do filters and column removals early, while folding still works.
  3. 3Learn which steps commonly break folding with your source.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge