Calculate difference between values PivotData C# examples


This documentation describes usage of DifferencePivotTable (Toolkit component).

Let's assume that you already have IPivotTable instance with some values and need to display difference between these values (on rows or columns). In same manner as for percentages it is possible to calculate differences by wrapping original pivot table model with DifferencePivotTable wrapper:

var basePvtTbl = new PivotTable(
  new[]{"product"}, new[]{"year"}, pvtData);
var diffPvtTbl = new DifferencePivotTable(
  basePvtTbl, 
  DifferencePivotTable.DifferenceMode.PreviousColumn);

It is possible to calculate a difference between rows or columns; also you can specify which value (previous or next) should be used:

  • DifferencePivotTable.DifferenceMode.PreviousColumn
  • DifferencePivotTable.DifferenceMode.NextColumn
  • DifferencePivotTable.DifferenceMode.PreviousRow
  • DifferencePivotTable.DifferenceMode.NextRow
  2015 2016 2017
Product1 15 17 9
Product2 10 8 11
Totals 25 25 20
  2015 2016 2017
Product1 2 -8
Product2 -2 3
Totals 0 -5

If pivot table has several measures it is possible to specify which measure should be calculated as the difference with DifferencePivotTable constructor that accepts measure index as a 3rd parameter:

var percentPvtTbl = new DifferencePivotTable(
  basePvtTbl, 
  DifferencePivotTable.DifferenceMode.PreviousColumn, 0);
Difference value may be shown as percentage to base value if DifferencePivotTable.Percentage property set to true.