Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
MicrosoftUser42
Regular Visitor

PowerBI setSlicerState fails to set Hierarchy Slicer when executed through Python (JavaScript API)

I have a JSON with multiple reports configurations, each item has a slicers key, which again contains multiple slicer definitions that look like this:

"slicers": [
    {
        "slicer_filter": {
            "$schema": "http://powerbi.com/product/schema#hierarchy",
            "target": [
                {
                    "table": "Regional Hierarchy",
                    "column": "Lead Country"
                },
                {
                    "table": "Regional Hierarchy",
                    "column": "Country"
                }
            ],
            "filterType": 9,
            "hierarchyData": [
                {
                    "operator": "Selected",
                    "value": "LC Austria"
                }
            ]
        },
        "slicer_id": "e076ac766bb91090aa01"
    },
    ...
]

 

This hierarchy filter specifically causes issues. The slicer filter works perfectly fine, when I use it in the Playground like this:

const filter = {
            "$schema": "http://powerbi.com/product/schema#hierarchy",
            "target": [
                {
                    "table": "Regional Hierarchy",
                    "column": "Lead Country"
                },
                {
                    "table": "Regional Hierarchy",
                    "column": "Country"
                }
            ],
            "filterType": 9,
            "hierarchyData": [
                {
                    "operator": "Selected",
                    "value": "LC Austria"
                }
            ]
        };

// Retrieve the page collection and get the visuals for the active page.
try {
    const pages = await report.getPages();

    // Retrieve the active page.
    let page = pages.filter(function (page) {
        return page.isActive;
    })[0];

    const visuals = await page.getVisuals();

    // Retrieve the target visual.
    let slicer = visuals.filter(function (visual) {
        return visual.type === "slicer" && visual.name === "e076ac766bb91090aa01";
    })[0];

    // Set the slicer state which contains the slicer filters.
    await slicer.setSlicerState({ filters: [filter] });
    console.log("Slicer was set.");
}
catch (errors) {
    console.log(errors);
}

 

But when I use the exact same filter using python like this:

for slicer in slicers:
  slicer_id = slicer["slicer_id"]
  slicer_filter = slicer["slicer_filter"]
  result = driver.execute_async_script(f"""
      var callback = arguments[0];
      (async function() {{
          const filter = {slicer_filter};
          try {{
              const pages = await report.getPages();
              let page = pages.filter(function(page) {{
                  return page.isActive;
              }})[0];

              const visuals = await page.getVisuals();
              let slicer = visuals.filter(function(visual) {{
                  return visual.type === "slicer" && visual.name === "{slicer_id}";
              }})[0];

              await slicer.setSlicerState({{ filters: [filter] }});
              
              callback("Slicer was set.");
          }} catch (errors) {{
              callback("Error: " + JSON.stringify(errors, null, 2));
          }}
      }})();
    """)
  print(result)

 

I get:

Error: {
  "message": "slicerTargetDoesNotMatch",
  "detailedMessage": "Filters target doesn't match slicer target",
  "level": 3
}

 

The approach I am using does work for basic slicers like:

{
    "slicer_filter": {
        "$schema": "http://powerbi.com/product/schema#basic",
        "target": {
            "table": "Fiscal Calendar",
            "column": "Fiscal Year"
        },
        "operator": "In",
        "values": [2025],
        "filterType": 1
    },
    "slicer_id": "d1e2c93c8009480134b1"
}

 

For those, I get:

Slicer was set.

 

Am I missing something?

 

Best Regards

1 ACCEPTED SOLUTION
MicrosoftUser42
Regular Visitor

Excuse me I was not using the latest version of powerbi-client. Since hierarchy slicers have only been added in a later release, the target of the slicer was reported incorrectly to the API.
Using the latest stable version of powerbi-client fixed the issue:
<script src="https://cdn.jsdelivr.net/npm/powerbi-client@2.23.1/dist/powerbi.min.js"></script>

View solution in original post

1 REPLY 1
MicrosoftUser42
Regular Visitor

Excuse me I was not using the latest version of powerbi-client. Since hierarchy slicers have only been added in a later release, the target of the slicer was reported incorrectly to the API.
Using the latest stable version of powerbi-client fixed the issue:
<script src="https://cdn.jsdelivr.net/npm/powerbi-client@2.23.1/dist/powerbi.min.js"></script>

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors