Power BI relative time filter scripts
- Last UpdatedJul 18, 2024
- 3 minute read
Relative time filter methods can be used in layout scripting to select data to appear in a report based upon a specified time period. Relative time filters can be set at the Power BI report and page level.
SetRelativeTimeReportFilter()
SetRelativeTimeReportFilter(string tableName, string columnName, string filterOperator, string timeUnitsCount, string timeUnitsType, string displayName, bool isHiddenInViewMode, bool isLockedInViewMode, bool update = false
SetRelativeTimeReportFilter() Parameters
Relative time filters are similar to relative date filters and use similar parameters.
-
tableName
Name of the Power BI report table containing the data to be filtered in the report.
-
columnName
Name of the column within the Power BI report table containing the data to be filtered in the report.
-
filterOperator
The operator for relative date and time filters can be one of the following:
-
0 = InLast
-
1 = InThis
-
2 = InNext
The single digit value of filterOperator specifies the type of date and time operator.
-
-
timeUnitsCount
The number of time units.
-
timeUnitsType
The unit of time the filter is using, as shown below:
-
0 = Days
-
1 = Weeks
-
2 = CalendarWeeks
-
3 = Months
-
4 = CalendarMonths
-
5 = Years
-
6 = CalendarYears
-
7 = Minutes
-
8 = Hours
-
-
displayName
A filter can be displayed in the filter pane with a personalized name. When the value is undefined or null, the default name of the filter will be displayed (typically the name of the filtered data field).
-
isHiddenInViewMode
Boolean value that determines if a hidden filter is applied to the report but not displayed in the filter pane in view mode. Set to true to hide the filter.
-
isLockedInViewMode
Boolean value that determines if a filter lock is applied and displayed in the filter pane. The filter value cannot be changed in view mode. Set to true to lock the filter.
-
update
Boolean value that determine whether multiple filters can be set. When update is true, a new filter will be added to the existing filters. If set to false, the existing filters are replaced.
Example
MyContent.PBIViewerCntrl.SetRelativeTimeReportFilter("Sales","OrderDate", "0","12","8" ,"MyFilter5", true, false);
This filter statement selects order data from the Sales table that has been collected in the last 12 hours. The filter has been assigned the name MyFilter5 and is hidden on the Power BI report Filter page.
SetRelativeTimePageFilter()
The basic relative time filter can also be applied to a Power BI report page. The page name must be passed in as the first parameter.
SetRelativeTimePageFilter(string pageName, string tableName, string columnName, string filterOperator, string timeUnitsCount, string timeUnitsType, string displayName, bool isHiddenInViewMode, bool isLockedInViewMode, bool update = false)
SetRelativeTimePageFilter() Paramers
The RelativeTimePageFilter method includes the same parameters as the SetRelativeTimeFilter() method and also includes the pageName parameter.
-
pageName
Name of the Power BI report page that displays the data to be filtered in the report.
-
tableName
Name of the Power BI report table containing the data to be filtered in the report.
-
columnName
Name of the column within the Power BI report table containing the data to be filtered in the report.
-
filterOperator
The operator for relative date and time filters can be one of the following:
-
0 = InLast
-
1 = InThis
-
2 = InNext
The single digit value of filterOperator specifies the type of date and time operator.
-
-
timeUnitsCount
The number of time units.
-
timeUnitsType
The unit of time the filter is using, as shown below:
-
0 = Days
-
1 = Weeks
-
2 = CalendarWeeks
-
3 = Months
-
4 = CalendarMonths
-
5 = Years
-
6 = CalendarYears
-
7 = Minutes
-
8 = Hours
-
-
displayName
A filter can be displayed in the filter pane with a personalized name. When the value is undefined or null, the default name of the filter will be displayed (typically the name of the filtered data field).
-
isHiddenInViewMode
Boolean value that determines if a hidden filter is applied to the report but not displayed in the filter pane in view mode. Set to true to hide the filter.
-
isLockedInViewMode
Boolean value that determines if a filter lock is applied and displayed in the filter pane. The filter value cannot be changed in view mode. Set to true to lock the filter.
-
update
Boolean value that determine whether multiple filters can be set. When update is true, a new filter will be added to the existing filters. If set to false, the existing filters are replaced.Example
Example
MyContent.PBIViewerCntrl.SetRelativeTimePageFilter("ReportSection1","Sales","OrderDate", "0","12","8" ,"MyFilter6", true, false, false);
This filter statement selects data from the Sales table that has been collected in the last 12 hours and is displayed on the ReportSection1 report page. The filter has been assigned the name MyFilter6 and is hidden on the Power BI report Filter page.