Sub topic 3
Creating the RangeStart and RangeEnd parameters
This chapter assumes you can connect to a relational source in Power BI Desktop and load a table in Import mode. It also assumes you can work in the Power Query Editor and read the Applied Steps pane. You should know that Import mode copies data into the model, while DirectQuery sends queries to the source at report time.
What the parameters do
Incremental refresh splits a table into date-based partitions. Power BI needs a way to ask the source for one slice of data at a time. It does this through two reserved Power Query parameters, RangeStart and RangeEnd.
The two parameters play two roles at different times:
- In Power BI Desktop, they hold fixed values. Those values decide which small slice of data you load while you build the report.
- In the Power BI service, the service ignores those fixed values. For each partition it refreshes, it substitutes that partition's own start and end dates, then runs your query once per partition.
Because of the second role, the parameters are a placeholder for "the date window of this partition". The next chapter shows how to use them in a filter. In this chapter you only create them.
The naming and type rules
The rules are strict. If you break one, the incremental refresh option stays unavailable or the policy cannot be applied.
| Requirement | Value |
|---|---|
| Parameter names | Exactly `RangeStart` and `RangeEnd` |
| Case | Case-sensitive, so `rangestart` or `Range_Start` does not work |
| Type | Date/Time (`datetime`), not Date, Text, or Decimal Number |
| Required | Yes, each parameter must be marked as required |
| Current value | Must not be empty |
Two points deserve emphasis.
First, the type is Date/Time even if your source column is a plain date. The parameters always carry a time part. You adapt the source column to them in the filter step, not by changing the parameter type.
Second, the names are reserved. The service looks for those exact names. A parameter called StartDate is an ordinary parameter and triggers nothing.
Creating the parameters in the Power Query Editor
Follow these steps in Power BI Desktop.
- On the Home tab, select Transform data to open the Power Query Editor.
- On the Home tab of the editor, select Manage Parameters, then New Parameter.
- Set Name to
RangeStart. - Set Type to Date/Time.
- Leave Suggested Values as Any value.
- Enter a Current Value, for example
1/1/2020 12:00:00 AM. The exact display format follows your regional settings. - Keep Required checked, then select OK.
- Repeat the steps for
RangeEnd, with a current value such as2/1/2020 12:00:00 AM.
You can also create them without the dialog. In the editor, choose New Source, then Blank Query, open the Advanced Editor, and paste the M code. Do this once per parameter. The result is the same.
RangeStart = #datetime(2020, 1, 1, 0, 0, 0) meta [IsParameterQuery = true, Type = "DateTime", IsParameterQueryRequired = true]
RangeEnd = #datetime(2020, 2, 1, 0, 0, 0) meta [IsParameterQuery = true, Type = "DateTime", IsParameterQueryRequired = true]The meta record is what turns a plain value into a parameter. IsParameterQuery = true marks it as a parameter, Type = "DateTime" sets the type, and IsParameterQueryRequired = true makes it required. If you use the dialog, Power BI writes this record for you. Check it in the Advanced Editor if you are unsure whether a parameter was created correctly.
A parameter shows up in the Queries pane with its own icon, and the current value appears in the preview. Selecting it lets you edit the value at any time.
Choosing development values
The values you pick control how much data Desktop loads each time it refreshes during development. They have no effect on what the service loads, but they affect your day-to-day speed.
A good starting point is a window that holds enough rows to test your report logic and small enough to refresh in seconds. One to a few months of data is typical for a table with years of history.
Consider these points when you choose the values:
- Use a range that contains real data. If the window sits before your first order date, the table loads empty. Your visuals and measures will look broken, and you may think the filter is wrong.
- Keep
RangeStartearlier thanRangeEnd. If you swap them, the filter returns no rows. - Prefer round boundaries. Midnight on the first of a month is easy to reason about and easy to compare with source data.
- Do not treat the window as a data limit. After you publish, the service loads the full history defined by the refresh policy. The development window does not cap it.
You can change the values later. When you do, the next Desktop refresh loads the new slice. This is handy when you need to reproduce a bug that only shows up in a certain month.
Where the parameters sit in the query
The parameters are not used until you reference them in a filter. A table with the parameters defined but no filter behaves like any other Import table and loads everything. In that state the Incremental refresh dialog can still be opened, but the policy will not partition the data in a useful way, because every partition would return all rows.
Think of the setup as three linked pieces:
- The parameters define the window.
- A filter step on the large table uses the window.
- The refresh policy tells the service how many windows to create and which to refresh.
This chapter covers piece one. The filter is the next chapter, and the policy comes after it.
Boundary conventions you should plan for
The parameters are datetime values, and partitions meet end to start. One partition's RangeEnd is the next partition's RangeStart. If your filter used >= and <= on both ends, a row timestamped exactly on a boundary would load into two partitions. The row would then be counted twice.
The usual convention is to keep one boundary inclusive and the other exclusive:
RangeStartinclusive, so>= RangeStart.RangeEndexclusive, so< RangeEnd.
You do not enforce this in the parameter. You enforce it in the filter. It is worth deciding now, because it affects how you pick the development values. If you want to test that a row dated 2020-02-01 00:00:00 appears in the February window and not the January one, set RangeEnd to exactly that moment and check that the row is absent.
Common mistakes
Most problems at this stage come from a small set of causes.
- Wrong type. If you choose Date instead of Date/Time, the option to define a policy is not offered for that table, or the filter step shows a type mismatch error. Reopen Manage Parameters and change the type.
- Misspelled name.
Rangestartis a different parameter fromRangeStart. Power BI does not warn you. The Incremental refresh dialog just reports that the parameters are missing. - Parameter not required. If Required is cleared, the parameter can hold an empty value. Set it back to required.
- Empty current value. A required parameter with no current value causes the query to fail on load.
- Created in the wrong place. The parameters must exist in the same Power BI semantic model as the table you want to partition. One pair serves every table with a policy in that model.
Because one pair serves the whole model, you do not create separate parameters per table. Each table you configure uses the same two names in its own filter step. Each table can still have a different policy, so one table can keep two years and another ten.
Checking your work
Before moving on, confirm the setup with these checks.
- In the Queries pane, both
RangeStartandRangeEndappear under the parameters group. - In the Advanced Editor for each one, the
metarecord showsIsParameterQuery = true,Type = "DateTime", andIsParameterQueryRequired = true. - Each has a non-empty current value, and
RangeStartis earlier thanRangeEnd. - The names match the table above character for character.
Once these hold, you are ready to apply the parameters as a filter on the large table.
Exercise
A colleague creates two parameters named StartDate and EndDate, both of type Date, then reports that the Incremental refresh option cannot be configured for the table. Name the problems and describe how to fix them.
Show answer
There are two problems. The names are wrong, because the service looks only for RangeStart and RangeEnd, spelled and capitalized exactly that way. The type is also wrong, because it must be Date/Time. To fix it, create or rename the parameters as RangeStart and RangeEnd, set the type to Date/Time, keep Required checked, and give each a non-empty current value with RangeStart earlier than RangeEnd.
Exercise
You set RangeStart to 2025-01-01 and RangeEnd to 2025-02-01 in Desktop, but the table preview is empty even though the source has millions of rows. The filter step is correct. What is the most likely cause, and does it mean the published model will also be empty?
Show answer
The source most likely has no rows dated within that window, for example because its data ends in 2024. Change the development values to a window that contains data. The published model will not be empty for this reason, because the service replaces the Desktop values with the partition ranges from the refresh policy.