Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
pivot_field.group() method (automatic grouping by data type) and pivot_field.group(pivot_field_group_options) (custom grouping options).The parameterless pivot_field.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.
import aspose.cells as ac
# Load the sample workbook
wb = ac.Workbook("sampleGroupPivotFieldsInPivotTable.xlsx")
# Access the second worksheet and its pivot table
pt = wb.worksheets[1].pivot_tables[0]
# Group the first row field automatically.
# The field contains dates, so it is grouped by months.
field = pt.row_fields[0]
field.group()
# Calculate the pivot table data and save the output file
pt.calculate_data()
wb.save("outputAutoGroupPivotField.xlsx")
The PivotFieldGroupOptions class provides custom grouping configuration. For date fields, set start_date, end_date, and group_types. For numeric fields, set start_number, end_number, and interval. The new_pivot_field property controls whether grouping creates new pivot fields alongside the original (see the Note section at the end of this article).
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.group_types 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.group_types, yet the quarters are displayed as the outer level and the months as the inner level.
import aspose.cells as ac
from datetime import datetime
from aspose.cells.pivot import PivotGroupByType
from aspose.cells.pivot import PivotFieldGroupOptions
# Load the sample workbook
wb = ac.Workbook("sampleGroupPivotFieldsInPivotTable.xlsx")
# Access the second worksheet and its pivot table
pt = wb.worksheets[1].pivot_tables[0]
# Group the first row field (a date field) by months and quarters
options = PivotFieldGroupOptions()
options.start_date = datetime(2008, 1, 1)
options.end_date = datetime(2008, 9, 5)
options.group_types = [PivotGroupByType.MONTHS, PivotGroupByType.QUARTERS]
pt.row_fields[0].group(options)
# Calculate the pivot table data and save the output file
pt.calculate_data()
wb.save("outputGroupPivotFieldsInPivotTable.xlsx")

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 pivot_table.add_field_to_area(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 start_number=0, end_number=5 and interval=2.
import aspose.cells as ac
from aspose.cells.pivot import PivotFieldType
from aspose.cells.pivot import PivotFieldGroupOptions
# Load the sample workbook
wb = ac.Workbook("sampleGroupPivotFieldsInPivotTable.xlsx")
# Access the second worksheet and its pivot table
pt = wb.worksheets[1].pivot_tables[0]
# FieldTwo is a data field: move it to the row area first,
# because only row/column fields can be grouped
pt.add_field_to_area(PivotFieldType.ROW, "FieldTwo")
pt.calculate_data()
# Group the numeric field into the ranges 0-1, 2-3, 4-5
options = PivotFieldGroupOptions()
options.start_number = 0.0
options.end_number = 5.0
options.interval = 2.0
pt.row_fields[1].group(options)
# Calculate the pivot table data and save the output file
pt.calculate_data()
wb.save("outputNumberGroupPivotField.xlsx")
pivot_field.group_by() method overloads are deprecated and removed from the API surface. pivot_field.group() and pivot_field.group(pivot_field_group_options) are the replacements.pivot_field.group() methods follow the same behavior as Excel. The PivotFieldGroupOptions.new_pivot_field property exists for compatibility with older versions: it defaults to false for numeric grouping and true for date-time grouping; set it to false if you want date grouping to replace the original pivot field instead of creating new pivot fields.PivotFieldGroupOptions.interval property takes effect only for numeric grouping and for date grouping by days (for example, grouping dates into 7-day periods); it is ignored when dates are grouped by months, quarters or years.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.