Skip to main content

How to: Filter Items in a Pivot Table

  • 8 minutes to read

The Spreadsheet Control provides numerous ways to filter items in a pivot table. Different types of filters are available: you show or hide specific items, construct the filter expression to display item labels that meet the given criteria (Label Filters and Date Filters), or filter a field based on summary values in the data area (Value Filters).

The table below describes API members that allow you to apply a filter to a PivotTable field.

Member Description
PivotField.ShowSingleItem Displays the specified item in the PivotTable field.
PivotItem.Visible Specifies whether the current item is displayed in the field.
PivotTable.Filters Provides access to the collection of filters applied to a pivot table.
PivotFilterCollection.Add Applies a filter to the specified row or column field.
PivotFilterType Specifies the type of the filter to be applied to the PivotTable field.
PivotFilter Represents a filter applied to the PivotTable field.
PivotFilter.Field Returns the PivotTable field to which the current filter is applied.
PivotFilter.Value Returns the first filter criteria value.
PivotFilter.SecondValue Returns the second filter criteria value.
PivotFilter.MeasureField Returns the data field used by the current value filter.
PivotFilter.Top10Type Specifies the type of the “Top 10” value filter.

Select the task you wish to perform.

Show or Hide Specific Items

The following example uses a report filter to filter the entire pivot table to show sales data for the Northeastern region. Before using the report filter, make sure that the corresponding field is added to the report filter area of your PivotTable report.

Spreadsheet_PivotTables_ReportFilter

Worksheet worksheet = workbook.Worksheets["Report4"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];

// Move the "Region" field to the report filter area.
pivotTable.PageFields.Add(pivotTable.Fields["Region"]);

// Select the second (Northeast) item in the "Region" field.
pivotTable.Fields["Region"].ShowSingleItem(1);

Use a Label Filter

The following example demonstrates how to apply a label filter to the “Region” column field to display sales data only for the Southern region.

Tip

To perform more versatile filtering, you can use wildcard characters. The asterisk * matches any number of characters, while the question mark ? represents a single character. To filter labels containing a specific character, such as the asterisk, question mark or tilde, put the tilde (~) before it.

Spreadsheet_PivotTables_LabelFilter

View Example

Worksheet worksheet = workbook.Worksheets["Report4"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Access the "Region" field.
PivotField field = pivotTable.Fields[0];
// Filter the "Region" field by text to display sales data for the "South" region.  
pivotTable.Filters.Add(field, PivotFilterType.CaptionEqual, "South");

Use a Date Filter

The following example demonstrates how to apply a filter to the “Date” row field to show sales data for the second quarter.

Spreadsheet_PivotTables_DateFilter

View Example

Worksheet worksheet = workbook.Worksheets["Report6"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Access the "Date" field.
PivotField field = pivotTable.Fields[0];
// Filter the "Date" field to display sales for the second quarter.
pivotTable.Filters.Add(field, PivotFilterType.SecondQuarter);

Use a Value Filter

The following example demonstrates how to apply a value filter to the “Product” row field to display products whose total sales fall between $6000 and $13000.

Spreadsheet_PivotTables_ValueFilter

View Example

Worksheet worksheet = workbook.Worksheets["Report4"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Access the "Product" field.
PivotField field = pivotTable.Fields[1];
// Filter the "Product" field to display products with grand total sales between $6000 and $13000.
pivotTable.Filters.Add(field, pivotTable.DataFields[0], PivotFilterType.ValueBetween, 6000, 13000);

Use a Top 10 Filter

A “Top 10” value filter allows you to display the specified number of top or bottom items in a field (PivotFilterType.Count), show the top or bottom values that contribute to the specified percent of the filtered field’s grand total (PivotFilterType.Percent), or filter the top or bottom values that make up a specific sum (PivotFilterType.Sum).

The following example demonstrates how to apply the “Top 10” filter to the “Product” row field to display two products with the lowest total sales.

Spreadsheet_PivotTables_Top10Filter

View Example

Worksheet worksheet = workbook.Worksheets["Report4"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Access the "Product" field.
PivotField field = pivotTable.Fields[1];
// Filter the "Product" field to display two products with the lowest sales.
PivotFilter filter = pivotTable.Filters.Add(field, pivotTable.DataFields[0], PivotFilterType.Count, 2);
filter.Top10Type = PivotFilterTop10Type.Bottom;

Use Multiple Filters per Field

To enable the capability to apply multiple filters to a single row or column field, set the PivotBehaviorOptions.AllowMultipleFieldFilters property to true. By default, this property is false and if you try to apply more than one filter to a PivotTable field, only the last specified filter will be applied.

View Example

Worksheet worksheet = workbook.Worksheets["Report6"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];

// Allow multiple filters for a field.
pivotTable.Behavior.AllowMultipleFieldFilters = true;

// Filter the "Date" field to display sales for the second quarter.
PivotField field1 = pivotTable.Fields[0];
pivotTable.Filters.Add(field1, PivotFilterType.SecondQuarter);

// Add the second filter to the "Date" field to display two days with the lowest sales.  
PivotFilter filter = pivotTable.Filters.Add(field1, pivotTable.DataFields[0], PivotFilterType.Count, 2);
filter.Top10Type = PivotFilterTop10Type.Bottom;

Remove a Filter

Clear a specific filter

You can remove a filter applied to a PivotTable field using one of the methods listed in the table below.

Method Description
PivotFilter.Delete Removes the current filter from the collection of filters applied to a pivot table.
PivotFilterCollection.Remove Removes the specified filter from the collection of filters applied to a pivot table.
PivotFilterCollection.RemoveAt Removes the filter at the specified index from the collection of filters applied to a pivot table.
PivotField.ShowAllItems Redisplays all items in the specified PivotTable field.
// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];

// Apply a filter to the "Region" field.  
PivotFilter labelFilter = pivotTable.Filters.Add(pivotTable.Fields["Region"], PivotFilterType.CaptionEqual, "South");

// Remove the specified filter. 
labelFilter.Delete();

Clear all filters

To remove all filters applied to the row and column fields of the PivotTable report at once, use the PivotFilterCollection.Clear method.

// Clear all filters applied to the PivotTable fields.
pivotTable.Filters.Clear();