How to Sort a Pivot Table (6 Easy Ways)

Here’s an unsorted pivot table with the annual salaries of each department for different designations and the same pivot table after sorting alphabetically.

Overview Image


Pivot Table Sorting Restrictions

  • If no pivot fields are present to the left of the field you are sorting, all the pivot items will be sorted together in the order you selected.
  • If fields are present to the left of the field you are sorting, the pivot items will be sorted within each item of the next field to the left.

6 Ways to Sort a Pivot Table in Excel

We will use the dataset shown below.

Dataset

To create a pivot table from this dataset:

  • Select any cell in the data range.
  • Go to the Insert tab, select PivotTable, and choose From Table/Range.
  • In the PivotTable from the table or range box, choose where to put the pivot table in the New worksheet or Existing worksheet and press OK.
  • In the PivotTable Fields pane on the right, drag the fields in the Rows, Columns, and Values fields. 

Method 1 – Sorting a PivotTable by Label Data

We want to sort the row labels in the pivot table in alphabetic order.

Unsorted pivot table

  • Select a cell in the column that is to be sorted by row labels.
  • Go to the Data tab and click Sort.
  • Choose between Manual, Ascending, or Descending options from the Sort box and press OK.

Sorting by Labels

  • We chose the Descending sorting option. You can also sort by values or auto-sort by clicking More Options.

More Sorting Options

  • The pivot table will be sorted in descending order.

Sorted by Labels

Method 2 – Sorting by Values

  • Select a cell with a value in the pivot table.
  • Go to the Sort tab and select Sort from the Sort & Filter group.
  • In the Sort By Value box:
    • Choose the sorting option, either Smallest to Largest or Largest to Smallest.
    • Choose the sorting direction Top to Bottom or Left to Right.
    • Press OK.

choose sorting option and direction

As we can sort pivot tables by values in two different directions, we will sort them vertically and horizontally by the Grand Total values.

Case 2.1 – Sorting a Pivot Table by Grand Total Horizontally

  • Select a cell with a Grand Total value horizontally in the pivot table.
  • Go to Data and select Sort.
  • In the Sort By Value box:
    • Choose Smallest to Largest as sorting option.
    • Choose Left to Right as sorting direction.
    • Press OK.

Sorting Grand Total Values Horizontally

  • The Grand Total values in the row will be sorted from smallest to largest.

Grand Total Values Sorted Horizontally

Case 2.2 – Sorting a Pivot Table Vertically

  • Select a cell from the Grand Total column and choose Top to Bottom as sorting directions.

Sorting Grand Total Values Vertically

Sorting the grand total in the vertical and horizontal directions can be done concurrently.

Grand Total Values Sorted Vertically

Method 3 – Sorting a Pivot Table with Custom Options

We have a pivot table with employee names and their annual salaries shown in the image below.

Pivot table with employee name and annual salary

  • Click on the filter arrow for Row Labels.
  • Select More Sort Options.Choosing more sort option
  • In Sort box, choose Manual, Ascending, or Descending. We chose Descending option.
  • Press More Options to see more sorting options.
    Sort options
  • In the More Sort Options box, check AutoSort and press OK.
    Viewing More Sort Options

AutoSort will automatically sort every time the report is updated. If you uncheck this option, the First key sort order option will be available, and you can choose a custom order to sort. In general, you can see the weekdays and months in a year as a custom list. However, you can also create your own custom sort list and use that list from here. Updating (refreshing) the data in your PivotTable results in the loss of the sorting order.

Custom sort orders

  • In the Sort By feature, select either Grand Total or Values in specific columns to arrange the data based on these values. This functionality is disabled when the sorting is set to Manual.

Method 4 – Sorting a Pivot Table with Multiple Category Fields

We have a pivot table that shows annual salaries for different departments.

pivot table with multiple category fields

While sorting by row labels, this pivot table will sort by department. Let’s sort by employee designations:

  • Click on the sort and filter sign for Row Labels.
  • Click on the drop-down for Select Field.
  • Choose the category field or row label to sort. We chose Designation.

Choosing Category to Sort from Multiple Category

  • Select the sorting option. We selected the “Sort Z to A” option.
  • Press OK.
    Choosing Sorting Option

After sorting, the designations of each department will be sorted in a descending order.

Sorted in Descending Manner

Method 5 – Sorting a Pivot Table by Dates

We have a pivot table containing the employee names, their hiring dates, and annual salaries.

Pivot table with dates

  • Click on the sort and filter sign for Row Labels.
  • Select the Sort Newest to Oldest sort option.
  • Press OK.

Sorting dates from newest to oldest

  • The dates are sorted from newest to oldest in the image below.

Dates sorted from newest to oldest

Method 6 – Sort Pivot Fields with a Macro

We will sort the Grand Total column of the pivot table in ascending order using VBA.

Pivot table to sort with VBA

  • Go to the Developer tab and select Visual Basic.
    Selecting Visual Basic
  • Go to Insert and select Module in the Microsoft Visual Basic for Applications window.
    Selecting Module from Insert Tab
  • Insert the following VBA code in the module.
    Sub SortUsingVBA()
        ActiveSheet.PivotTables("PivotTable4").PivotFields("Designation").AutoSort _
            xlAscending, "Sum of Annual Salary", ActiveSheet.PivotTables("PivotTable4"). _
            PivotColumnAxis.PivotLines(5), 1
    End Sub
  • Press Ctrl + S to save the file as a macro-enabled file.
  • Click the No button in the Microsoft Excel dialog box.
    Clicking NO in the warning message
  • In the Save As dialog box, choose the Save as type option as .xlsm type and click the Save
  • Go back to the sheet and select a cell in the column to sort.
  • Select Macros from the Code group in the Developer tab.
    Select a cell and open Macro from Developer tab
  • Select the SortUsingVBA macro and click on the Run button in the Macro box.
    Select Macro and press run
  • You can see the pivot table is sorted by values in ascending order.

Pivot table sorted using VBA


What to Do If Pivot Table Sorting Is Not Working in Excel?

Case 1 – Pivot Table Sort by Value Not Working

The image below shows that the grand total of annual salaries for each designation column is sorted in ascending order. But the Finance column is not sorted in ascending order.

Sorting by values not working

Reason:  The pivot table makes groups of different parts of data. When the pivot table is sorted based on grand totals, all the designations are sorted based on that value. However, the finance department’s column can not be sorted similarly as it will disrupt the existing order. The pivot table gets filtered based on the value of the right-most column. Also while sorting it does not take any other column on the left of that in considertaion.

Solution: Decide which column value you want to consider for sorting your data. Put that column as the rightmost column of your pivot table.

Case 2 – Pivot Table Months Not in Order

The pivot table might sort the months alphabetically, like in the image below.

months not in order

Reason: Pivot table sorts months with a default custom sort list of months. If this custom sorting is disabled, then months get sorted alphabetically.

Use Custom List while sorting option is disabled

Solution:

  • Right-click on any cell in the pivot table.
  • Choose PivotTable Options from the context menu.
    Choosing Pivot Table options
  • In the PivotTable Options box, go to the Totals & Filters section.
  • Under Sorting, check Use Custom List when Sorting.
  • Press OK.
    Enable Use Custom List while sorting option

You will see the months automatically sorted in custom order instead of alphabetically sorting.

Months Sorted in Custom Order

Case 3 – New Pivot Table Items Out of Sort Order

We have a sorted dataset showing annual salaries for different designations of each department.

Sorted List

We added a new department named “IT” with “Analyst” designation. After refreshing the pivot table, the “IT” department was added at the bottom.

Newly added item in sorted list

Solution:

  • Click on the Row Labels sort and filter sign.
  • Choose the Sort A to Z option and press OK.
    Sorting the list selecting department field

You will find the list sorted in the proper order.

New added item in sorted order


Frequently Asked Questions

Why Does Excel Not Allow Sorting?

Excel can sort most of the data sourced externally. However, Excel can not sort data sourced externally from an SQL table or some other sources. In this case, you have to copy and paste the data as value in your spreadsheet and then use pivot table sorting.

Why Pivot Table Report Filter Is Not Sorted?

If new data is added to the source data and after refreshing it is not shown as a sorted list in the pivot table report filter. In this case, drag that category from the filter field into the Value field so that pivot table can treat it as a value field. Then apply sorting and drag the field again to filter the field. Now, you can see the newly added items in sorted order in the report filter.

How to Sort Pivot Table by Count?

To sort the pivot table by count, click on the arrow sign of a value field, select Value Field Settings, choose Count from the Summarize value field by section, and press OK. Now when you sort that value field column, it will be sorted based on the count value.


<< Go Back to Pivot Table in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Priti
Priti

Priti Halder holds a BSc degree in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology. She has been a part of the ExcelDemy project for 6 months and during this time, she has written over 30 articles and 5 comments for the platform. Priti is currently employed as an Excel and VBA content developer and provides effective solutions to various Excel-related issues. She is passionate about expanding her knowledge of data analysis and Microsoft... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo