Grouping Shows Items Before and After Date Range

Grouping Shows Items Before and After Date Range

When you group a date field, Excel automatically creates creates groups for all the dates before the start date and after the end date. These items start with a “<” or a “>” symbol, such as “<2012-01-01” and “>2013-12-31”

Usually, you will only see these dates in the filter drop downs.

groupeddatesoutsiderange01

However, if you have the date field to show items with no data, those items might  appear in the headings too.

groupeddatesoutsiderange02

Hide the Out of Date Range Items

Unfortunately, there’s no setting you can change to prevent these items from appearing in the drop down filter, if you group the dates. However, you could type over the captions, and change them to space characters, so the text isn’t visible.

First, check the date field settings, to make sure that it shows items with no data. This will make the groups appear in the pivot table headings. To check:

  1. Right-click on the “Years” field heading in the pivot table, and click Field Settings
  2. On the Layout & Print tab, add a check mark to “Show Items with No Data”
  3. Click OK

Next, follow these steps to change the pivot item captions:

  • On the worksheet, select the pivot table cell with the  “<” date group heading

  • Type one space character, and press the Enter key

  • Next, select the pivot table cell with the  “>” date group heading
  • Type two space characters, and press the Enter key

Now you will just see blanks in the filter list, instead of the out of range date labels.

groupeddatesoutsiderange03

Hide the Pivot Table Headings

To remove the groups from the pivot table headings, change the date field settings, so it does not show items with no data.

    1. Right-click on the “Years” field heading in the pivot table, and click Field Settings
    2. On the Layout & Print tab, remove the check mark from “Show Items with No Data”
    3. Click OK

If you need to leave the date field set to show items with no data, you can uncheck the out of date range groups in the date field’s filter list, so they don’t appear in the pivot table.

groupeddatesoutsiderange04

Macro to Hide Out of Range Dates

Instead of manually changing the grouped date captions, you can use a macro to do the job. The following code works on the first pivot table on the active sheet, and changes the pivot items in the “Years” field.

NOTE: Test this on a backup copy of your file, to make sure it works correctly with your data.

Sub HideDatesOutOfRange()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
On Error Resume Next

Application.EnableEvents = False
Set pt = ActiveSheet.PivotTables(1)
Set pf = pt.PivotFields("Years")
pt.ManualUpdate = True

For Each pi In pf.PivotItems
   Select Case Left(pi.SourceName, 1)
      Case "<"
         pi.Visible = False
         pi.Caption = " "  '1 space
      Case ">"
         pi.Visible = False
         pi.Caption = "  " '2 spaces
   End Select
Next pi

pt.ManualUpdate = False
Application.EnableEvents = True

End Sub

___________________

3 thoughts on “Grouping Shows Items Before and After Date Range”

  1. It seems that there is now a way to prevent ‘out of date range items’ from appearing in drop down filters, pivot tables, and pivot charts. This works even when the ‘Show items with no data’ option is selected for the date field settings. In the ‘PivotTable Options’ window, on the ‘Totals & Filters’ tab, there is an option to ‘Allow multiple filters per field’. With this checked, you can deselect the start/end groups with ” in their names from the filter drop down in the ‘Field List’. Then, any further filters (from slicers, timelines, etc.) won’t see those two groups and – most importantly – won’t keep making them show again whenever another filter is changed!
    I’m on the Office 365 Monthly Channel, but this may also be available in earlier versions. I hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.