Sheets tip 257: I've lost count how many times I've used this formula...



Hi Reader,

Welcome to the Google Sheets Tips newsletter #257, your Monday morning espresso, in spreadsheet form!

_______

➜ Google Sheets Tip #257: How to number groups

Today's tip is one I've used countless times in my spreadsheets.

It's a hugely useful formula if you work with data in groups.

As always, there are many different ways to achieve this. I'll show you two methods in this email: a simple, manual way, and a more advanced single formula method.

Here's the data in column A and the expected output in column B:

We want to create a formula that generates the numbering pattern in column B.

Method 1: Simple method

This formula checks whether the value in the current row is different to the one on the row above.

If they are different, the output of the formula is 1 (i.e. start the numbering anew).

If the are the same then the numbering increments the value from the line above by 1.

The formula in B2 is:

=IF(A2<>A1,1,B1+1)

It can be dragged down as far as required.

This picture illustrates what's happening:

One drawback with this formula is that if you delete a row or insert a row, you need to drag the formula down from the top again. Otherwise your references will be out of line.

Method 2: SCAN function method

This method uses the SCAN, IF and OFFSET functions.

The value "name" inside the LAMBDA is the name of the current value from the input array (the values in column A).

The OFFSET(name, -1, 0) grabs the value of the row above and the IF compares it against the current "name" value.

=SCAN(0 , A2:A15 , LAMBDA( counter , name , IF( name<>OFFSET(name,-1,0) , 1 , counter+1 )))

This formula has a number of advantages:

  • it only needs to be entered once, in cell B2
  • it will update the numbering automatically if you delete a row, add a row, or change the groups

In case you haven't noticed, I LOVE these new (2022) LAMBDA functions.

If you're new to them, do yourself a favor and check out my free Lambda Function course.

_______

If you enjoyed this newsletter, please forward it to a friend who might enjoy it.

Have a great week!

Cheers,
Ben

P.S. Map of Ancient Grease

Google Sheets Tips

Get better at working with Google Sheets! Join 50,000 readers to get an actionable tip in your inbox every Monday.

Read more from Google Sheets Tips

Hi Reader, Welcome to Future Proof (formerly the Google Sheets Tips newsletter), your Monday morning espresso, in spreadsheet AI form! I published the first Google Sheets Tips newsletter in April 2018, a lifetime ago in technology terms. The mission was simple: one practical, actionable lesson each week to help you work more effectively. That mission hasn't changed. Lately though, more and more of you are turning to AI to get things done. And if you're anything like me, you're finding that...

Hi Reader, Welcome to the Google Sheets Tips newsletter #400, your Monday morning espresso, in spreadsheet form! Well, this is it folks... This is the final issue of the Google Sheets Tips newsletter. (I will continue sending a weekly tech tutorial newsletter. Just that the format and the focus will be changing.) So then. The end of an era. I think this is where I'm supposed to write something profound and wax lyrical about how life-changing it's all been, blah, blah, blah. But I've always...

Hi Reader, Welcome to the Google Sheets Tips newsletter #399, your Monday morning espresso, in spreadsheet form! This is the penultimate issue of the Google Sheets Tips newsletter. You may recall from one of my emails earlier in the summer that I mentioned changing this newsletter. Recently, I decided that issue 400 was a nice milestone to reach and a good place to close the curtain on these dedicated Google Sheets tips. So the final issue of the Google Sheets Tips newsletter—#400—will be...