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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

getSlicerState() not working for slicers with hierarchy

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.

 

playground-reproduction.png

 

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);
}

 

 

 

 

Status: New
Comments
v-chuncz-msft
Community Support

@Piloos 

 

You may check Limitations section and share feedback via Ideas.

Piloos
Advocate I

@v-chuncz-msft , ok... I find it very hard to see this as a limitation.  It basically voids the complete use case of getting & setting slicers.  Moreover, the JavaScript API just throws an exception in stead of passing an error message.  In order to cope with this behavior you need to implement a try...catch block.

 

Nevertheless, I created the suggestion: https://ideas.powerbi.com/ideas/idea/?ideaid=b2186200-2683-eb11-8ced-0003ff454088

Piloos
Advocate I

For the occasional visitor.  This issue is solved since powerbi-client version 2.21.  Hierarchical slicers are now supported for getting/setting state and applying state 'on load'.