Sub topic 4
Filtering the Table with the Range Parameters
This chapter shows how to filter a large table with RangeStart and RangeEnd so that Power BI can build date partitions and push the filter to the source. It assumes you can connect to a relational source in Import mode, use the Applied Steps pane in Power Query, and have already created the two parameters from the previous chapter. The fact table needs a date or datetime column.
What the filter does
The filter has two jobs.
- In Power BI Desktop, it limits the rows you load while you develop. Only rows between
RangeStartandRangeEndare imported. - In the service, Power BI replaces the parameter values for each partition. Each partition runs the same query with its own date boundaries.
You write the filter once. The service reuses it for every partition the refresh policy creates.
Filter a datetime column
Open Power Query Editor, select the large table, and apply a custom filter on the date column. In the Advanced Editor, the query looks like this:
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
FilteredUse >= on one boundary and < on the other. If both boundaries were inclusive, a row stamped exactly at a partition boundary would load into two partitions and be counted twice. With one inclusive and one exclusive boundary, every row lands in exactly one partition.
The column type must be Date/Time to match the parameters. If OrderDate is a Date column, change its type to Date/Time before you filter, or the comparison may not fold.
Filter an integer date key
Many warehouses store dates as integer keys in yyyymmdd form, such as 20200115. The parameters are still Date/Time, so convert them to the same integer format and compare against 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
FilteredThe same half-open rule applies. Power Query can usually fold this arithmetic into the SQL WHERE clause.
Keep the filter foldable
The filter helps only if it folds, meaning Power Query translates it into a WHERE clause that the database runs. If it does not fold, Power BI downloads the whole table for each partition and filters it locally. That can make a refresh slower than a full refresh.
Follow these habits:
- Apply the range filter early, directly after the source step.
- Do not put steps before the filter that break folding, such as adding an index column or merging with a non-foldable query.
- Filter on a column that exists in the source, not on one you computed in Power Query.
- Check folding after every change.
To check, right-click the filter step in Applied Steps. If View Native Query is available, the step folds. If it is greyed out, the step does not fold.
Common mistakes
| Mistake | Effect |
|---|---|
| Using `<=` and `>=` together | Rows on a boundary are loaded twice |
| Filtering a Date column without converting it | The filter may not fold |
| Filtering after steps that break folding | Whole table is read for each partition |
| Misspelled parameter names | The policy cannot be configured |
| Filtering a column that changes after load | Rows can move between partitions |
The last row matters for design. Choose a date that does not change after the row is created, such as the order date, not a "last modified" date. Otherwise a row can appear in two partitions, or in none after an update.
Apply the change
After you add the filter, select Close & Apply. Desktop now loads only the slice between your parameter values, so the model stays small while you build reports. The next step is the refresh policy, which tells the service how much history to keep and how much to refresh.
Exercise
A fact table has an integer DateKey column such as 20200115. You filter with [DateKey] >= RangeStart and [DateKey] <= RangeEnd. Name two problems with this filter and describe the fix.
Show answer
First, DateKey is an integer and the parameters are Date/Time, so the comparison is between mismatched types and will not work or fold correctly. Convert RangeStart and RangeEnd to yyyymmdd integers, for example Date.Year(x) 10000 + Date.Month(x) 100 + Date.Day(x). Second, <= on the upper bound makes both boundaries inclusive, so a row on a partition boundary loads into two partitions. Use >= on the start key and < on the end key.
Exercise
You filter on a datetime column, but View Native Query is greyed out on the filter step. What should you check first?
Show answer
Check whether an earlier step breaks query folding, such as an index column or a merge with a non-foldable query. Move the range filter to directly after the source step, or remove the step that breaks folding, then confirm View Native Query is available again.