28
The Table in Figure 7-8 results from the changes outlined in the previous paragraph. Because the prices
do not vary in the source data, it is not surprising that the weighted average prices for each product in
the PivotTable do not vary either.
Figure 7-8
For example, say you don’t have Revenue in the source data, and you decide to calculate it by defining a
CalculatedFieldequal to Pricemultiplied by NumberSold. This would not give the correct result.
You can’t get Revenue by multiplying the sum of Price by the sum of NumberSold, except in the spe-
cial case where only one record from the source data is represented in each cell of the PivotTable.
PivotItems
Each PivotField object has a PivotItems collection associated with it. You can access the PivotItems
using the PivotItems method of the PivotField object. It is a bit peculiar that this is a method and
not a property, and is in contrast to the HiddenItems property and VisibleItems property of the
PivotFieldobject that return subsets of the PivotItemscollection.
The PivotItemscollection contains the unique values in a field. For example, the Product field in the
source data has four unique values—”Apples”, “Mangoes”, “Oranges”, and “Pears”, which consti-
tute the PivotItems collection for that field.
Grouping
You can group the items in a field in any way you like. For example, NSW, QLD, and VIC(New South
Wales, Queensland, and Victoria, respectively) could be grouped as EasternStates. This can be very
useful when you have many items in a field. You can also group dates, which have a predefined group
structure including years, quarters, and months.
Take care when creating CalculatedFields. You need to appreciate that the calcu-
lations are performed afterthe source data has been summed. In this example,
Revenueand NumberSoldwere summed and one sum was divided by the other
sum. This works fine for calculating a weighted average price and is also suitable
for simple addition or subtraction. Other calculations might not work as you expect.
171
Chapter 7: PivotTables