Forum Discussion
Goudriaan
May 30, 2023Copper Contributor
EXCEL ONLINE script filter pivottable with Power Automate data
Hi all, I have a script where I have a pivot table (PivotTable1) on worksheet Transportkosten_klant. I want to use the customer specific name using a Power Automate flow which runs every quarter...
- May 30, 2023
Perhaps like
function main(workbook: ExcelScript.Workbook) { const filterField = workbook .getWorksheet("Sheet1") .getPivotTable("ptSample") .getHierarchy("Category") .getPivotField("Category") const filter: ExcelScript.PivotLabelFilter = { condition: ExcelScript.LabelFilterCondition.contains, substring: "Microsoft", exclusive: false }; filterField.applyFilter({ labelFilter: filter }); }Just an idea, but works on attached file
SergeiBaklan
May 31, 2023Diamond Contributor
If for manual filter as here
script could be
function main(workbook: ExcelScript.Workbook) {
const pivot =
workbook
.getWorksheet("Sheet1")
.getPivotTable("ptSample")
const filterFiled =
pivot
.getHierarchy("Category")
.getFields()[0]
const filterBy =
filterFiled
.getItems()
.map( x => x.getName() )
.filter(x => x.includes("Microsoft") )
filterFiled
.applyFilter( {
manualFilter: { selectedItems: filterBy }
} )
}
it also works if move Category in rows
If finally you would like to have collapsed fields as
we may add to the script the block at the bottom
function main(workbook: ExcelScript.Workbook) {
const pivot =
workbook
.getWorksheet("Sheet1")
.getPivotTable("ptSample")
const filterFiled =
pivot
.getHierarchy("Category")
.getFields()[0]
const filterBy =
filterFiled
.getItems()
.map( x => x.getName() )
.filter(x => x.includes("Microsoft") )
filterFiled
.applyFilter( {
manualFilter: { selectedItems: filterBy }
} )
pivot.
getHierarchy("Product")
.getFields()[0]
.getItems()
.map( x => x.setIsExpanded(false))
}Goudriaan
Jun 01, 2023Copper Contributor
Thank you!
I found that the code from the first option is best usable for my situation. As it is a standard sheet which will be updated when other category names filtering, I don't need to add the collapse code.
KR,
- SergeiBaklanJun 01, 2023Diamond Contributor
Goudriaan , you are welcome.
More options we know the better, everything in Excel could be done by several different ways. What to use depends on concrete scenario.