Created
May 17, 2019 02:18
-
-
Save joshuatz/38938cc95f4eecbdd13e2890a9d67ecf to your computer and use it in GitHub Desktop.
Reset all Slicers (visual filter widgets) within a Power BI Javascript Embed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function resetAllSlicers() { | |
// This will target first embed on page, you can easily change | |
let report = powerbi.embeds[0]; | |
let pages = await report.getPages(); | |
for (let x = 0; x < pages.length; x++) { | |
let visuals = await pages[x].getVisuals(); | |
for (let x = 0; x < visuals.length; x++) { | |
if (visuals[x].type === 'slicer') { | |
// Clear state, but wait before moving on to next one, since clearing a filter can have a cascade effect | |
try { | |
let cleared = await visuals[x].setSlicerState({ 'filters': [] }); | |
} | |
catch (e) { | |
// setSlicerState or getSlicerState will fail if visual element is not initialized | |
// visualConfigIsNotInitialized | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment