I am trying to filter a column in an SSAS cube pivot table. I want to exclude only two identified values from that column, and select all the rest values.
Somehow my code is excluding many other values and selecting only few other ones.
Can someone help, and tell me what I am doing wrong? Here is the code:
ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"[MasterData Publisher Groups].[Publisher Group].[Publisher Group]"). _
ClearAllFilters
Range("E8").Select
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim includeItems As Collection
Dim item As Variant
Dim i As Long
Dim excludeList As Variant
Dim visibleItemsArray() As Variant
Dim itemCount As Long
' Set your PivotTable and PivotField
Set pt = ActiveSheet.PivotTables("PivotTable1")
Set pf = pt.PivotFields("[MasterData Publisher Groups].[Publisher Group].[Publisher Group]")
' Refresh the PivotTable to ensure it has the latest data
pt.RefreshTable
' Define the items to exclude
excludeList = Array("[MasterData Publisher Groups].[Publisher Group].&[30118]", _
"[MasterData Publisher Groups].[Publisher Group].&[27123]")
' Initialize the collection to store items to include
Set includeItems = New Collection
' Loop through all items in the pivot field
For Each pi In pf.PivotItems
' Check if the item is in the exclude list
Dim shouldExclude As Boolean
shouldExclude = False
For i = LBound(excludeList) To UBound(excludeList)
If pi.Name = excludeList(i) Then
shouldExclude = True
Exit For
End If
Next i
' Add the item to includeItems if it should not be excluded
If Not shouldExclude Then
includeItems.Add pi.Name
End If
Next pi
' Convert the collection to an array
itemCount = includeItems.count
ReDim visibleItemsArray(1 To itemCount)
For i = 1 To itemCount
visibleItemsArray(i) = includeItems(i)
Next i
' Clear existing filters
pf.ClearAllFilters
' Set the VisibleItemsList property
pf.visibleItemsList = visibleItemsArray