Filtering Pivot Tables by Label or Value

Introduction

Pivot tables are powerful analytical tools, but raw summaries often contain far more information than you need to present. Filtering is the primary mechanism for narrowing a pivot table down to the rows, columns, or values that matter for a specific report. Aspose.Cells for Java mirrors the filtering capabilities that are available in Microsoft Excel, exposing them programmatically so that report generation can be fully automated.

The following filtering strategies are covered in this article:

  1. Label Filter — filters row or column field items based on their text labels.
  2. Date Filter — filters row or column fields that contain only date-time values (or blanks).
  3. Value Filter — filters items based on the aggregated values of a data field.
  4. Top 10 Filter — shows only the top or bottom N items ranked by a value field.
  5. Hide / Unhide Pivot Items — manually controls the visibility of each individual item in a field.

Each approach uses a different method on the PivotField class or a property on the PivotItem class. After applying any filter, you must call refreshData() and calculateData() on the pivot table so that the cached data and calculated values reflect the new filter state.

Label Filter

A label filter allows you to filter the items of a row or column field by comparing their text captions against a pattern. This is useful when you want to display only products whose names start with a specific letter, contain a particular word, or match some other caption-based criterion.

Aspose.Cells exposes label filtering through the PivotField.filterByLabel(PivotFilterType, String) method. The PivotFilterType enumeration includes values such as CaptionBeginsWith, CaptionContains, CaptionEndsWith, CaptionDoesNotContain, CaptionIsNotBlank, CaptionIsBlank, and so on. The second argument supplies the label string used for comparison.

The following example loads a workbook containing an existing pivot table, applies a label filter so that only items whose captions begin with a specified prefix remain visible, refreshes the pivot table, and saves the result.

import com.aspose.cells.*;

String fileName = "sample.xlsx";
String prefix = "B";

// Load the existing workbook containing a pivot table
Workbook workbook = new Workbook(fileName);

// Access the worksheet by index (first worksheet)
Worksheet worksheet = workbook.getWorksheets().get(0);

// Access the pivot table by index
PivotTable pivotTable = worksheet.getPivotTables().get(0);

// Retrieve the first row PivotField
PivotField rowField = pivotTable.getRowFields().get(0);

// Apply the label filter - show only row items whose labels begin with the supplied prefix
rowField.filterByLabel(PivotFilterType.CAPTION_BEGINS_WITH, prefix, "");

// Refresh and recalculate the pivot table data so the filter takes effect
pivotTable.refreshData();

// Save the workbook back to disk
workbook.save(fileName);

Date Filter

Date filters let you narrow a pivot table by date-based criteria such as today, last week, this month, next quarter, or a specific date range. They are specialized filters that work only against fields that store date-time information.

Aspose.Cells exposes date filtering through the PivotField.filterByDate(PivotFilterType, params DateTime[] values) method. The PivotFilterType enumeration contains dedicated date values such as Today, Yesterday, LastWeek, ThisWeek, NextWeek, LastMonth, ThisMonth, NextMonth, LastQuarter, ThisQuarter, NextQuarter, LastYear, ThisYear, NextYear, and Between. Depending on the chosen filter type, you pass one or two DateTime values (for Between, you pass the start and end dates).

The following example loads a workbook with a pivot table whose row area contains a date field, applies a date filter that restricts the visible items to a particular date range, refreshes the pivot table, and saves the workbook.

import java.io.File;
import java.io.FileNotFoundException;

String inputPath = "sample.xlsx";
String outputPath = "output_filtered.xlsx";

if (!new File(inputPath).exists())
{
    throw new FileNotFoundException("Source workbook not found: " + inputPath);
}

// Load the existing workbook that contains the pivot table
Workbook workbook = new Workbook(inputPath);

// Access the worksheet that holds the pivot table (by index)
Worksheet worksheet = workbook.getWorksheets().get(0);

// Access the pivot table by index
PivotTable pivotTable = worksheet.getPivotTables().get(0);

// Retrieve the date PivotField from the row area
// (Date filter only works when the row/column area contains only date-time cells or blanks)
PivotField dateField = pivotTable.getRowFields().get(0);

// Define the date criterion for the Between filter
DateTime startDate = new DateTime(2020, 1, 1);
DateTime endDate = new DateTime(2020, 12, 31);

// Apply the date filter on the pivot field
dateField.filterByDate(PivotFilterType.DATE_BETWEEN, startDate, endDate);

// Refresh and recalculate the pivot table so the filter takes effect
pivotTable.refreshData();

// Persist the workbook
workbook.save(outputPath);

Value Filter

Value filters operate on the aggregated values that a pivot table calculates in its data area. Instead of matching text labels, they compare numeric totals against a threshold. Typical use cases include showing only products whose sum of sales exceeds a target amount or only regions whose count of transactions falls within a range.

Aspose.Cells exposes value filtering through the PivotField.filterByValue(PivotField valueField, PivotFilterType filterType, params Object[] values) method. The filterType parameter uses values such as ValueGreaterThan, ValueLessThan, ValueBetween, ValueEqual, ValueNotEqual, ValueGreaterThanOrEqual, and ValueLessThanOrEqual. The valueField parameter specifies which data field should be evaluated, and the final argument(s) supply the threshold value(s).

The following example loads a workbook with a pivot table, applies a value filter that keeps only items whose aggregated sales exceed a numeric threshold, refreshes the pivot table, and saves the workbook.

import com.aspose.cells.*;

Workbook workbook = new Workbook("sample.xlsx");
Worksheet worksheet = workbook.getWorksheets().get(0);
PivotTable pivotTable = worksheet.getPivotTables().get(0);

PivotField rowField = pivotTable.getRowFields().get(0);
PivotField dataField = pivotTable.getDataFields().get(0);

// Find the data field index manually since PivotFieldCollection doesn't have IndexOf
int dataFieldIndex = -1;
for (int i = 0; i < pivotTable.getDataFields().getCount(); i++)
{
    if (pivotTable.getDataFields().get(i) == dataField)
    {
        dataFieldIndex = i;
        break;
    }
}

if (dataFieldIndex >= 0)
{
    rowField.filterByValue(dataFieldIndex, PivotFilterType.VALUE_GREATER_THAN, 5000, Double.MAX_VALUE);
}

pivotTable.refreshData();

workbook.save("output.xlsx");

Top 10 Filter

The top 10 filter is a specialized form of value filter that retains only the highest or lowest N items based on a chosen value field. It is commonly used for ranking reports such as “top 10 products by revenue” or “bottom 5 regions by sales count”.

Aspose.Cells exposes top 10 filtering through the PivotField.filterTop10(int itemCount, boolean isTop, PivotField valueField, PivotFilterType filterType) method. The itemCount parameter defines how many items to retain, isTop indicates whether to keep the top items (true) or the bottom items (false), valueField references the data field used for ranking, and filterType controls how the value is computed (typically Sum, but also Count and Percent).

The following example loads a workbook with a pivot table that contains a value field, applies a top 10 filter to keep only the highest 10 items by the sum of sales, refreshes the pivot table, and saves the workbook.

import com.aspose.cells.*;

// Load the existing workbook that contains the pivot table
String inputPath = "input.xlsx";
String outputPath = "output.xlsx";
Workbook workbook = new Workbook(inputPath);

// Access the worksheet that holds the pivot table (index 0)
Worksheet worksheet = workbook.getWorksheets().get(0);

// Access the pivot table by index
PivotTable pivotTable = worksheet.getPivotTables().get(0);

// Confirm there is at least one value PivotField in the data area
if (pivotTable.getDataFields().getCount() == 0)
{
    throw new RuntimeException("Pivot table has no value (data) PivotField.");
}
PivotField valueField = pivotTable.getDataFields().get(0);

// Retrieve the target row PivotField (the field we want to apply Top 10 on)
PivotField rowField = pivotTable.getRowFields().get(0);

// The first (and only) data field is at index 0; Top 10 ranks by it.
int valueFieldIndex = 0;

// Apply the Top 10 filter on the row field:
//   - itemCount   = 10
//   - filterType  = PivotFilterType.SUM
//   - isTop       = true (top N; false would mean bottom N)
//   - valueFieldIndex = the index of the data field used to rank items
rowField.filterTop10(10, PivotFilterType.SUM, true, valueFieldIndex);

// Refresh the pivot table data and recalculate it so the filter takes effect
pivotTable.refreshData();

// Save the workbook
workbook.save(outputPath);

Filter by Hiding or Unhiding Pivot Items

In addition to the structured filter APIs, Aspose.Cells allows you to control the visibility of each individual pivot item directly. By iterating through the PivotItems collection of a PivotField and toggling the IsHidden property, you can selectively suppress specific items without applying a formula-based filter. Setting IsHidden = true hides the item from the pivot table; setting IsHidden = false unhides it and makes it visible again.

This approach is useful when the filtering rule is irregular or item-specific, such as hiding a small number of named categories that should not appear in a particular report. The example below loads a pivot table, hides a specific item by name, demonstrates how to unhide it, refreshes the pivot table, and saves the workbook.

import com.aspose.cells.*;

// Load an existing workbook containing a pivot table
Workbook workbook = new Workbook("pivot_table_sample.xlsx");

// Access the first worksheet which contains the pivot table
Worksheet sheet = workbook.getWorksheets().get(0);

// Access the pivot table by index (the first pivot table on the sheet)
PivotTable pivotTable = sheet.getPivotTables().get(0);

// Retrieve the target PivotField (the first row label field that we'll hide/unhide items in)
PivotField pivotField = pivotTable.getRowFields().get(0);

// Iterate through the PivotItems collection of the selected PivotField
int itemCount = pivotField.getPivotItems().getCount();
for (int i = 0; i < itemCount; i++)
{
    PivotItem item = pivotField.getPivotItems().get(i);

    // Hide pivot items that match a specific name/criterion
    if (item.getName() == "Item1" || item.getName() == "Item2")
    {
        item.setHidden(true);
    }

    // Demonstrate unhiding: re-show a previously hidden pivot item
    if (item.getName() == "Item3")
    {
        item.setHidden(false);
    }
}

// Refresh and recalculate the pivot table so changes take effect
pivotTable.refreshData();

// Save the workbook - hidden items stay in the underlying data
// but are excluded from the displayed pivot table output
workbook.save("output_pivot_filtered.xlsx");

Summary

Aspose.Cells for Java provides a complete set of pivot table filtering capabilities that match those found in Microsoft Excel. Label, date, and value filters cover the most common analytical scenarios, while the top 10 filter handles ranking reports. When the filtering rule is irregular, the PivotItem.IsHidden property offers a flexible, item-level fallback. Combining these strategies — for example, applying a label filter and then hiding specific items — allows you to build precisely targeted pivot table reports entirely from code.