One of the missing features in SSAS we found is filtering by list of values. In most of clients (Excel, Panorama) you are able to set a filter for one or several items. But filter cube by 300 items could be tricky task. Let say we have a bussines task like: filter a Adventure Works cube by 300 different products. These 300 products spread in all Products categories hierarchy and depend to different product categories. To select 300 products you have to open Product hierarchy and check each item. To check 300 different items could be a tricky tasks, isn’t it? The solution – to find a way programically select items.
Using Excel 2007 you can filter Pivot Table items using “VisibleItemsList” property. Look for VBA code below.
Dim iTmp As Integer
Dim ProductKey As String
Dim ProductKeyArray() As String
iTmp = 2
ProductKey = Excel.Worksheets(“Filter”).Cells(iTmp, 1).Value
While ProductKey <> Empty
ReDim Preserve ProductKeyArray(iTmp – 2)
ProductKeyArray(iTmp – 2) = “[Product].[Product Key].&[" & ProductKey & "]”
iTmp = iTmp + 1
ProductKey = Excel.Worksheets(“Filter”).Cells(iTmp, 1).Value
Wend
On Error Resume Next
Excel.Worksheets(“Data”).PivotTables(Excel.Worksheets(“Data”).PivotTables.Count).PivotFields( _
“[Product].[Product Key].[Product Key]“).VisibleItemsList = _
ProductKeyArray()
To look how this works, download file FilterPTSample.xlsm (rename zip to FilterPTSample.xlsm). Enable macros and data connection, change data connection to Adventure Works, put product keys on sheet “Filter”, down from “A2″ and then press “Filter by product key”. After filter was applied, in 1′st sheet (“Data”) , attribute Product Key and hierarchy “Product Categories” you have to see only products from sheet “Filter”. To clear filter, choose “Clear” from Data\Sort & Filter box.



Ramunas,
I tried doing this but I couldn’t get it to work. I have a similar situation where I am trying to make a filter for my entire workbook (14 pivot tables). I want to be able to filter on the Product field and I am using a drop down to select them. The drop down is a lot shorter than 300 more like 15. How would you change your code for my use? Also, I referenced cell B2 on my “Summary” worksheet for my drop down.
Thanks,
Travis
I’ve tryed the code but it is incredibly SLOW.
What I can do to speed it up?
Regards
This was a very useful tip. Thanks again Ramunas.
It have tried it. It is good and not that much slow as i thought after reading Giancarlo’s comment.
It’s the first time I commented here and I should say that you share us genuine, and quality information for bloggers! Good job.
p.s. You have a very good template . Where did you find it?
Ramunas,
Great macro.
When I tried it I am getting a message:
The item could not be found in OLAP Cube.
Any advice?
Thanks in advance.
markimark ,
seems you are using another version of Adventure Wroks database. Try edit object names that match you database.
Thanks,
Ramunas
Thanks,
Ramunas!!!
Ramunas, thank for your feedback!This is the second time that Excel 2007 and SSAS team refuse to get responsibility for an issue that involves a combination of factors. This is really disappointing.