Dev Mehta
Dev Mehta

Reputation: 33

Change the slicer state on clicking a button

I have 4 buttons that indicate different ranges and a slicer in my report. I want to change the slicer value according to the button I click.

Is it possible to change the value of the slicer on clicking the button?

Upvotes: 1

Views: 5860

Answers (3)

d3ll_in_algovacay
d3ll_in_algovacay

Reputation: 1

There is no need for 4 copies of the slicer. Just change the slicer options, save it into bookmarks and connect each button to the bookmark depicting the slicer option you want to depict.

Upvotes: 0

Rohit Ronte
Rohit Ronte

Reputation: 129

You can do this in Power BI by applying bookmarks to slicer and add those bookmarks in action property of buttons. In Power BI Embed you can apply buttonClick event to the button and set the state of slicer but that event will be triggered for all the buttons in the report. Instead of having 4 buttons you can use only one button and set slicer state according to the number of times the button is clicked. To change the state of a slicer on clicking a button in an Embed report, Please find the below code snippet:

  1. Set the Filter config:
const filter = {
    $schema: "http://powerbi.com/product/schema#advanced",
    target: {
        table: "Table_Name",
        column: "Column_Name"
    },
    filterType: models.FilterType.Advanced,
    logicalOperator: "And",
    conditions: [
        // Add condition here as per your requirement
    ]
};
  1. Get the current active page:
const pages = await report.getPages();

    // Retrieve the active page.
let page = pages.filter(function (page) {
    return page.isActive;
})[0];
  1. Get all the visuals:
const visuals = await page.getVisuals();
  1. Find the slicer:
let slicer = visuals.filter(function (visual) {
        return visual.type === "slicer" && visual.name === "Visual_Name";
    })[0];
  1. Add the button click event and set the state of slicer:
report.on("buttonClicked", function (event) {
        slicer.setSlicerState({ filters: [filter] });
    });

Pleaser find references here: https://learn.microsoft.com/javascript/api/overview/powerbi/handle-events#buttonclicked
https://learn.microsoft.com/javascript/api/overview/powerbi/control-report-slicers#set-slicer-state

Upvotes: 1

ZZZSharePoint
ZZZSharePoint

Reputation: 1351

Make 4 copy of slicer and select different value in each slicer. Now select the Bookmark and selection pane from ribbon and hide all 3 slicer and keep one visible. And then select the property of the button and in Action , choose Bookmark and name of bookmark created above.

Same thing you need to do for other 3 buttons. Each time selecting a new slicer.

Upvotes: 0

Related Questions