Written by Allen Wyatt (last updated June 22, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
David has many PivotTables that are filtered by year. In his data there is a separate field for year (values 2016, 2017, 2018, etc., derived via the Year function). He excludes older years in most cases and reports on more recent years. When a new year arrives, it is a tedious process for David to edit every PivotTable and select the most recent year. He wonders if there is a way to specify the selection values for a PivotTable filter so he doesn't need to go through the tedious editing.
Perhaps the easiest way to do this would be to add a single column to the source data for your PivotTable. The column could contain a simple formula that designates whether the row is within the desired range for inclusion in the PivotTable. For instance, if column A contains the transaction date for the row, then you could include the following in the added column:
=YEAR(A2)>YEAR(NOW())-3
The result of the formula is either True or False, depending on whether the transaction is within the previous three years or not. Thus, if this formula is evaluated in 2018, then any transactions within 2016, 2017, and 2018 would return True; all others would be False. Then, within your PivotTable definition you could filter based on the contents of this particular column, thereby ensuring that only those True rows are included in the PivotTable.
If you prefer a macro-based solution, you could easily develop one that examined each of the PivotTables and changed the PivotField named "Year" so that it was equal to a desired year. The following shows how easy it is to make such a change:
Sub ChangePivotYear() Dim sht As Worksheet Dim pvt As PivotTable Dim iYear as Integer iYear = 2018 ' Change to desired year For Each sht In Worksheets For Each pvt In sht.PivotTables pvt.PivotFields("Year").ClearAllFilters pvt.PivotFields("Year").CurrentPage = iYear Next pvt Next sht End Sub
The macro sets the field to 2018; if you want to use a different year, just change what is assigned to the Year variable. Note, as well, that the macro affects all the PivotTables in the entire workbook.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12571) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Are you attached to the classic PivotTable layout? Looking for a way to make that layout the default for new PivotTables? ...
Discover MoreOne of the ways you can use PivotTables is to generate counts of various items in a data table. This is a great technique ...
Discover MoreWhen you create a PivotTable, it can have a name. You may want this name to appear within the PivotTable itself. There is ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-02-19 06:03:19
David Robinson
Another easy, but non-dynamic, method, is if the year is a row or column field (rather than a report filter, which oddly lacks the functionality), you can use the row/column labels filter with a "greater than" condition, and enter the most recent year to exclude. New years will appear automatically because they're greater than the cut-off. Of course the cut-off is controlled manually so when you want to knock out an old year you would change the label filter.
So I'd use the simple formula in the main post if the report always needs to show N years, but the "greater than" label filter I mentioned above if you consciously decide when to remove an old year.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments