Excel GROUPBY & PIVOTBY Functions - All You Need to Know (do they BEAT Pivot Tables? 🤔)

Leila Gharani
12 Jan 202416:37

Summary

TLDRThe video introduces two new Excel functions, GROUPBY and PIVOTBY, that provide powerful ways to aggregate, analyze, and present data. It demonstrates how these dynamic functions can automatically group data, calculate totals and subtotals, handle dates, exclude unwanted data, and pivot data from rows into columns. Key benefits over pivot tables include easier filtering, text support, and automatic updates. Overall, these functions enable faster, more flexible Excel reporting while avoiding tedious manual steps.

Takeaways

  • 😀 The new GROUPBY and PIVOTBY functions allow for easier data aggregation and pivot table-like reporting directly in Excel formulas
  • 😃 GROUPBY has 3 required arguments: Row fields (categories), Values (numbers to aggregate), and Aggregation function like SUM()
  • 📈 Optional arguments in GROUPBY: show field headers, toggle totals, sort order, filter array
  • 📊 PIVOTBY pivots data with row and column fields for layout; useful when report gets too long
  • 📉 GROUPBY can return text values, unlike pivot tables, using functions like ARRAYTOTEXT()
  • 🔢 GROUPBY excludes errors and stray calculations, cleaning up the data
  • 🗓 GROUPBY can easily handle date fields using the YEAR() and TEXT() functions
  • 🤝 HSTACK() helps include multiple fields like dates and categories in GROUPBY rows
  • 📌 GROUPBY and PIVOTBY formulas update dynamically when source data changes
  • ⬇️ PIVOTBY has additional options to customize column totals, sorting and filtering

Q & A

  • What are the two new Excel functions introduced in the video?

    -The two new Excel functions introduced are GROUPBY and PIVOTBY.

  • What are the mandatory arguments for the GROUPBY function?

    -The mandatory arguments for the GROUPBY function are: Row fields, Values, and Function.

  • How can you exclude certain rows from aggregations in the GROUPBY function?

    -You can use the Filter Array optional argument in GROUPBY to exclude rows based on criteria.

  • What is one advantage of using GROUPBY over pivot tables?

    -One advantage is that GROUPBY results are dynamic - any changes to the source data will automatically update the GROUPBY output.

  • How can you handle dates in the GROUPBY function?

    -Dates can be handled by wrapping them in functions like YEAR or TEXT to extract specific components like year and month.

  • When would you use PIVOTBY instead of GROUPBY?

    -Use PIVOTBY when you want one of the grouping categories in the columns instead of the rows to avoid too much horizontal scrolling.

  • What are the additional arguments available in PIVOTBY?

    -PIVOTBY has a Columns argument to specify which fields should be pivot columns. It also has additional options like Column Total Depth.

  • Can GROUPBY and PIVOTBY output text fields?

    -Yes, by using text aggregation functions like ARRAYTOTEXT or writing a custom Lambda function.

  • How can the GROUPBY output be sorted?

    -The optional Sort Order argument allows sorting the GROUPBY output by a specified column.

  • What calculations can be used to aggregate values in GROUPBY?

    -Options like SUM, AVERAGE, MEDIAN, MAX, MIN, PERCENTOF and custom Lambda functions can be used.

Outlines

00:00

😊 Introducing New Excel Functions GROUPBY and PIVOTBY

Paragraph 1 introduces two new Excel functions called GROUPBY and PIVOTBY that will change how formulas are written. It provides an example scenario where the boss asks for sales data aggregated by division and sales manager. The GROUPBY function with the SUM operation can quickly accomplish this using the data from the TSALES table.

05:02

😃 GROUPBY Function Advantages Over Pivot Tables

Paragraph 2 highlights 3 advantages of the GROUPBY function over pivot tables - it is dynamic and automatically updates if the data changes, it can return text values instead of just numbers, and it can easily exclude extraneous rows with calculations that pivot tables struggle with.

10:09

😉 More GROUPBY Function Features for Text, Dates and Advanced Logic

Paragraph 3 demonstrates additional GROUPBY features - using text functions like CONCAT or ARRAYTOTEXT within it, handling dates by extracting parts like YEAR or formatting with TEXT, logically filtering with complex criteria using Filter Array, and calculations like AVERAGE and PERCENTOF.

15:13

😀 Introducing PIVOTBY Function to Transform Data Layout

Paragraph 4 introduces the PIVOTBY function to pivot data layouts. It has a Column Fields argument to move a category into columns for better visibility. The function otherwise works similarly to GROUPBY. It covers PIVOTBY features like optional arguments and turning column totals on/off.

Mindmap

Keywords

💡GROUPBY

GROUPBY is a new Excel function introduced in the video. It allows users to quickly aggregate data in a table or range based on categories defined in the Row fields. For example, total sales by division and sales manager. It eliminates the need to create pivot tables for such reports. Examples of using GROUPBY are shown throughout the video to aggregate sales data.

💡pivot table

Pivot tables are an existing feature in Excel to summarize data for reporting. They allow data to be aggregated based on row and column fields. The video introduces GROUPBY as an alternative to pivot tables in some cases, with additional benefits like automatic updates and ability to return text.

💡aggregation

Aggregation refers to mathematical functions that summarize multiple data values into a single value, like SUM, AVERAGE, COUNT etc. The video shows how the Function argument in GROUPBY and PIVOTBY allows aggregate calculations on the Values field.

💡Lambda function

A Lambda function is an anonymous inline function that can be defined within the GROUPBY formula itself. It allows custom aggregation logic if the existing functions don't meet the needs. An example is shown to return unique sales manager names.

💡dynamic

Dynamic refers to formulas updating automatically when the source data changes. The video cites this as an advantage of GROUPBY over pivot tables. Since GROUPBY uses direct references, the output stays updated.

💡HSTACK

HSTACK allows horizontal stacking or concatenation of arrays/ranges into a single array. It is used in the video to combine the date range and sales manager range into a single array for the Row fields argument.

💡PIVOTBY

PIVOTBY is the second new function introduced. It works similarly to GROUPBY but transposes one of the Row/Column fields into columns for a compact layout. It is ideal when the report becomes too long with GROUPBY.

💡column fields

Column fields is a new argument only available in PIVOTBY. It allows the user to define which field should become the output columns rather than rows.

💡optional arguments

Both GROUPBY and PIVOTBY have additional optional parameters like sort order, hide totals etc. These optional arguments allow customization of the output table.

💡Filter Array

Filter Array is the last argument in GROUPBY which allows filtering the Row field, for example to exclude subtotals. This is used in the video to handle unwanted data.

Highlights

Introduction of GROUPBY and PIVOTBY functions in Excel that will change formula writing.

GROUPBY function simplifies generating reports such as total sales by sales manager and division.

GROUPBY requires three mandatory arguments: Row fields, Values, and the aggregation Function.

The function supports a variety of pre-built aggregation functions, including SUM, AVERAGE, and COUNT, and allows custom Lambda functions.

GROUPBY automatically provides a total as part of its default behavior.

Optional arguments of GROUPBY include Field Headers, Total Depth, and the ability to sort data.

GROUPBY's dynamic update feature ensures real-time report adjustments based on data changes.

GROUPBY can return text in the Values field, a capability not available with pivot tables.

Using GROUPBY to exclude unwanted data or calculations with the Filter Array argument.

PIVOTBY is recommended when reports get too lengthy and categories are better suited for columns.

PIVOTBY allows for specifying row fields, column fields, values, and the aggregation method.

GROUPBY and PIVOTBY can handle dates efficiently, including year and month combinations.

HSTACK function in GROUPBY can combine multiple categories, like date and sales manager, in the report.

PIVOTBY enhances reports by organizing data with additional dimensions in columns and offering optional arguments for customization.

Examples demonstrate the versatility and dynamic capabilities of GROUPBY and PIVOTBY for advanced Excel reporting.

Transcripts

play00:00

They're coming, and they're here to stay!  Two new functions called GROUPBY and PIVOTBY  

play00:05

that are going to change the way you write your  formulas. These are functions for everyone,  

play00:11

so make sure you watch this video,  no matter what your Excel level is,  

play00:15

so you're prepared once they  show up in your spreadsheet.

play00:19

So, imagine you get to the office, and you  start to enjoy your cup of coffee. Then,  

play00:23

the boss comes along and says, "Hey, I have  this Excel table. It's called TSALES. I want  

play00:28

you to quickly give me the total sales for  each sales manager by division right now."  

play00:34

You take a look at this and you think, "Well, I  could create a pivot table." If you know how  

play00:39

to create a pivot table, if not, don't worry;  I have lots of videos on the channel that can  

play00:43

help you out. Your other option starting  now into the future is to use a function  

play00:49

that gets this done. That function is the  new GROUPBY function, and it's super easy  

play00:54

to use. All you need to do is type in =GROUPBY. It only needs three mandatory arguments.

play01:01

The first one is the Row fields. These are  basically your categories, so the boss wanted  

play01:06

Division and Sales Manager. I'm just going  to select these columns. The next argument is  

play01:11

the Values. In this case, it's my sales. If I had  multiple values, so if I had Profit as well, for example, I would  

play01:17

just select that as well. Next is the Function.  How do we want to aggregate these numbers? Take a  

play01:23

look at this; we have a lot of pre-built functions  that we can choose from. One is the SUM function;  

play01:29

this is probably going to be the most used one.  Then we have AVERAGE, MEDIAN, COUNT, MAX, MIN,  

play01:35

and so on, right? Lots of things to choose  from, plus you get the ability to write your  

play01:40

own Lambda function if you can't find a function  here that gets the job done for you. But for that,  

play01:47

you have to be a bit more advanced, and I'm  going to show you an example in a little bit.

play01:51

So in this case, all we need is the SUM.  You can either type it in or select it,  

play01:56

close the bracket, press Enter, and that's  it. You can go back and enjoy your coffee  

play02:02

now. Another great feature of this, though, is  that we get a total automatically as part of  

play02:08

this formula. That's the default behavior. The  total right now comes with it. As you can see,  

play02:13

the sum of these values is this number,  which is our total value right here.

play02:19

Now, let's go and take a look at our optional  arguments. We know these arguments are optional  

play02:24

because they're in square brackets. So, the  first optional argument is the ability to show  

play02:29

the Field Headers. In my case, I didn't select  any headers, so there's nothing to show. But  

play02:34

if I had selected it, I could select  3 here to show the field headers.

play02:39

Now, just to demonstrate how that looks, if I  had selected the headers as well, where... So,  

play02:45

I'm just clicking and clicking again to make sure  I select the headers. I have to be consistent,  

play02:51

and if I select the headers for my Row fields,  I have to also select it for my Values field,  

play02:57

right? And I choose a 3 here to show  the field header. This is what I get:  

play03:01

Division and Sales Manager, right?  But I could also just put in Division,  

play03:07

Sales Manager. I could change this to  Manager or Employee or whatever I want.  

play03:12

You can just always hardcode this, but that's  one of the optional arguments that we have.

play03:18

Personally, I would have preferred if this  argument comes all the way to the end or  

play03:24

somewhere later because I find the next two  arguments much more useful than this one. But  

play03:30

let's just move on to the next argument. This is  Total Depth. This allows us to choose whether we  

play03:35

want to show the grand totals on top or on the  bottom and also if we want to show subtotals.

play03:42

So in this case, I'm showing two different  columns, right? I could select Show Grand Totals  

play03:49

and Subtotals as well. So here, I get Health. I  get a total for Health, total for Productivity,  

play03:57

Utility, and then a grand total. You have  the ability to reverse that as well. So if  

play04:02

I put a minus 2, I show the grand total on  top, the subtotals on top, and so on. Let's  

play04:09

say boss changes their mind and they say,  "Well, I also want to include Product in there  

play04:14

as well." It's really easy to update this. All  you have to do is just remove this and select  

play04:23

these three columns, and that's it, right? We  get the total, we get our subtotals, and so on.

play04:30

If at any point in time you want  to turn off the totals altogether,  

play04:35

you can put a zero and don't show any totals.  Okay, I'm just going to press Ctrl+Z to go back.  

play04:43

Another optional argument that you have  is the ability to decide the sort order.  

play04:48

So let's say I want to sort everything by the  fourth column, by my sales value. So I'm just  

play04:54

going to put 4 here. When I press Enter,  it sorts my sales values in ascending order,  

play05:01

right? So that's the default; it's ascending  order. Obviously, it excludes the totals and  

play05:06

subtotals. If I want to sort everything in  descending order, I have to put a minus in  

play05:12

front of the column number. Now I get all my  categories here sorted in descending order.

play05:19

Now, the advantage of this over a pivot  table is that everything is dynamic. The  

play05:26

moment something changes in  my data set, so for example,  

play05:29

let's say Jesse Pinkman's FitTrack is right here  in my report. If it really has explosive sales,  

play05:37

everything updates automatically in my report.  So that's one advantage. Let me show you the  

play05:44

second advantage of this using GROUPBY over  pivot tables. That advantage is the ability  

play05:49

to return text in your Values field, which  is something you can't do with pivot tables.

play05:55

So, for example, let's say boss wants the  names of the sales managers that work for each  

play06:00

division. We could quickly use the GROUPBY function. The Row fields is my Division,  

play06:08

the Values fields is going to be my Sales Manager  field, and as for the Function, I have to select  

play06:14

something that can work with text. So CONCAT is an  option, but that's just going to stick everything  

play06:20

together. ARRAYTOTEXT is better because it's  going to separate everything nicely with a  

play06:26

comma. So when I press Enter, I get my sales  managers separated with a comma. But notice,  

play06:32

because my data set is detailed and sales  managers are repeated, I get the repetition  

play06:38

here too. Optimally, in my example, I want to  get a unique version of this ARRAY TO TEXT.

play06:45

Now, there isn't anything inbuilt  right now that you can use,  

play06:49

but you have the ability to write your own  function. So here, I could write a Lambda  

play06:54

function. For the parameter that's going to  be a representative of the sales manager,  

play07:00

it's going to be any sales manager. I'm going to  go with A for any. Then, the calculation is going  

play07:06

to be the unique version of the sales manager  should go into ARRAYTOTEXT. Inside ARRAYTOTEXT,  

play07:15

I'm going to go with UNIQUE of A, right? Any  sales manager. So let's just close these brackets,  

play07:22

and when I press Enter, I get my report.  That's another advantage over pivot tables.

play07:27

Let me show you the third and my favorite  advantage over pivot tables. Over here,  

play07:32

I have the same data set; it's just not formatted  as an Excel table, and my colleague has gone in,  

play07:38

and for some reason, they've added calculations  in the middle of this data set. So here, I have  

play07:43

Total Quarter 1, down here I have Total Quarter  2 with the sum of the cells above it, then I have  

play07:49

Total Half Year, and so on. I can create a pivot  table based on this unless I clean this up, which  

play07:55

I could do with Power Query. But you can also  clean up your data using the GROUPBY function.

play08:02

So let's say we want to get the total sales  for each sales manager. My Row fields are  

play08:07

going to be the sales manager. Now, this is  going to be a hassle to select, so I'm just  

play08:12

pressing Ctrl+Shift+Down Arrow until I get to the  bottom. Now, Ctrl+Backspace to jump back up. Next,  

play08:19

I want to get the Values fields, which is  right here, but let's just type it in. Last,  

play08:26

let's go with SUM, close bracket, press  Enter. What do we get? Those empty rows;  

play08:32

those calculations are all going to add up  and mess up our data. We want to exclude  

play08:38

these. We can do that by using the last argument  of the GROUPBY function, which is Filter Array.

play08:44

So I'm just pressing the commas or the  Excel separator just to get to the last  

play08:49

option here. Now, we want to exclude  anything that is in the Sales Manager  

play08:54

column that is a blank cell. To do that,  you basically treat it similarly to the  

play09:01

Include argument in the FILTER function. So  we want to select this column, that's 313,  

play09:07

and include anything that doesn't equal to blank,  right? An empty cell. And when I press Enter,  

play09:15

I get my proper report, excluding all  these other calculations in the middle.

play09:22

If, for some reason, you couldn't do this,  if you had some random data in here, and you  

play09:28

wanted to base your logic, let's say, on another  column, for example, to take a look at Column A,  

play09:35

and if anything starts with the word "Total,"  I want to exclude it. Can we do that? Yes,  

play09:42

we can. All we have to do is just  find the logic. So in this case,  

play09:48

it would be LEFT of the value that we  have in the A column. So we're going  

play09:54

to take a look at the entire A column,  and if the left five characters, right,  

play10:01

so that's "Total," five characters, if they don't  equal to "Total," then they should be included.  

play10:09

If they are equal to "Total," then they should  be excluded. And we get the same thing back;  

play10:14

pretty neat way of excluding things you  don't want to show up in your final report.

play10:20

Now we've been taking a look at the SUM a lot.  You can, of course, change this and take a look  

play10:25

at the AVERAGE sales for each person or the one  that's new and pretty cool is the PERCENTOF.  

play10:34

So when I press Enter, I get the percentage of  each person as compared to the total. So let's  

play10:40

just format these as a percentage, Ctrl+Shift,  and Percentage key. If I wanted to sort these so  

play10:46

that I can easily tell who sold the most, I'm  going to go to the Sort Order argument here,  

play10:53

and let's sort the second column in descending  order. I'm going to put a minus 2, press Enter,  

play10:59

and we can see the salesperson that has the  highest sales as compared to everyone else.  

play11:06

And remember, all of this is fully dynamic. So  if Christopher, for example, has a crazy sales  

play11:13

number, and he's going to jump all the way to the  top. But if we decide to ignore it because this  

play11:19

is the total of whatever quarter, then it's  going to be ignored from this calculation.

play11:27

Now it's time to take a look at how you can handle  dates in the GROUPBY function. So let's say you  

play11:33

want to take a look at total sales for each year  and month combination or for each year. How can  

play11:38

you do that? So I'm back at my original clean  table called TSales. What you're going to do  

play11:45

is start off with GROUPBY. Then we are going to  select a date column. But let's say we want to  

play11:51

get the year; you're going to put it inside the  YEAR function. That's it. Then, you're going to  

play11:58

select your Values. In this case, it's sales and  your aggregation, SUM. Press Enter, and that's it.

play12:06

What about year and month combination? Well,  here you can just use anything that can give  

play12:12

you that combination. So one thing that comes to  mind is the TEXT function because a TEXT function  

play12:18

allows you to format a date or any number or any  text into the format that you want. So we want to  

play12:26

format this date as, in quotation mark, "yyyy-mm", quotation, Enter,  and then I have the year and month combination,  

play12:38

and I get the total sales for each of these.  And we can double-check quickly if it really  

play12:43

works. So we have 87,400 for February 2024. This  is February 2024; let's just sum these, 87,400.

play12:56

Now, what if I wanted to expand this and add  another category in addition to period? So  

play13:03

I want to have the year-month combination,  and then I want to have sales manager and  

play13:07

then total sales. How would I handle that?  Well, let's copy and paste this over here,  

play13:13

and let's see how we can update this argument.  So until now, we've been highlighting,  

play13:19

like selecting a column to include as the Row  fields. For dates, we put it inside the TEXT  

play13:25

function so that we could grab the year and  month combination. But now, I also want to  

play13:30

add the sales manager column. To be able to do  that, I can use the HSTACK function, which is  

play13:36

the horizontal stacking of ranges. So I'm going  to put this inside the HSTACK function. My Array1

play13:44

is the first column that I want to show; this  is the year-month combination. Then, my Array2  

play13:50

is going to be my sales manager column. Close the  bracket for HSTACK, and that's all I need to do.

play13:57

At the very beginning of the video, I promised  you that I'm going to show you PIVOTBY too. But  

play14:01

until now, I've just been talking about GROUPBY. The good thing is that once you understand  

play14:06

how GROUPBY works, you already know how PIVOTBY works. You only need to know when will you  

play14:11

use PIVOTBY instead of GROUPBY. Whenever you  have a case where you feel that your report,  

play14:18

your data analysis is getting too long, you're  scrolling a lot more than you would like,  

play14:24

and you'd actually rather prefer to  have one of these categories in the  

play14:29

columns instead of the rows, that's  when you're going to use PIVOTBY.

play14:33

So, for example, let's say we wanted to get total  sales for each sales manager and have the year not  

play14:40

here beside the sales manager, have the year in  the columns. We would use PIVOTBY. So let's do  

play14:46

that right here. You're going to start off with  PIVOTBY. The row fields – that's what you want to  

play14:51

have in the rows that's going to be Sales Manager.  This one, the column fields – this is the extra  

play14:56

argument that comes with PIVOTBY because you  can decide what is going to be in the column. So,  

play15:02

we want to have the date, but not the date  like this because otherwise, we're going to  

play15:05

have to scroll a lot horizontally. We want  to grab the year from the date. For Values,  

play15:12

we're going to have sales, and we want to  aggregate these using SUM. That's it. We  

play15:18

get a nice report with the Sales Managers here,  we have a total on the bottom, we have the Years  

play15:23

in the columns, and we have a total on the side  as well. You can expand on this, so for example, 

play15:30

let's say you want to have the Sales Manager and  the product. You just include these in the rows,  

play15:38

and you press Enter. We have Sales Manager,  Product, and then the years on top. Because  

play15:45

we have this additional column argument,  you also get a lot of optional arguments,  

play15:51

not just for the rows but also for the column.  So, we have the Column Total Depth. Let's say  

play15:57

we don't want to show the totals in the columns,  we can turn them off by putting a 0 here. You can  

play16:03

decide on the column sort order and the filter  array. In this case, let's just hide the totals  

play16:09

in the columns, and when I press Enter, that  is gone. So, these are pretty cool functions.  

play16:16

Let me know your thoughts about them. Add  your comments below this video. As usual,  

play16:21

thank you for being here. Thank you for watching,  and I'm going to catch you in the next video.