Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
When you create a pivot table based on known data, you may find that the data in it is not what you want. The desired data is a combination of the original data. For example, you need to add, subtract, multiply, and divide the original data before obtaining the results you need. At that point, you need to build a calculated field and set the corresponding formula for calculation, then perform some statistics and other operations on the calculated field.
Insert a calculated field in a PivotTable in Excel, follow these steps:


Add a calculated field to an Excel file using Aspose.Cells. Please see the following sample code. After executing the example code, a pivot table with a calculated field is added to the worksheet.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Creating a Workbook object
Workbook workbook;
// Obtaining a reference to the newly added worksheet
Worksheet ws = workbook.GetWorksheets().Get(0);
Cells cells = ws.GetCells();
// Setting the values to the cells
Cell cell = cells.Get("A1");
cell.PutValue(u"Fruit");
cell = cells.Get("B1");
cell.PutValue(u"Count");
cell = cells.Get("C1");
cell.PutValue(u"Price");
cell = cells.Get("A2");
cell.PutValue(u"Apple");
cell = cells.Get("A3");
cell.PutValue(u"Mango");
cell = cells.Get("A4");
cell.PutValue(u"Blackberry");
cell = cells.Get("A5");
cell.PutValue(u"Cherry");
cell = cells.Get("B2");
cell.PutValue(5);
cell = cells.Get("B3");
cell.PutValue(3);
cell = cells.Get("B4");
cell.PutValue(6);
cell = cells.Get("B5");
cell.PutValue(4);
cell = cells.Get("C2");
cell.PutValue(5);
cell = cells.Get("C3");
cell.PutValue(20);
cell = cells.Get("C4");
cell.PutValue(30);
cell = cells.Get("C5");
cell.PutValue(60);
// Adding a PivotTable to the worksheet
int32_t i = ws.GetPivotTables().Add(u"=A1:C5", u"D10", u"PivotTable1");
// Accessing the instance of the newly added PivotTable
PivotTable pivotTable = ws.GetPivotTables().Get(i);
pivotTable.AddFieldToArea(PivotFieldType::Row, 0);
// Adding a calculated field to the PivotTable and dragging it to the data area
pivotTable.AddCalculatedField(u"total", u"=Count*Price", true);
pivotTable.RefreshData();
pivotTable.CalculateData();
workbook.Save(u"out.xlsx");
Aspose::Cells::Cleanup();
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.