Custom Data Calculations
This article will cover the types of custom calculations supported by Telerik RadPivotGrid control:
-
Calculated Fields (type of AggregateField)
-
Calculated Items
These calculations will help you generate efficient statistical reports in your production or business application depending on your specific requirements.
Calculated Fields
The Calculated Fields, which are type of Aggregate Field, do not directly compute their cell values based on the DataField from the data source. Instead, they use CalculationDataFields for setting one or more required fields and thereby, they calculate the values using an expression based on these data fields (Figure 1).
<telerik:PivotGridAggregateField DataField="Average Price"
CalculationDataFields="TotalPrice,Quantity" CalculationExpression="{0}/{1}">
</telerik:PivotGridAggregateField>
Figure 1: The Average Price field displays custom calculation depending on the used expression.
In case you need a greater control over the calculations, you can also achieve the sample scenario demonstrated above programmatically, which will enable you to make further custom alterations dynamically using your specific conditions.
<telerik:PivotGridAggregateField DataField="TotalPrice" Aggregate="Sum"
DataFormatString="{0:C}">
</telerik:PivotGridAggregateField>
<telerik:PivotGridAggregateField DataField="Quantity" Aggregate="Sum">
</telerik:PivotGridAggregateField>
<telerik:PivotGridAggregateField DataField="Average Price"
CalculationDataFields="TotalPrice,Quantity">
</telerik:PivotGridAggregateField>
Note that even if you set the CalculationExpression declaratively in the mark-up, you can still modify the default value of the e.CalculatedValue property during the ItemNeedCalculation event.
Calculated Items
Calculated items are summary values that extend items from existing GroupField (Row or Column field). You can define such items for each PivotGridColumnField or PivotGridRowField using their CalculatedItems collection.
You can use Calculated Items to place a custom summary at the end of your products, month sales, orders by city, etc.
The following sample will calculate prediction values of products for the year 1999 using the information from the previous years. Figure 2 shows the result when we implement the logic demonstrated in the code snippets below.
<telerik:PivotGridColumnField DataField="Year">
<CalculatedItems>
<telerik:PivotGridCalculatedItem
GroupName="Forecast for 1999">
</telerik:PivotGridCalculatedItem>
</CalculatedItems>
</telerik:PivotGridColumnField>
Figure 2: For a live demo of this sample please check the links in the Other Resources section.
SolveOrder property
Since Q3 2014, PivotGridCalculatedItem exposes SolveOrder property, which could be used in complex scenarios with multiple CalculatedItems, where one (or more) calculation(s) depend on the calculated values of other calculated items. With SolveOrder property you can specify the order by which the calculations will be performed.
Limitations
Currently, custom data calculations are not applicable for pivot Fields with defined GroupInterval property.