Power Query folding: Where does the work happen?
A shift meeting needs one day’s production runs. The SQL table contains years. The final table looks identical whether the database selects that day or millions of rows cross the network first.
One filter, two places to run it
A Power Query step describes a transformation. With a suitable connector, the engine tries to translate supported steps into operations the source understands. This is query folding. A SQL database might perform the time filter and select four columns itself. Whether it uses an index is a separate decision for its query optimiser.
As an estimate, suppose a table holds ten million runs and 20,000 match the day. At an assumed 100 bytes per row these represent about 1 GB and 2 MB before compression. The 500-to-one ratio illustrates possible transfer volumes. It cannot predict runtime. Measure the elapsed time, the load on the database and the amount actually transferred.
- Source filteringSQL selects the day. Power Query receives the matching rows and columns.
- Local filteringThe source returns a larger set. The mashup engine evaluates the remaining filter.
Use a window that counts midnight once
Create two Date/Time parameters named RangeStart and RangeEnd. Replace the SQL server and database names with your test environment. Here EventAt is a database timestamp interpreted in UTC. Include the lower bound and exclude the upper bound. Adjacent windows then fit together without overlaps or gaps.
let
Database = Sql.Database("sql-host", "Factory"),
Runs = Database{[Schema="dbo", Item="Runs"]}[Data],
Window = Table.SelectRows(Runs, each
[EventAt] >= RangeStart and [EventAt] < RangeEnd),
Selected = Table.SelectColumns(Window,
{"RunID", "EventAt", "Inspected", "Rejected"})
in
Selected| EventAt | Included? | Reason |
|---|---|---|
| 1 June 00:00:00 | Yes | Lower bound included |
| 1 June 23:59:59 | Yes | Before the end |
| 2 June 00:00:00 | No | Start of next window |
Prove what reached the source
At the filter step, inspect the native query if the connector exposes it. Look for the time predicates and the projected columns. A disabled command alone does not establish that everything ran locally. Depending on the connector and interface, use query diagnostics, the query plan or the SQL requests observed at the source. Compare identical parameter values and collect rows read as well as rows returned where available.
Formatting a timestamp as text before filtering is a common trap. It can change sorting, introduce locale dependencies and limit translation. Filter on the original type where possible. If a later custom function runs locally, the earlier filter may still fold. Validate a full refresh as well as the small authoring preview.
Compare query plans as an experiment
Make two versions for the same closed day. In A, filter the timestamp immediately after navigation to the table. In B, move a genuinely required, connector-dependent transformation ahead of that filter. First compare the lists of RunID and EventAt. If they differ, the timings describe different jobs.
The SQL below illustrates the intended shape of the source query. Parameter names, quoting and operation order in the generated SQL can differ. If the predicate reaches the database but the database still reads the entire table, inspect its execution plan with the database team. Folding can save network traffic while leaving an expensive source scan.
SELECT RunID, EventAt, Inspected, Rejected
FROM dbo.Runs
WHERE EventAt >= @RangeStart AND EventAt < @RangeEnd;Try it yourself
An event occurs exactly at midnight on 2 June. Both daily queries have inclusive upper bounds. How would you find and fix the error?
Show the solution
The same RunID appears on both days. Test records at the two boundaries and compare IDs across adjacent results. Use greater-than-or-equal at the start and strictly less-than at the end. Repeat the test using the agreed time zone.