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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
osb1
Helper I
Helper I

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!

1 ACCEPTED SOLUTION

thank you 

 

That's right I also tried this but it didn't work

 

I think powerbi library is not too much clear about what we can do as a developers. After several testing I found a function that can help to grab data from the slicer, it can be used to try to pre-validate what you want to send against slicer data and avoid having non existing values.  

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

    // Retrieve the page that contain the visual. For the sample report it will be the active page
    let page = pages.filter(function (page) {
        return page.isActive
    })[0];

    const visuals = await page.getVisuals();

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

    const result = await visual.exportData(models.ExportDataType.Summarized);
    console.log(result.data);
}
catch (errors) {
    console.log(errors);
}



View solution in original post

4 REPLIES 4
osb1
Helper I
Helper I

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

 

That's right I also tried this but it didn't work

I think powerbi library is not too much clear about what we can do as a developers. After several testing I found a function that can help to grab data from the slicer, it can be used to try to pre-validate what you want to send against slicer data and avoid having non existing values.  

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

    // Retrieve the page that contain the visual. For the sample report it will be the active page
    let page = pages.filter(function (page) {
        return page.isActive
    })[0];

    const visuals = await page.getVisuals();

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

    const result = await visual.exportData(models.ExportDataType.Summarized);
    console.log(result.data);
}
catch (errors) {
    console.log(errors);
}

 

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.

thank you 

 

That's right I also tried this but it didn't work

 

I think powerbi library is not too much clear about what we can do as a developers. After several testing I found a function that can help to grab data from the slicer, it can be used to try to pre-validate what you want to send against slicer data and avoid having non existing values.  

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

    // Retrieve the page that contain the visual. For the sample report it will be the active page
    let page = pages.filter(function (page) {
        return page.isActive
    })[0];

    const visuals = await page.getVisuals();

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

    const result = await visual.exportData(models.ExportDataType.Summarized);
    console.log(result.data);
}
catch (errors) {
    console.log(errors);
}



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 MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.