-
Notifications
You must be signed in to change notification settings - Fork 471
Slicers
Using slicer APIs, You can also get and set slicers state. In addition, you can use load config to change slicer state when loading a report
You may want to read Slicer Docs about how to use slicers in Power BI.
Slicer state contains the current filters applied by the slicer. Slicers support the following types of filters:
- Basic filters
- Basic filters with Keys.
- Advanced filters
- Relative date filters.
You can read more about creating filter object in Filters page.
type ISlicerFilter = IBasicFilter | IBasicFilterWithKeys | IAdvancedFilter | IRelativeDateFilter;
interface ISlicerState {
filters: ISlicerFilter[];
}
Example of slicer state
let slicerState = {
filters: [{
$schema: "http://powerbi.com/product/schema#advanced",
target: {
table: "Store",
column: "Number"
},
logicalOperator: "And",
conditions: [
{
operator: "GreaterThanOrEqual",
value: 30
},
{
operator: "LessThan",
value: 40
}
],
filterType: pbi.models.FilterType.AdvancedFilter
}]
}
Slicer APIs has methods which allow you to get and set slicer state.
getSlicerState(): Promise<models.ISlicerState>;
setSlicerState(state: models.ISlicerState): Promise<void>;
To get a slicer state you need to find the slicer visual and call a method called getSlicerState on this visual.
visual.getSlicerState()
.then(state => {
...
});
Result is of type ISlicerState. You can find an example of ISlicerState above.
Default state case
If the slicer is in default state (i.e. no filter applied), getSlicerState will give ISlicerState with empty array of filters.
To set a slicer state you need to create a SlicerState object, find the slicer visual and call a method called setSlicerState with the slicer state you created. To reset a slicer, just call setSlicerState with an empty array of filters.
visual.setSlicerState(state)
.catch(errors => {
// Handle error
});
A slicer can be shown from one of various types:
This slicer shows a list, dropdown values (or other views), where you can select single or multiple items to filter the report accordingly.
To change a selection for these types of slicers you need to create an object of type IBasicFilter. example:
const basicFilter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Store",
column: "Count"
},
operator: "In",
values: [1,2,3,4],
filterType: pbi.models.FilterType.BasicFilter
};
visual.setSlicerState({
filters: [basicFilter]
});
Range slicers support conditions like: Between, Before, After, .. To change a selection for range slicers you need to create an object of type IAdvancedFilter. example:
const advancedFilter = {
$schema: "http://powerbi.com/product/schema#advanced",
target: {
table: "Store",
column: "Number"
},
logicalOperator: "And",
conditions: [
{
operator: "GreaterThanOrEqual",
value: 30
},
{
operator: "LessThan",
value: 40
}
],
filterType: pbi.models.FilterType.AdvancedFilter
};
visual.setSlicerState({
filters: [advancedFilter]
});
Relative date slicers support conditions like: Last Week, Last 5 Years, ... To change a selection for Relative date slicers you need to create an object of type IRelativeDateFilter. example:
const relativeDateFilter = {
$schema: "http://powerbi.com/product/schema#relativeDate",
target: {
table: "Sales",
column: "OrderDate"
},
operator: pbi.models.RelativeDateOperators.InLast,
timeUnitsCount: 30,
timeUnitType: pbi.models.RelativeDateFilterTimeUnit.Days,
includeToday: true,
filterType: pbi.models.FilterType.RelativeDate
};
visual.setSlicerState({
filters: [relativeDateFilter]
});
Again, You can read more about creating filter object in Filters page.
Note: Custom slicers expected to reach production on July 13, 2018.
To get custom slicer selection, call getSlicerState. This works the same as in native slicers. To set custom slicer selection, you need to create an object of type ISlicerFilter, which can be of different types: IBasicFilter, IAdvancedFilter and more. see above.
Different custom slicers (custom visual slicers) support different type of filters. To understand what the filter type you need to create in order to change the slicer, you may call visual.getSlicerState() on the specific slicer you need to change.
Note: Custom slicers expected to reach production on July 6, 2018.
Slicers parameter added to load config. This way, you can change each slicer by id on report load. To do this, you should pass an array of ISlicer, where each ISlicer contains a slicer selector and a slicer state. The slicer selector selects a slicer to change.
interface ISlicer {
// Selects a slicer to change.
selector: SlicerSelector;
// A new state of the slicer
state: ISlicerState;
}
// Use slicers parameter in load config to set slicers on load.
interface IReportLoadConfiguration {
...
slicers?: ISlicer[];
...
}
Note
Be careful when setting slicers on load if slicers and in the same sync group. If you pass multiple slicers to change on load, and they are in the same sync group, the result will be unexpected.
There is no API to change the slicers sync configuration. But, sync configuration saved in the report will be respected in slicer APIs. That means, if you set a slicer through the API, all slicers in the same sync group will be affected.
Read more about slicers sync in Slicers docs
- Legacy custom slicers are not supported.
- Native slicers support only one filter.
- Calling setSlicerState on a visual which is not a slicer will return rejected promise with an error: “Operation works on slicers only”.