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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
osb1
Frequent Visitor

Get all data from an slicer using powerbi-client js library, not only selected values

Hello comunity

 

I am working in one implementation using the library powerbi-client javascript library to embedd a pbix report in my web, this report has 2 slicer with data. 

 

the first slicer has a list of years 

the second slicer has a list of texts

 

if you select a value in the first slicer, values in the second slicer will change because it depends on the first.

 

Analyzing the powerbi sandbox page I found the way to set values from my web to my embedded report using javascript, and it is working fine, but now I realized that if I send a value that doesn't exist in the slicer, the value appears in the list.

i.e. my first slicer has as values

2021

2022


I send the value 2023 (this doesn't exist in the slicer)

using the next code:

 

``` part of the implementation is not added in this code example

let pageWithSlicer = pages.filter(function (page) {
 return page.isActive;
})[0];

const visuals = await pageWithSlicer.getVisuals();
            
// Retrieve the target visual.
let slicer = visuals.filter(function (visual) {
 return visual.type === "slicer" && visual.title.includes(sliceName);
})[0];

await slicer.setSlicerState({ filters: [filter] });

 




the slicer now has the next values 

2021
2022
2023

I was wondering if anybody could help me know if there's any way to avoid having new values in slicer when this value doesn't exist as a valid value, or could you please help me know if there's any way to get the data from an slicer so that I will be able to check agains the value sent via web.


 

Kind Regards!

2 REPLIES 2
osb1
Frequent Visitor

Hello @v-yangliu-msft  I tested what you recomended, but unfortunatelly the function getFilters() doesn't contain a definition for property values, so I wasn't able to access the values loaded in the dropd down slicer filter as you can see in the next screenshot.

osb1_0-1720472760260.png

 

v-yangliu-msft
Community Support
Community Support

Hi @osb1 ,

 

I think you may need to update your code to check whether the value is valid.

You may refer to the code as below and I hope it could help you to resolve your issue.

const slicer = visuals.find((visual) => visual.type === 'slicer' && visual.title.includes(sliceName));



if (slicer) {

    // Get all slicer data (valid options)

    const slicerData = await slicer.getFilters();



    // Check if the value you want to set exists in the slicer data

    const valueToSet = '2023'; // Example value

    const isValidValue = slicerData.some((item) => item.values.includes(valueToSet));



    if (isValidValue) {

        // Set the slicer state

        await slicer.setSlicerState({ filters: [filter] });

    } else {

        console.log(`Invalid value: ${valueToSet}`);

    }

} else {

    console.log('Slicer not found.');

}

 

This is the related document, you can view this content:

powerbi-client package | Microsoft Learn

Use slicers in Power BI embedded analytics | Microsoft Learn

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

August Carousel

Fabric Community Update - August 2024

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