# Calculated Fields

## Calculated Fields Description

If your data analysis requires results that are not available using just the data source fields and RadPivotGrid's built-in calculations, you can insert a calculated field that uses a custom formula to derive the results you need. A calculated field is a new data field in which the values are the result of a custom calculation formula. You can display the calculated field along with another data field or on its own. A calculated field is really a custom summary calculation, so in almost all cases, the calculated field references one or more fields in the source data.

## Define Calculated Field

The first task is to decide what is the calculation formula that you want to use. For example, you can show the commission that will be paid to all salespeople. Commission will be paid only to those who have more sold for more than \$15 000. The price of the sold items is kept by the ExtendedPrice property from the source. So the new class will look like this:

``````public class CommissionCalculatedField : CalculatedField
{
private RequiredField extendPriceField;

public CommissionCalculatedField()
{
this.Name = "Commission";
this.extendPriceField = RequiredField.ForProperty("ExtendedPrice");
}

protected override IEnumerable<RequiredField> RequiredFields()
{
yield return this.extendPriceField;
}

protected override AggregateValue CalculateValue(IAggregateValues aggregateValues)
{
var aggregateValue = aggregateValues.GetAggregateValue(this.extendPriceField);
if (aggregateValue.IsError())
{
return aggregateValue;
}

double extendedPrice = aggregateValue.ConvertOrDefault<double>();
if (extendedPrice > 15000)
{
return new DoubleAggregateValue(extendedPrice * 0.1);
}

return null;
}
}
``````
``````Public Class CommissionCalculatedField
Inherits CalculatedField

Private extendPriceField As RequiredField

Public Sub New()
Me.Name = "Commission"
Me.extendPriceField = RequiredField.ForProperty("ExtendedPrice")
End Sub

Protected Overrides Iterator Function RequiredFields() As IEnumerable(Of RequiredField)
Yield Me.extendPriceField
End Function

Protected Overrides Function CalculateValue(ByVal aggregateValues As IAggregateValues) As AggregateValue
Dim aggregateValue = aggregateValues.GetAggregateValue(Me.extendPriceField)
If aggregateValue.IsError() Then
Return aggregateValue
End If

Dim extendedPrice As Double = aggregateValue.ConvertOrDefault(Of Double)()
If extendedPrice > 15000 Then
Return New DoubleAggregateValue(extendedPrice * 0.1)
End If

Return Nothing
End Function
``````

Now it is time to add a new instance of this class to the CalculatedFields collection of LocalDataSourceProvider:

``````<pivot:LocalDataSourceProvider.CalculatedFields>
<local:CommissionCalculatedField Name="Commission" />
</pivot:LocalDataSourceProvider.CalculatedFields>
``````
``````var calculatedField = new CommissionCalculatedField();
calculatedField.Name = "Commission";
``````
``````Dim calculatedField = New CommissionCalculatedField()
calculatedField.Name = "Commission"
``````

If you add caculated fields in code behind, you have to set the ItemsSource of LocalDataSourceProvider after you have added all calculated fields or to wrap the code between BeginInit() - EndInit() methods (or inside using DeferRefresh() { ... } section ).

This way the Commission field will be visible in a special folder inside RadPivotFieldList called Calculated Fields. In order to use the calculated field in the generated report, you have to add it to LocalDataSourceProvider's AggregateDescriptions collection:

``````<pivot:LocalDataSourceProvider.AggregateDescriptions>
<pivot:CalculatedAggregateDescription CalculatedFieldName="Commission" />
</pivot:LocalDataSourceProvider.AggregateDescriptions>
``````
``````var calculatedAggregate = new CalculatedAggregateDescription();
calculatedAggregate.CalculatedFieldName = "Commission";
``````
``````Dim calculatedAggregate = New CalculatedAggregateDescription()
calculatedAggregate.CalculatedFieldName = "Commission" 