Can I use a condition in GETPIVOTDATA?

Doghouse308

New Member
Joined
May 28, 2019
Messages
13
I have a pivot table that is tracking occurrence per device as follows:

PJ10651
PJ10891
PJ11561
PJ11971
PJ15732
PJ17541
PJ31591
PJ32233
PJ36481
PJ39441
PJ40641
PJ40731
PJ43091


<colgroup><col><col></colgroup><tbody>
</tbody>


I need to find a way to count the number of instances with 2 and with 3 or more occurrences per device. I tried using GETPIVOTDATA, but cannot find a way to count conditionally. I also tried using the GETPIVOTDATA inside of a countif without success.

I greatly appreciate any suggestions. Once I have this one last piece of the puzzle I can complete a dashboard project.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I'll try using the function inside of an onchange to refresh the data preparation table.

A workaround that worked for me.
I added a new argument to the function: a range, a cell, that belongs to the PVT - just to force function to recalculates when the PVT is refreshed

Something like this

PivotTable1 in, say, A3:Bn

A
B
1
2
3
Code​
Count of Code​
4
PJ1065​
1​
5
PJ1089​
1​
6
PJ1156​
1​
7
PJ1197​
1​
8
PJ1573​
2​
9
PJ1754​
1​
10
PJ3159​
1​
11
PJ3223​
3​
12
PJ3648​
1​
13
PJ3944​
1​
14
PJ4064​
1​
15
PJ4073​
1​
16
PJ4309​
1​
17
Grand Total​
16​
18

<tbody>
</tbody>


Function (new version)
Code:
Function GetCountIfV2(sPVT As String, sField As String, val As String, [COLOR=#ff0000]r As Range[/COLOR])
    GetCountIfV2 = Application.CountIf(ActiveSheet.PivotTables(sPVT).PivotFields(sField).DataRange, val)
End Function

Excel
to get =2
=GetCountIfV2("PivotTable1","Count of Code","=2",A3)

to get >=2
=GetCountIfV2("PivotTable1","Count of Code",">=2",A3)

M.
 
Last edited:
Upvote 0
If you want "to avoid hard-coded references", as you said in post 6, maybe you can use this event (Not tested)

Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    'code here
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top