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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Issue in setting slicer value using JavaScript API

Hello Experts,

We need your help in this.
We have various PowerBI reports that are used on several pages in Drupal based Website, each with slicers in the visualisations. We'd like to apply a slicer filter keyword using the JavaScript API, only if the slicer contains that given keyword.

We found that setting the slicer keyword when the keyword is not in the dropdown results in the keyword being added to the dropdown. To avoid this, we've tried checking if the filter is available in the dropdown before setting it. However after fetching the slicer filter values, it only returns the default value and not the others.
Here's a simplified version of the code we've got so far:

report.on('rendered', () => {
  report.getPages().then(pages => {
    const filter = {
      $schema: 'http://powerbi.com/product/schema#basic',
      target: {
        table: 'Organisation Source',
        column: 'Organisation Description'
      },
      operator: 'In',
      values: ['SLICER KEYWORD I WANT TO SET'],
    };
    pages.forEach(page => {
      page.getVisuals().then(visuals => {
        for (visual of visuals) {
          if (visual.type !== 'slicer') {
            continue;
          }
          visual.getSlicerState().then(state => {
            // state.filters.values === ['Statewide'] not ['Statewide', 'another option', ...']
            if (state.filters.values.includes('SLICER KEYWORD I WANT TO SET')) {
              visual.setSlicerState({
                filters: [filter]
              })
            }
          });
        }
      });
    });
  });
});

However note that after calling `visual.getSlicerState()`, `state.filters.values` only includes 1 (default) value, with no other dropdown values. How can I access the full list of slicer drop-down values, regardless of whether they're checked?

Thanks!

2 REPLIES 2
v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

Do you Apply filter in Javascript APIs in embedding scenarios?

If so, please post in Develop forums where people have more experience in Power BI Embed.

 

Based on my research, i find a useful article you may refer to

https://github.com/Microsoft/PowerBI-JavaScript/wiki/Slicers

According to it :

Categorical slicers supports displaying a list, dropdown or cards of values, 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 IBasicFilter object. 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]
});

 

Best Regards
Maggie

Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I don't think Maggie's reply answers this question

"However note that after calling `visual.getSlicerState()`, `state.filters.values` only includes 1 (default) value, with no other dropdown values. How can I access the full list of slicer drop-down values, regardless of whether they're checked?"

I have the same question.  How do I get a list of all the options for a slicer in an embedded PBI using the javascript api.  visual.getSlicerState() only lists the selected options.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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