Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
The getSlicerState() call gives an error when it is called on a slicer which has hierarchy and where on the second level not all items are selected.
As you see in the image below, only cities 'Chicago' and 'Antwerpen' are selected on the second level.
If you make sure that you don't have 'incomplete' selections on the second level, the issue is not present.
This was probably also noticed here:
https://community.powerbi.com/t5/Issues/getSlicerState-error/idc-p/1692319#M64661
You can easily reproduce this in the new Power BI playground using this PBIX (https://bizzcontrol-public.s3.eu-central-1.amazonaws.com/get-slicer-state.pbix) and the following code:
let loadedResolve, reportLoaded = new Promise((res, rej) => { loadedResolve = res; });
let renderedResolve, reportRendered = new Promise((res, rej) => { renderedResolve = res; });
// Embed a Power BI report in the given HTML element with the given configurations
// Read more about how to embed a Power BI report in your application here: https://go.microsoft.com/fwlink/?linkid=2153590
function embedPowerBIReport() {
// Read embed application token
let accessToken = EMBED_ACCESS_TOKEN;
// Read embed URL
let embedUrl = EMBED_URL;
// Read report Id
let embedReportId = REPORT_ID;
// Read embed type from radio
let tokenType = TOKEN_TYPE;
// Get models. models contains enums that can be used.
let models = window['powerbi-client'].models;
// We give All permissions to demonstrate switching between View and Edit mode and saving report.
let permissions = models.Permissions.All;
// Create the embed configuration object for the report
// For more information see https://go.microsoft.com/fwlink/?linkid=2153590
let config = {
type: 'report',
tokenType: tokenType == '0' ? models.TokenType.Aad : models.TokenType.Embed,
accessToken: accessToken,
embedUrl: embedUrl,
id: embedReportId,
permissions: permissions,
settings: {
panes: {
filters: {
visible: true
},
pageNavigation: {
visible: true
}
}
}
};
// Get a reference to the embedded report HTML element
let embedContainer = $('#embedContainer')[0];
// Embed the report and display it within the div container.
report = powerbi.embed(embedContainer, config);
// report.off removes all event handlers for a specific event
report.off("loaded");
// report.on will add an event handler
report.on("loaded", function () {
loadedResolve();
report.off("loaded");
});
report.on("error", function (event) {
console.log(event.detail);
});
// report.off removes all event handlers for a specific event
report.off("rendered");
// report.on will add an event handler
report.on("rendered", function () {
renderedResolve();
report.off("rendered");
});
}
embedPowerBIReport();
await reportLoaded;
await reportRendered;
// Retrieve the page collection and get the visuals for the first page.
try {
const pages = await report.getPages();
let pageWithSlicer = pages[0]; // Retrieve the page that contains the slicer
const visuals = await pageWithSlicer.getVisuals();
// Retrieve the slicer visual
let slicer = visuals.filter(function (visual) {
return visual.type === "slicer";
})[0];
// Get the slicer state
console.log('trying to get the slicer state')
const state = await slicer.getSlicerState();
console.log('could get the slicer state')
console.log(state);
}
catch (errors) {
console.log(errors);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.