PDA

View Full Version : OLAP Pivot Table - Variable number of filters



ettedo
11-27-2017, 05:12 PM
Hi everyone :hi:

This is my first post on a tech support forum, so please bear with me :)

I've been tasked with automating my organisation's financial reports using VBA to manipulate OLAP pivot tables in Excel (2016, 64bit).
My design is based around userforms for manual selection of which report to run. The OLAP pivot table is then filtered based on the selected parameters in the userform.

The part that I'm having troubles with is filtering my table using a variable number of variable values...
The report I am currently trying to produce requires a filter on YTD (Year to Date), so there could be a possible 1-12 number of variable filters (e.g. 2 filters for the August report, 12 filters for the June report etc)

I've found some code on an old post in this forum which had the base logic for what I'm trying to do, but for some reason it produces an error for me [Run-time error '1004': Query (1, 39) Parser: The syntax for ',' is incorrect]. There appears to be absolutely no information on Google for this particular error in an Excel environment.

Below is my script that is run when the user clicks 'Submit' on the userform for selecting a report;


For Each PivotTable In Sheets("REPORT").PivotTables
PivotTable.RefreshTable
PivotTable.ClearTable
PivotTable.AddDataField PivotTable.CubeFields("[Measures].[Amount In Lc]")
PivotTable.AllowMultipleFilters = True
If ComboBox_Report.Value = "Income Statement" Then
With PivotTable.CubeFields("[Time Hierarchy].[Period]")
.Orientation = xlRowField
.Position = 1
End With

'----THIS IS THE SECTION THAT APPLIES THE VARIABLE FILTERS----
Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long
cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = """[Time Hierarchy].[Period].&[" & (vPer) & "]"""
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & """[Time Hierarchy].[Period].&[" & (vPer) & "]"""
Loop

'----THIS PRODUCES THE BELOW VALUE FOR VARIABLE 'PivotStr', WHICH BTW WORKS WHEN I RUN IT MANUALLY AS TEXT
'---- "[Time Hierarchy].[Period].&[201707]","[Time Hierarchy].[Period].&[201708]","[Time Hierarchy].[Period].&[201709]","[Time Hierarchy].[Period].&[201710]")
msgbox1 = MsgBox(PivotStr)


'----THIS IS THE LINE THAT GIVES ME ERROR: (The syntax for ',' is incorrect)
PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = Array(PivotStr)
End If

Next PivotTable



When I record a macro to perform the above filters it produces the below code which is identical to what I'm entering using the PivotStr variable :banghead:


ActiveSheet.PivotTables("PivotTable5").PivotFields( _
"[Time Hierarchy].[Period].[Period]").VisibleItemsList = Array( _
"[Time Hierarchy].[Period].&[201707]", "[Time Hierarchy].[Period].&[201708]", _
"[Time Hierarchy].[Period].&[201709]", "[Time Hierarchy].[Period].&[201710]")


I know this could probably be achieved using a 'Select Case' and typing out every possible scenario, but this is clunky and inefficient

Hope someone can point me in the right direction :)

Thanks in advance for anyone's assistance!

Aflatoon
11-28-2017, 06:35 AM
You're actually passing one string value to the Array operator, so all you get is an array with one string in it, not the array of values you need. Try this:


Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").Range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Loop

PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = Split(PivotStr, ",")

ettedo
12-03-2017, 03:50 PM
[Sorry for the delayed response - I'd assumed I'd get some form of email notification for activity on this thread, but that didn't happen]

Thanks for your help Aflatoon :)
Your suggestion makes complete sense, and I think I'm definitely 1 step closer now. Unfortunately I get an XML error though... I found another thread online where someone suggested to put the split into another variable as below:

CODE:

DatesArray = Split(PivotStr, ",")
PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = DatesArray


This also produces an XML error for me, but seemed to work for the poster on the thread.
(BTW - Please excuse me if my terminology is a little off - I'm an SQL person, and only just starting to learn VBA!)

I haven't declared "DatesArray" which may be the problem. I attempted to declare as a String, but that gave me a 'type mismatch' error.

Any ideas?

Thanks :)

Aflatoon
12-04-2017, 05:02 AM
What exactly do you mean by an XML error?

ettedo
12-04-2017, 03:19 PM
The error produced when entering either ".VisibleItemsList = DatesArray" or ".VisibleItemsList = Split(PivotStr, ",")" is:
"Run-time error ‘1004’: XML for Analysis parser: The restriction value provided by the consumer either does not match other restrictions or refers to an unknown object."

When I declare DatesArray as 'Variant' type, get the below error:
"Run-time error '1004': An MDX expression was expected while a full statement was specified."


Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long, i As Long cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = "[Time Hierarchy].[Period].&;[" & (vPer) & "]"
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&;[" & (vPer) & "]"
Loop
Dim DatesArray As Variant
DatesArray = Split(PivotStr, ",")
PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = DatesArray

Aflatoon
12-05-2017, 01:48 AM
Where did the semicolons come from?

Both of these:


[Time Hierarchy].[Period].&;

need the semicolons removed.

ettedo
12-05-2017, 03:46 PM
Thanks for picking that up. I think I'd added those while testing at some point. That has fixed the error I was getting before :) :)

Once removing the semi-colons, I have run the below 3 variations of code and am now getting the following error for all 3: "Run-time error '1004': The item could not be found in the OLAP Cube."
I am certain the values exist in the cube though...

.VisibleItemsList = Split(PivotStr, ",") :

Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long, i As Long cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Loop

PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = Split(PivotStr, ",")

.VisibleItemsList = DatesArray (no Dim type):

Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long, i As Long cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Loop
Dim DatesArray
DatesArray = Split(PivotStr, ",")

PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = DatesArray



.VisibleItemsList = DatesArray (DatesArray As Variant):

Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long, i As Long cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Loop
Dim DatesArray
DatesArray = Split(PivotStr, ",")

PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = DatesArray
Thanks again for your help and patience :)

Aflatoon
12-06-2017, 03:09 AM
I am certain the values exist in the cube though...


The error would suggest otherwise. ;)

What is the exact value of PivotStr​?

Arpit
02-29-2020, 07:51 PM
Thanks for picking that up. I think I'd added those while testing at some point. That has fixed the error I was getting before :) :)

Once removing the semi-colons, I have run the below 3 variations of code and am now getting the following error for all 3: "Run-time error '1004': The item could not be found in the OLAP Cube."
I am certain the values exist in the cube though...

.VisibleItemsList = Split(PivotStr, ",") :

Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long, i As Long cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Loop

PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = Split(PivotStr, ",")

.VisibleItemsList = DatesArray (no Dim type):

Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long, i As Long cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Loop
Dim DatesArray
DatesArray = Split(PivotStr, ",")

PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = DatesArray



.VisibleItemsList = DatesArray (DatesArray As Variant):

Dim PivotStr As String, cMth As Long, fMth As Long, vPer As Long, fVar As Long, i As Long cMth = Month(Now) - 1
fMth = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
fVar = Application.WorksheetFunction.VLookup(cMth, Sheets("REFERENCE").range("A2:G13"), 2, False)
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Do While fVar <= fMth And fVar > 1
fVar = fVar - 1
vPer = Application.WorksheetFunction.VLookup(fVar, Sheets("REFERENCE").range("B2:E13"), 4, False)
PivotStr = PivotStr & ", " & "[Time Hierarchy].[Period].&[" & (vPer) & "]"
Loop
Dim DatesArray
DatesArray = Split(PivotStr, ",")

PivotTable.PivotFields("[Time Hierarchy].[Period].[Period]").VisibleItemsList = DatesArray
Thanks again for your help and patience :)