Custom Calculations

CalculationType is an enumerator defined in the PivotComputationInfo class that is used to specify the type of calculation. The various types of calculations are:

• NoCalculation: Removes the custom calculations and restores to original values (default value). Displays the pivot values as default value.
• PercentageOfGrandTotal: Displays a value cell as a percentage of grand total of all value cells of PivotEngine.
• PercentageOfColumnTotal: Displays all value cells in each column as a percentage of its corresponding column total.
• PercentageOfRowTotal: Displays all value cells in each row as a percentage of its corresponding row total.
• PercentageOfParentColumnTotal: Displays a value cell as a percentage of parent column item values.
• PercentageOfParentRowTotal: Displays a value cell as a percentage of parent row item values.
• PercentageOfParentTotal: Displays a value cell as a percentage of base field (parent row/column total).
• Index: Displays a value cell as an index value based on PivotEngine generation.
• Formula: Displays a calculation based on a well formed algebraic expression involving other calculations.
• PercentageOf: Displays values as a percentage of the value of the base item in the base field.
• DifferenceFrom: Displays values as the difference from the value of the base item in the base field.
• PercentageOfDifferenceFrom: Displays values as the percentage difference from the value of the base item in the base field.
• RunningTotalIn: Displays the value for successive items in the base field as a running total.
• PercentageOfRunningTotalIn: Calculates the value for successive items in the base field that are displayed as a running total as a percentage.
• RankSmallestToLargest: Displays the rank of selected values in a specific field and lists the smallest item in the field as 1 and each larger value as a higher rank value.
• RankLargestToSmallest: Displays the rank of selected values in a specific field and lists the largest item in the field as 1 and each smaller value as a higher rank value.
• Distinct: Displays the subtotals based on the distinct values of BaseItem defined for the calculation item.

To achieve this, the `CalculationType` property is set for the corresponding pivot calculation item through the `PivotComputationInfo` class. It can be set through XAML or code-behind.

For XAML, refer to the following code sample.

• xaml
• ``````<Grid>
<syncfusion:PivotGridControl HorizontalAlignment="Left" Name="pivotGrid" VerticalAlignment="Top" VisualStyle="Metro" ItemSource="{Binding   Source={StaticResource data}}">

<syncfusion:PivotGridControl.PivotRows>
</syncfusion:PivotGridControl.PivotRows>
<syncfusion:PivotGridControl.PivotColumns>
</syncfusion:PivotGridControl.PivotColumns>
<syncfusion:PivotGridControl.PivotCalculations>
<syncfusion:PivotComputationInfo CalculationName="Total" FieldName="Amount" Format="C" SummaryType="DoubleTotalSum" CalculationType="PercentageOfColumnTotal" />
<syncfusion:PivotComputationInfo CalculationName="Total" FieldName="Quantity" SummaryType="Count" />
</syncfusion:PivotGridControl.PivotCalculations>
</Grid>``````

For code-behind, refer to the following code sample.

• c#
• ``````public partial class MainWindow: Window {
PivotGridControl pivotGrid = new PivotGridControl();
public MainWindow() {
InitializeComponent();
pivotGrid.ItemSource = ProductSales.GetSalesData();
PivotItem m_PivotItem = new PivotItem() {
};
PivotItem m_PivotItem1 = new PivotItem() {
};
PivotItem n_PivotItem = new PivotItem() {
};
PivotItem n_PivotItem1 = new PivotItem() {
};
PivotComputationInfo m_PivotComputationInfo = new PivotComputationInfo() {
CalculationName = "Amount", FieldName = "Amount", Format = "C", SummaryType = SummaryType.DoubleTotalSum, CalculationType = CalculationType.PercentageOfColumnTotal
};
PivotComputationInfo m_PivotComputationInfo1 = new PivotComputationInfo() {
CalculationName = "Quantity", FieldName = "Quantity", SummaryType = SummaryType.Count
};
}
}``````

Providing expression field calculation for summaries

To provide a calculated field support to summary cells in the pivot grid and make it behave accordingly when options such as sum, count, maximum, and minimum are provided in summaries, set the calculation type to “Formula” and specify the appropriate formula. Refer to the following code samples and screenshots.

• xaml
• ``````<Grid>
<syncfusion:PivotGridControl HorizontalAlignment="Left" Name="pivotGrid" VerticalAlignment="Top" VisualStyle="Metro" ItemSource="{Binding   Source={StaticResource data}}">

<syncfusion:PivotGridControl.PivotRows>
</syncfusion:PivotGridControl.PivotRows>
<syncfusion:PivotGridControl.PivotColumns>
</syncfusion:PivotGridControl.PivotColumns>
<syncfusion:PivotGridControl.PivotCalculations>
<syncfusion:PivotComputationInfo CalculationName="Total" FieldName="Amount" Format="C" SummaryType="DoubleTotalSum" />
<syncfusion:PivotComputationInfo CalculationName="Total" FieldName="Quantity" SummaryType="Count" />
<syncfusion:PivotComputationInfo CalculationName="Total" FieldName="UnitPrice" CalculationType="Formula" Formula="[Amount] / [Quantity]" />
</syncfusion:PivotGridControl.PivotCalculations>
</syncfusion:PivotGridControl>
</Grid>``````
• c#
• ``````public partial class MainWindow: Window {
PivotGridControl pivotGrid = new PivotGridControl();
public MainWindow() {
InitializeComponent();
pivotGrid.ItemSource = ProductSales.GetSalesData();
PivotItem m_PivotItem = new PivotItem() {
};
PivotItem m_PivotItem1 = new PivotItem() {
};
PivotItem n_PivotItem = new PivotItem() {
};
PivotItem n_PivotItem1 = new PivotItem() {
};
PivotComputationInfo m_PivotComputationInfo = new PivotComputationInfo() {
CalculationName = "Amount", FieldName = "Amount", Format = "C", SummaryType = SummaryType.DoubleTotalSum
};
PivotComputationInfo m_PivotComputationInfo1 = new PivotComputationInfo() {
CalculationName = "Quantity", FieldName = "Quantity", SummaryType = SummaryType.Count
};