Group Pivot Fields in the Pivot Table

Possible Usage Scenarios

  • Grouping pivot fields is useful when a row or column field of a pivot table contains many distinct items, such as a date field with hundreds of dates or a numeric field with many scattered values. Grouping collapses these items into meaningful sections: dates into months, quarters and years, and numbers into ranges such as 0-1, 2-3, 4-5. This makes a large pivot table compact and easier to analyze. Aspose.Cells provides this capability through the PivotField.Group() method (automatic grouping by data type) and PivotField.Group(PivotFieldGroupOptions) (custom grouping options).
  • Date grouping rolls the individual dates up into larger periods, such as days, months, quarters and years, and builds a time hierarchy (for example Years, Quarters, Months). Trends over time become visible at a glance, and each period level becomes its own pivot field in the row or column area of the pivot table.
  • Grouping is available only for fields placed in the ROW or COLUMN area of a pivot table. A data (value) field cannot be grouped directly. To group such a field you must first add it to the row or column area of the pivot table.

Group Pivot Fields Automatically

The parameterless PivotField.Group() overload inspects the data type of the field and groups it automatically. A field that contains only dates (and blanks) is grouped by months. A field that contains only numbers (and blanks) is grouped into numeric ranges using an automatic interval.

#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
    Aspose::Cells::Startup();
    Workbook wb(u"sampleGroupPivotFieldsInPivotTable.xlsx");
    Worksheet ws = wb.GetWorksheets().Get(1);
    PivotTable pt = ws.GetPivotTables().Get(0);
    PivotField field = pt.GetRowFields().Get(0);
    field.Group();
    pt.CalculateData();
    wb.Save(u"outputAutoGroupPivotField.xlsx");
    Aspose::Cells::Cleanup();
    return 0;
}

Group Pivot Fields with Custom Options

The PivotFieldGroupOptions class provides custom grouping configuration. For date fields, set StartDate, EndDate, and GroupTypes. For numeric fields, set StartNumber, EndNumber, and Interval. The NewPivotField property controls whether grouping creates new pivot fields alongside the original (see the Note section at the end of this article).

Group a Date Field by Months and Quarters

The following example groups the date row field of the sample pivot table by months and quarters between a start and end date. The code loads the sample Excel file, performs the grouping, calculates the pivot table data, and saves the grouped result. The screenshot shows the effect: the date row field is now grouped by quarters and months. Note that the order of the values in options.GroupTypes is ignored. No matter in which order the group types are listed, the grouped fields are always displayed from the largest period to the smallest: years, quarters, months, days, hours, minutes, seconds. In this example the months are listed before the quarters in options.GroupTypes, yet the quarters are displayed as the outer level and the months as the inner level.

#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
    Aspose::Cells::Startup();
    // Load the sample workbook
    Workbook wb(u"sampleGroupPivotFieldsInPivotTable.xlsx");
    // Access the second worksheet and its pivot table
    Worksheet ws = wb.GetWorksheets().Get(1);
    PivotTable pt = ws.GetPivotTables().Get(0);
    // Group the first row field (a date field) by months and quarters
    PivotFieldGroupOptions options;
    Date startDate{2008, 1, 1, 0, 0, 0, 0};
    Date endDate{2008, 9, 5, 0, 0, 0, 0};
    options.SetStartDate(startDate);
    options.SetEndDate(endDate);
    // Set group types: Months and Quarters
    Vector<PivotGroupByType> groupTypes{ PivotGroupByType::Months, PivotGroupByType::Quarters };
    options.SetGroupTypes(groupTypes);
    // Apply grouping to the first row field
    PivotField rowField = pt.GetRowFields().Get(0);
    rowField.Group(options);
    // Calculate the pivot table data and save the output file
    pt.CalculateData();
    wb.Save(u"outputGroupPivotFieldsInPivotTable.xlsx");
    Aspose::Cells::Cleanup();
    return 0;
}

Pivot table grouped by months and quarters

Group a Numeric Field into Ranges

FieldTwo of the sample pivot table is initially a data (value) field. Because only row or column fields can be grouped, the code first calls PivotTable.AddFieldToArea(PivotFieldType.Row, "FieldTwo") to move FieldTwo to the row area, refreshes the pivot, then groups the numeric field into the ranges 0-1, 2-3 and 4-5 using StartNumber = 0, EndNumber = 5 and Interval = 2.

#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
    Aspose::Cells::Startup();
    Workbook wb(u"sampleGroupPivotFieldsInPivotTable.xlsx");
    Worksheet ws = wb.GetWorksheets().Get(1);
    PivotTable pt = ws.GetPivotTables().Get(0);
    pt.AddFieldToArea(PivotFieldType::Row, U16String("FieldTwo"));
    pt.CalculateData();
    PivotFieldGroupOptions options;
    options.SetStartNumber(0);
    options.SetEndNumber(5);
    options.SetInterval(2);
    pt.GetRowFields().Get(1).Group(options);
    pt.CalculateData();
    wb.Save(u"outputNumberGroupPivotField.xlsx");
    Aspose::Cells::Cleanup();
    return 0;
}

Note