Forum Discussion
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!
- Anonymous1 year ago
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); }
4 Replies
- AnonymousNot applicable
Hi Anonymous ,
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.
- AnonymousNot applicable
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); }
- AnonymousNot applicable
Hello Anonymous 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.
- AnonymousNot applicable
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); }