Consolidation Function

Consolidation function

Aspose.Cells can be used to apply ConsolidationFunction to data fields (or value fields) of the pivot table. In Microsoft Excel, you can right-click the value field and then select Value Field Settings… option and then select the tab Summarize Values By. From there, you can select any ConsolidationFunction of your choice like Sum, Count, Average, Max, Min, Product, Distinct Count, etc.

Aspose.Cells provides ConsolidationFunction enumeration to support the following consolidation functions.

  • ConsolidationFunction.SUM
  • ConsolidationFunction.COUNT
  • ConsolidationFunction.AVERAGE
  • ConsolidationFunction.MAX
  • ConsolidationFunction.MIN
  • ConsolidationFunction.PRODUCT
  • ConsolidationFunction.COUNT_NUMS
  • ConsolidationFunction.STD_DEV
  • ConsolidationFunction.STD_DEVP
  • ConsolidationFunction.VAR
  • ConsolidationFunction.VARP
  • ConsolidationFunction.DISTINCT_COUNT

Applying ConsolidationFunction to Data Fields of Pivot Table

The following code applies AVERAGE consolidation function to the first data field (or value field) and STD_DEV consolidation function to the second data field (or value field).

Sample source file and output files can be downloaded from here for testing the sample code:

Source Excel File

Output Excel File

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the resource directory
// Create workbook from source excel file
Workbook workbook = new Workbook("source.xlsx");
// Access the second worksheet of the workbook
Worksheet worksheet = workbook.getWorksheets().get(1);
// Access the first pivot table of the worksheet
PivotTable pivotTable = worksheet.getPivotTables().get(0);
// Apply AVERAGE consolidation function to first data field
pivotTable.getDataFields().get(0).setFunction(ConsolidationFunction.AVERAGE);
// Apply STD_DEV consolidation function to second data field
pivotTable.getDataFields().get(1).setFunction(ConsolidationFunction.STD_DEV);
// Calculate the data to make changes affect
pivotTable.calculateData();
// Saving the Excel file
workbook.save("output.xlsx");