Excel Question - Sorting Pivot Tables
Excel Question - Sorting Pivot Tables
Author
Discussion

paulrockliffe

Original Poster:

16,633 posts

258 months

Wednesday 5th February 2020
quotequote all
Just thought I'd throw this out there to see if anyone knows of a solution to getting Excel Pivot Tables to sort on the Values fields?

Example data looks like this:



This is the basic structure of the data, but the reality is it's made up of a load of tables, some with hundreds of thousands of rows, so the only practical approach is to add these tables into Power Pivot / Data Model, link them with relationships and use Pivot Tables to create the tables that my users are interested in. As such I can't just do it this way.

When I draw the Pivot Table it looks like this:



I've done this using the individual fields and with a hierarchy of those fields, the result is the same. The issue I have is that I can't sort the salary column because Excel can only sort within the hierarchy and that column is a Sum, so there'll only be a single value there for each combination within the hierarchy.

Not sure if that makes complete sense, but essentially it will sort the Regions, it will sort the Office within the Regions, it will sort the Employees within the Office within the regions and the numbers must follow that.

In this (and most other) cases the thing of interest is the values, the structure around it is artificial, though different people are interested in different summaries as the hierarchy is folded up.

The effect is that the only column that can be sorted in order across the entire dataset is the first one.

This is the view I want:



But I want Salary to be the last column, because that is what makes sense to my users and because I want to retain the option to sort on an element within the hierarchy of geography and then collapse the Pivot to summarise within the hierarchy.

There is no obvious reason why Excel can't do that depending on how the data is sorted. But it won't.

I'm starting down the barrel of a VBA solution to flick the pivot between the two layouts to give the choice of how the data is viewed, which is easy enough but it shouldn't be necessary. I'm processing a million rows of data that links up to two other tables so reconfiguring the pivot is much much slower than changing the sort order. I'm hobbled with 32bit Excel so I'm much closer to the limit than I should be with this, so stability is an issue as well.

I'll bodge it, but hopefully there's something hidden somewhere that I've missed!

sideways sid

1,467 posts

246 months

Wednesday 5th February 2020
quotequote all
Step back a bit, and tell us what your users actually want to know.

Do they, for example want to identify Jamie's specific salary, or do they want to know the total salary bill for the Leeds office.

paulrockliffe

Original Poster:

16,633 posts

258 months

Wednesday 5th February 2020
quotequote all
This is just a simple example of a common problem.

The users want to be able to see all of the hierarchy (so they can collapse it up to summarise) and to be able to sort the data using the fields that are in Values. Values can contain a number of fields and some times they do need to be summed up or counted, so putting those fields into Rows to enable the sort isn't really a solution.

I can create accompanying fields when I process the data to create the sort order in some cases and I can use VBA to change the Pivot Table contents to allow the sort to be changed, but this is a pain in the arse when there should just be an option to sort on the Values column.

Dermot O'Logical

3,802 posts

160 months

Wednesday 5th February 2020
quotequote all
Henry is overpaid, and everybody in Southampton is underpaid.

HTH.

mmm-five

12,396 posts

315 months

Wednesday 5th February 2020
quotequote all
How about just putting the name first - as that's what the Salary relates to, and will make the sorting easier.

OlonMusky

708 posts

85 months

Wednesday 5th February 2020
quotequote all
Just add the Salary as first field in Rows and keep it in Values as Sum then sort on the first column not on Values. That's the only way to do this.

paulrockliffe

Original Poster:

16,633 posts

258 months

Wednesday 5th February 2020
quotequote all
mmm-five said:
How about just putting the name first - as that's what the Salary relates to, and will make the sorting easier.
The salary relates to the name, which relates to the office, which relates to the region, they're in the right order and changing the order loses utility elsewhere.

My question is whether there's something hidden away that gets around this issue or whether I just have to suck it up, bodge it and put more energy into getting corporate to let me use PowerBI instead.

paulrockliffe

Original Poster:

16,633 posts

258 months

Wednesday 5th February 2020
quotequote all
OlonMusky said:
Just add the Salary as first field in Rows and keep it in Values as Sum then sort on the first column not on Values. That's the only way to do this.
That's what I've done, but it's rubbish and doesn't work when I have multiple values and people want to sort on several columns. As an example, I have some stuff with trend analysis that is ranked and people want to be able to work with both absolute changes and percentage changes because both are equally useful or relevant.

I'm doing that by using VBA to change the column order, but its a bodge and we're not really allowed VBA anyway.

If it can't be done, it can't be done, I was just hoping there was a ninja out there and something I've missed.

Jinx

12,022 posts

291 months

Friday 7th February 2020
quotequote all
paulrockliffe said:
That's what I've done, but it's rubbish and doesn't work when I have multiple values and people want to sort on several columns. As an example, I have some stuff with trend analysis that is ranked and people want to be able to work with both absolute changes and percentage changes because both are equally useful or relevant.

I'm doing that by using VBA to change the column order, but its a bodge and we're not really allowed VBA anyway.

If it can't be done, it can't be done, I was just hoping there was a ninja out there and something I've missed.
Open up a new sheet. Link to each cell in the pivot table. Sort to your hearts content. Not pretty but works.

paulrockliffe

Original Poster:

16,633 posts

258 months

Saturday 8th February 2020
quotequote all
Jinx said:
paulrockliffe said:
That's what I've done, but it's rubbish and doesn't work when I have multiple values and people want to sort on several columns. As an example, I have some stuff with trend analysis that is ranked and people want to be able to work with both absolute changes and percentage changes because both are equally useful or relevant.

I'm doing that by using VBA to change the column order, but its a bodge and we're not really allowed VBA anyway.

If it can't be done, it can't be done, I was just hoping there was a ninja out there and something I've missed.
Open up a new sheet. Link to each cell in the pivot table. Sort to your hearts content. Not pretty but works.
That does work in principle, but it isn't a practical approach as I'm dealing with upwards of a million rows of data in some tables with various slicers reducing the number of rows to be displayed.

Excel won't work with a second copy of the data in the file anyway, as the usual on-screen whatevers it does in the back ground can't cope. Crash central. Even worse is you're running formulas and then asking them to recalculate.