ICAEW.com works better with JavaScript enabled.
Exclusive

Taking the new GROUPBY() and PIVOTBY() functions further

Author: Simon Hurst

Published: 20 Nov 2024

Exclusive content
Access to our exclusive resources is for specific groups of students, subscribers and members.
The main capabilities of the recently introduced GROUPBY() and PIVOTBY() functions have already been covered in recent articles. Here we are going to look at one aspect of their use in a bit more detail: the ability to use functions and formulas within the individual arguments of each function.

Introduction

The new GROUPBY() and PIVOTBY() functions have recently been added to the production editions of Excel. We have already included several articles covering the use of the new functions, including:

and

In this post, we are just going to have a look at one additional capability of both of the two new functions: the ability to use other functions and formulas within the individual arguments of each function.

Grouping by part of a field

In this example, each of our product codes is a composite value with three components, separated by dashes:

GUA-NOR-24

We are assuming that we need to group our data using just one of those components. For example, the first three characters might represent a category of product. To group our product rows by this category using the GROUPBY() function, we could include the LEFT() function as part of our first ’row_fields’ argument.

Here we are grouping by the first three characters of our Product code field:

=GROUPBY(LEFT(Invoices3[[#All],[Product code]],3),Invoices3[[#All],[Quantity]],SUM,,0)
Screenshot from an Excel spreadsheet

In this example, we have used the [#All] modifier to include our Table header row, as well as all of the data values in the Product code column. The fourth argument of our GROUPBY() function allows us to include that header in our resulting table, but doing so does reveal an issue with applying a formula to our column:

Screenshot from an Excel spreadsheet

As we can see, our LEFT() function is not only applied to our data values, but also to the header value, converting Product code into ‘Pro’. For this reason, if you are working with fixed columns, you might find it better to leave the automatic headers out and instead type them in manually.

Let’s go a bit further and use the same field from the original Table of data to create two groups. We are going to use the MID() function to group by 3 characters starting at position 5 (one position after our first three characters and the delimiter). In order to create two groups, we are going to wrap our two text functions within the HSTACK() function. This Dynamic Array function combines 2 or more arrays into multiple columns. We have also changed our Table column references to just use the data rows by removing the [#ALL] modifiers from each of our ranges. We have replaced the automatically generated headers with text:

=GROUPBY(HSTACK(LEFT(Invoices3[Product code],3),MID(Invoices3[Product code],5,3)),Invoices3[Quantity],SUM,,0)
Screenshot from an Excel spreadsheet

Grouping row and column fields in PIVOTBY()

The same techniques would also work with the PIVOTBY() function. Here, we have used our previous, HSTACK(), example to generate our ‘row_fields’, and then used the YEAR() function with our OrderDate field to create the group for our ‘col_fields’ argument. We have also deleted the manual headers from the previous example:

Screenshot from an Excel spreadsheet

Additional resources

You can explore all aspects of Excel, including many articles on conditional formatting, in the ICAEW archive.

Archive and Knowledge Base

This archive of Excel Community content from the ION platform will allow you to read the content of the articles but the functionality on the pages is limited. The ION search box, tags and navigation buttons on the archived pages will not work. Pages will load more slowly than a live website. You may be able to follow links to other articles but if this does not work, please return to the archive search. You can also search our Knowledge Base for access to all articles, new and archived, organised by topic.

Open AddCPD icon

Add Verified CPD Activity

Introducing AddCPD, a new way to record your CPD activities!

Log in to start using the AddCPD tool. Available only to ICAEW members.

Add this page to your CPD activity

Step 1 of 3
Download recorded
Download not recorded

Please download the related document if you wish to add this activity to your record

What time are you claiming for this activity?
Mandatory fields

Add this page to your CPD activity

Step 2 of 3
Mandatory field

Add activity to my record

Step 3 of 3
Mandatory field

Activity added

An error has occurred
Please try again

If the problem persists please contact our helpline on +44 (0)1908 248 250