Sub topic 4
Filtering the Table with the Range Parameters
Where this chapter fits
This chapter assumes you have already created the RangeStart and RangeEnd parameters as Date/Time parameters. It also assumes you know what query folding is: Power Query translates your steps into one native query that the source runs. If you are unsure about either, review the previous chapters first.
The parameters do nothing by themselves. Incremental refresh starts to work when a filter step in the table's query uses them. This chapter covers how to write that filter, why the boundary operators matter, and how to confirm that the filter reaches the source.
What the filter does at each stage
The filter is the link between your query and the partitions Power BI builds later. It behaves differently in two places.
- In Power BI Desktop,
RangeStartandRangeEndhold the values you typed. The filter loads only that slice of data, so your model stays small while you develop. - In the Power BI service, the service ignores your Desktop values. For each partition it supplies that partition's own start and end, then runs the query once per partition. The same filter step therefore retrieves a different slice each time.
So you write one filter, and the service reuses it for every partition. If the filter is wrong, every partition is wrong.
Filtering with a datetime column
Assume FactSales has an OrderDate column of type Date/Time. This is the filter:
let
Source = Sql.Database("myserver.database.windows.net", "SalesDb"),
FactSales = Source{[Schema = "dbo", Item = "FactSales"]}[Data],
Filtered = Table.SelectRows(
FactSales,
each [OrderDate] >= RangeStart and [OrderDate] < RangeEnd
)
in
FilteredThe two parameters are used as plain values, not as strings. The filter is also the first step after navigation. Nothing sits between the source table and the filter, which keeps the filter foldable.
Building the filter in the interface
You can write the formula by hand in the Advanced Editor. You can also build it with the column filter:
- Open the filter drop-down on the date column and choose Date/Time Filters, then Custom Filter.
- For the first condition, choose is after or equal to. Change the value selector from a typed value to Parameter, then pick
RangeStart. - For the second condition, choose is before, switch to Parameter, and pick
RangeEnd. - Make sure the two conditions are joined with And.
Afterward, open the Filtered Rows step and read the formula. The interface sometimes generates different operators than you expect, so check what it wrote.
Why one boundary is inclusive and the other is not
Use >= on one boundary and < on the other. This is the most important rule in the chapter.
Partitions are adjacent. The January partition ends where the February partition starts, so the RangeEnd of one partition is the RangeStart of the next. Suppose a row has the exact value 2020-02-01 00:00:00. The table shows where it lands under each choice.
| Filter | January partition (end = Feb 1) | February partition (start = Feb 1) | Result |
|---|---|---|---|
| `>= RangeStart and < RangeEnd` | Excluded | Included | Counted once |
| `>= RangeStart and <= RangeEnd` | Included | Included | Counted twice |
| `> RangeStart and < RangeEnd` | Excluded | Excluded | Lost |
Only the first row is correct. The second silently double-counts every row that sits exactly on a boundary. The third silently drops those rows. Neither mistake produces an error, and totals are simply wrong.
This is why you should avoid the is between option in the custom filter dialog unless you verify the generated formula. Depending on your version, it may produce an inclusive upper bound.
The column you filter on
It must be Date/Time
The filter column and both parameters must be Date/Time. If your source column is a date without a time, convert it to Date/Time in Power Query before the filter. Then check that folding still works (see below). Some sources fold this conversion and some do not.
It should never change after the row is created
Choose a column such as the order date or the event timestamp. Avoid a column such as "last modified". A row whose date changes will move from one partition to another. The old partition still contains the earlier version of the row, and the new partition gets the updated version. You then see the row twice, and the old partition is not reloaded unless it falls inside the refresh window.
If the source has both kinds of column, filter on the stable one. The "last modified" column has its own use, covered in the chapter on detecting data changes.
Rows with no date disappear
A null in OrderDate makes both comparisons fail. Those rows match no partition and never load. Before enabling incremental refresh, check that the column has no nulls, or decide what should happen to those rows.
Tables with an integer date key
Some warehouses store dates as an integer such as 20200131 and have no datetime column on the fact table. You cannot compare an integer to a Date/Time parameter. Instead, convert the parameters to the same integer format and filter on the key:
let
Source = Sql.Database("myserver.database.windows.net", "SalesDb"),
FactSales = Source{[Schema = "dbo", Item = "FactSales"]}[Data],
StartKey = Date.Year(RangeStart) * 10000 + Date.Month(RangeStart) * 100 + Date.Day(RangeStart),
EndKey = Date.Year(RangeEnd) * 10000 + Date.Month(RangeEnd) * 100 + Date.Day(RangeEnd),
Filtered = Table.SelectRows(FactSales, each [DateKey] >= StartKey and [DateKey] < EndKey)
in
FilteredStartKey and EndKey are calculated once from the parameters, so the filter compares a column to two constants. The same >= and < rule applies. The parameters still have to be Date/Time, because the refresh policy dialog and the service work with Date/Time values.
Because the key keeps only the date, a partition boundary at midnight maps cleanly onto it. That works for day, month, quarter, and year partitions.
Confirming the filter folds
An unfolded filter is the most common reason incremental refresh seems to work but saves nothing. Power Query downloads the whole table for every partition, then filters it locally. Each partition refresh costs as much as a full refresh, and there are many of them.
Check with View Native Query
In Power Query, right-click the Filtered Rows step. If View Native Query is available, the step folds. The result looks something like this:
select [_].[OrderDate], [_].[DateKey], [_].[Amount]
from [dbo].[FactSales] as [_]
where [_].[OrderDate] >= convert(datetime2, '2020-01-01 00:00:00')
and [_].[OrderDate] < convert(datetime2, '2020-02-01 00:00:00')The exact text varies by source, but the where clause containing both dates is what you are looking for. If View Native Query is greyed out, the step does not fold.
What breaks folding
Look at the steps before the filter. A step that Power Query cannot translate stops folding for everything after it. Here is an example that looks harmless:
let
Source = Sql.Database("myserver.database.windows.net", "SalesDb"),
FactSales = Source{[Schema = "dbo", Item = "FactSales"]}[Data],
Buffered = Table.Buffer(FactSales),
Filtered = Table.SelectRows(
Buffered,
each [OrderDate] >= RangeStart and [OrderDate] < RangeEnd
)
in
FilteredTable.Buffer loads the entire table into memory, and the filter then runs on the buffered copy. The query returns the right rows, but it reads every row from the source each time. The fix is to remove the step, or to move the filter above it.
Other causes follow the same pattern:
- A step that the connector cannot translate, such as some custom functions or complex column operations placed before the filter.
- A native SQL query you wrote yourself. A filter applied after a hand-written query usually cannot fold. Put the
RangeStartandRangeEndconditions inside the query text instead, or use a view on the server. - A source that does not support folding, such as flat files. Incremental refresh over such sources still reduces what the model rebuilds, but it cannot reduce what is read from the source.
Place the filter early
Put the filter as the first step after you navigate to the table. Steps that do not fold, such as adding a custom column, belong after it. Then the rows are already limited by the time those steps run.
Which tables get the filter
Apply the range filter only to the large fact table that you plan to partition. Small dimension tables stay as full loads, so they need no parameters and no filter.
If another query references the fact table, for example through a merge, make sure the filtered query is the one being used. A reference to the unfiltered source brings the whole table into the model again.
Common mistakes
- Parameters named differently. The names must be exactly
RangeStartandRangeEnd, with the same capitalization. A near-match is treated as an ordinary parameter and the service will not use it. - Parameter type set to Date. Date/Time is required.
- Filter on a different column from the policy. The policy you define later is set on a table, and the filter you write here defines which rows each partition gets. Keep them consistent.
- Checking only the row count. A filter with the wrong operators still returns plausible-looking rows. Read the formula.
- Forgetting that Desktop shows a slice. The one-month sample in Desktop is expected. The full history appears only after publishing and the first refresh in the service.
Summary
- The filter step uses
RangeStartandRangeEndas constants, and the service reruns it once per partition with that partition's boundaries. - Use
>=onRangeStartand<onRangeEnd. Any other combination duplicates or drops rows without an error. - Filter on a stable Date/Time column, or on an integer key converted from the parameters.
- Put the filter first and confirm that View Native Query is available on it. Without folding, each partition reads the whole table.
Exercise
A teammate filters FactSales with [OrderDate] >= RangeStart and [OrderDate] <= RangeEnd. The refresh succeeds, but monthly totals in the report are slightly higher than the totals in the source. What is the likely cause and what is the fix?
Show answer
Adjacent partitions share a boundary value, because the RangeEnd of one partition is the RangeStart of the next. With <=, rows stamped exactly at the boundary (for example 2020-02-01 00:00:00) are loaded into both partitions and counted twice. The fix is to change the upper bound to <, so the filter is >= RangeStart and < RangeEnd.
Exercise
You add the range filter, but right-clicking the Filtered Rows step shows View Native Query greyed out. The previous step is Table.Buffer(FactSales). What is happening, and what should you do?
Show answer
Table.Buffer stops folding, so the filter runs locally on a fully loaded copy of the table. In the service, every partition would read the whole table from the source. Remove the buffering step or move the range filter above it, so the filter directly follows the table navigation step, then confirm that View Native Query is available.