简体   繁体   中英

Date Sorting in Excel PivotTables

I have a PivotTable that has a date field in it, call it Mydate.

I have put this as a column label and then grouped the label by Years, Months and Days.

I then have put the years and days in reverse order by clicking on the label and then choosing to Sort Newest to Oldest.

If I then expand the month to show the days and then try to sort the days, the sorting is messed up....it treats the days above 10 as coming before day 2, 3, etc.

Here is what it looks like in the row label section:

    -2015  
         -Apr
             9-Apr
             8-Apr
             ...
             2-Apr
             1-Apr
             16-Apr
             15-Apr
             ...

Is there any way to fix this in the PivotTable or do I need to make an additional "Day" column to get this to work properly?

EDIT: These are properly stored as dates and the sorting works properly if I Ungroup the date field and then sort on just the dates without Years or Months.

Short: The dates in pivot table fields were sorted in text manner, not in date manner, if we sort the pivot table data field with the default sort mechanism of Excel. If there is only one digit for the day, this will not be in right order by date. Whether there is only one digit for the day, depends only on the locale settings of Windows, not on the date format in Excel.

Long: We have the short date format in locale settings of Windows to have only one digit for the day. Control Panel - Clock, Language, and Region - Region and Language :

在此处输入图片说明

This is a German Windows. In English Windows it will be m/d/yy for example.

Now we have Excel data like this.

在此处输入图片说明

Note that the Excel's date format is mm/dd/yyyy .

We create a pivot table and group that by Year, Month and Day. If we sort the days they will be sorted in text manner. This will not be in right order by date.

在此处输入图片说明

Now we change the short date format in locale settings of Windows to have two digits for the day.

在此处输入图片说明

Again, this is a German Windows. In English Windows it will be mm/dd/yyyy for example.

And we refresh the pivot table. Now it will be in right order by date. But it is always nor sorted in text manner.

在此处输入图片说明

Axel's answer works, but if you are planning on sending the worksheet to other users, you may not know if they have updated their settings.

Here is code to do this via VBA:

Private Declare Function SetLocaleInfo _
  Lib "kernel32" Alias "SetLocaleInfoA" ( _
  ByVal Locale As Long, _
  ByVal LCType As Long, _
  ByVal lpLCData As String) As Boolean

Private Declare Function GetUserDefaultLCID% Lib "kernel32" ()

Private Const LOCALE_SSHORTDATE = 31

Private Sub ChangeSettingExample()
'change the setting of the character displayed as the decimal separator.
    Call SetLocalSetting(LOCALE_SSHORTDATE, "MM/dd/yy") 'to change to proper format
    'check your control panel to verify or use the
    'GetLocaleInfo API function
End Sub

Private Function SetLocalSetting(LC_CONST As Long, Setting As String) As Boolean
    Call SetLocaleInfo(GetUserDefaultLCID(), LC_CONST, Setting)
End Function

Note: I modified the code found here: Control Windows Regional Settings by user Tom Schreiner

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM