Forum Discussion
Get all data from an slicer using powerbi-client js library, not only selected values
- 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); }
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.
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);
}