# Bind Pivot Grid Fields to Calculated Expressions | WPF Controls | DevExpress Documentation

Pivot Grid uses the [Binding API](/CoreLibraries/401533/devexpress-pivot-grid-core-library/data-binding-api) to bind the Pivot Grid’s fields to data. Data binding sources can be columns in a data source, calculated expressions, or window calculations.  

Calculated fields display the result of calculated expressions. Each calculated field has a binding expression that can be a formula or aggregate function. In addition to the ability to retrieve values from a field in the data source, the expression allows you to specify how data is calculated (for example, aggregate it).

Expressions are computed at the data source level. This means that if you specify a field in the expression, Pivot Grid uses the field values from the data source. Use the [PivotGridField.Name](/WPF/DevExpress.Xpf.PivotGrid.PivotGridField.Name) property to specify the field.

If you want to use aggregated field values, wrap a field in the corresponding aggregate function. The [PivotGridField.SummaryType](/WPF/DevExpress.Xpf.PivotGrid.PivotGridField.SummaryType) and [PivotGridField.AllowRuntimeSummaryChange](/WPF/DevExpress.Xpf.PivotGrid.PivotGridField.AllowRuntimeSummaryChange) properties are not in effect for the field that is bound to such expression.

For example, the following expression is calculated based on underlying values of the Unit Price and Quantity fields:

```
[Unit Price] * [Quantity]
```

If you want to calculate the average sales, use the following expression:

```
Avg([Unit Price]) * Avg([Quantity])
```

## Create a Calculated Field

Important

You cannot bind the Pivot Grid to data at design time in .NET 5+ projects.

Optimized mode supports [ExpressionDataBinding](/WPF/DevExpress.Xpf.PivotGrid.ExpressionDataBinding). 

Follow the steps below to create a calculated field:

1. Create an [ExpressionDataBinding](/WPF/DevExpress.Xpf.PivotGrid.ExpressionDataBinding) instance and pass the expression to its constructor as a parameter.
2. Assign the created object to the [PivotGridField.DataBinding](/WPF/DevExpress.Xpf.PivotGrid.PivotGridField.DataBinding) property.

[Run Demo](dxdemo://Wpf/DXPivotGrid/MainDemo/CalculatedFields) 

- CalculatedFields.xaml

<section id="tabpanel_lwHOsHmNB3_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code class="lang-xaml">&lt;dxpg:PivotGridControl.Fields&gt;
    &lt;dxpg:PivotGridField x:Name=&quot;fieldOrderCountBonus&quot;
                            DataBinding=&quot;{dxpg:ExpressionDataBinding &#39;Iif(Count() &amp;gt; 50 and Sum([fieldExtendedPrice]) / Count() &amp;gt; 500, 
                                15.0 * Sum([fieldExtendedPrice]) / Sum([fieldQuantity]) , 0)&#39;}&quot;
                            Area=&quot;DataArea&quot;
                            AreaIndex=&quot;4&quot;
                            AllowedAreas=&quot;DataArea&quot;
                            Caption=&quot;Order Count Bonus&quot;
                            CellFormat=&quot;c2&quot;
                            ValueTemplate=&quot;{StaticResource ResourceKey=UnboundFieldTemplate}&quot; /&gt;
&lt;/dxpg:PivotGridControl.Fields&gt;
</code></pre></section>

## Use Custom Functions in Expression

You can create functions with custom logic to build an expression that executes complex calculations for a Pivot Grid field.

Refer to the following article for more information about custom functions: [Custom Aggregate Functions](/CoreLibraries/403494/devexpress-pivot-grid-core-library/advanced-analytics/custom-aggregate-functions).

The example below shows how create a `Count Distinct` field and bind it to an expression that contains a custom function. The expression counts distinct values (the number of orders with equal product quantities).

![Pivot Grid Custom Summary](/WPF/images/custom-count-distinct-summary.png)

[View Example: How to Create a Custom Summary to Display the Distinct Value Count](https://github.com/DevExpress-Examples/wpf-pivot-grid-implement-custom-summary)

- MainView.xaml

<section id="tabpanel_W9XEnJxRhe_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code class="lang-xaml">&lt;dx:ThemedWindow x:Class=&quot;DXPivotGrid_CustomSummary.MainWindow&quot; mc:Ignorable=&quot;d&quot; Title=&quot;MainWindow&quot; Height=&quot;450&quot; Width=&quot;800&quot; 
    xmlns:dx=&quot;http://schemas.devexpress.com/winfx/2008/xaml/core&quot; 
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; 
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; 
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
    xmlns:local=&quot;clr-namespace:DXPivotGrid_CustomSummary&quot; 
    xmlns:dxpg=&quot;http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid&quot;&gt;
    &lt;Grid&gt;

        &lt;dxpg:PivotGridControl Loaded=&quot;PivotGridControl1_Loaded&quot; Name=&quot;pivotGridControl1&quot; 
            DataProcessingEngine=&quot;Optimized&quot;&gt;
            &lt;dxpg:PivotGridControl.Fields&gt;
            &lt;!-- ... --&gt;
                &lt;dxpg:PivotGridField Name=&quot;fieldQuantityDistinctCount&quot;  Area=&quot;DataArea&quot; AreaIndex=&quot;1&quot; 
                     Caption=&quot;Count Distinct&quot;&gt;
                    &lt;dxpg:PivotGridField.DataBinding&gt;
                        &lt;dxpg:ExpressionDataBinding Expression=&quot;DistinctCount([OrderID])&quot;/&gt;
                    &lt;/dxpg:PivotGridField.DataBinding&gt;
                &lt;/dxpg:PivotGridField&gt;
            &lt;/dxpg:PivotGridControl.Fields&gt;
        &lt;/dxpg:PivotGridControl&gt;

    &lt;/Grid&gt;
&lt;/dx:ThemedWindow&gt;
</code></pre></section>

## Enable Users to Edit Expressions

You can allow users to employ the [Expression Editor](/WPF/7554/common-concepts/expressions/expression-editor) to change specific calculated expressions. To do this, set the [PivotGridField.AllowExpressionEditor](/WPF/DevExpress.Xpf.PivotGrid.PivotGridField.AllowExpressionEditor) property to `true`. The **Expression Editor…** command appears in this field’s context menu as a result.

![wpf-pivot-express-field](/WPF/images/wpf-pivot-express-field.png)

You can also call the [PivotGridControl.ShowExpressionEditor](/WPF/DevExpress.Xpf.PivotGrid.PivotGridControl.ShowExpressionEditor.overloads) method to invoke the Expression Editor.

See Also

[ExpressionDataBinding](/WPF/DevExpress.Xpf.PivotGrid.ExpressionDataBinding)

[OlapExpressionBinding](/WPF/DevExpress.Xpf.PivotGrid.OlapExpressionBinding)