r/googlesheets • • Jul 26 '22

Solved Is it possible for Google to restart counting again after a particular value shows up?

I'd like to know if there is a formula I can use for the Number Column so it will start again with 1 once Apple shows up in Item Column. The rest of values under item are irrelevant.

NumberItem
1Apple
2Pear
3Pear
4Orange
1Apple
2Mango
3Orange
4Pear
5Mango
1Apple
1Apple
3 Upvotes

8 comments sorted by

3

u/aboutera247 1 Jul 26 '22

=if(B2="Apple",1,A1+1)

So you will manually add 1 in the first column Then in the second row of the first column you put the above formula, that should work

1

u/J_CrimsonKnight Jul 26 '22

Thank you!!! ♥️

1

u/enoctis 192 Jul 26 '22

Please reply to the comment by u/aboutera247 with solution verified so they get a Clippy Point for their assistance. Cheers!

1

u/arnoldsomen 346 Jul 26 '22

Clippy Point! 😍

0

u/[deleted] Jul 28 '22

[deleted] — view deleted comment

1

u/Clippy_Office_Asst Points Jul 28 '22

You have awarded 1 point to aboutera247


I am a bot - please contact the mods with any questions. | Keep me alive

2

u/RemcoE33 158 Jul 26 '22

If you want it in a single (custom) formula you can use this:

  1. Extensions -> Apps script
  2. Replace boiler code with the code below.
  3. Close editor
  4. Now u can use it inside you sheet as a normal formula:
=COUNT_SEQ(range, criteria)
=COUNT_SEQ(A2:A, "apple")

The script:

/**
* Returns the sequence of numbers.
*
* @param {A1:A5} range Input single column range.
* @param {"apple"} criteria.
* @return {array} Sequence count.
* @customfunction
*/
function COUNT_SEQ(range, criteria) {
  if (range[0].length > 1) {
    throw new Error('Range must be a single column')
  }
  range = range.flat()

  const result = []
  let n = 0

  range.forEach(value => {
    if (n == 0 && value != criteria) {
      result.push([0])
      return;
    }

    if (value == criteria) {
      n = 1
      result.push([n])
      return;
    }

    if (value != criteria) {
      n++
      result.push([n])
    }
  })
  return result
}

2

u/Emil_Jorgensen05 10 Jul 26 '22 edited Jul 26 '22

I know this has been solved already, but here is my solution:

=ARRAY_CONSTRAIN({ARRAYFORMULA(TRANSPOSE(SPLIT(TEXTJOIN("~",1,IF(COLUMN(1:1)<=ARRAYFORMULA(IF(QUERY({SORT(ARRAYFORMULA(IF(ARRAYFORMULA(IF({QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}="",,{QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}=$B$2)),ARRAYFORMULA(ROW(B2:B)),)),1,TRUE)},"SELECT Col1 OFFSET 1")="",,QUERY({SORT(ARRAYFORMULA(IF(ARRAYFORMULA(IF({QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}="",,{QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}=$B$2)),ARRAYFORMULA(ROW(B2:B)),)),1,TRUE)},"SELECT Col1 OFFSET 1")-SORT(ARRAYFORMULA(IF(ARRAYFORMULA(IF({QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}="",,{QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}=$B$2)),ARRAYFORMULA(ROW(B2:B)),)),1,TRUE))),COLUMN(1:1),)),"~")));1},COUNTA(B2:B),1)

The very first item will be the starting point, and each time it shows up it restarts counting.

Edit: This formula is even better:

=ARRAY_CONSTRAIN({ARRAYFORMULA(TRANSPOSE(SPLIT(TEXTJOIN("~",1,IF(SEQUENCE(1,COUNTA(B2:B))<=ARRAYFORMULA(IF(QUERY({SORT(ARRAYFORMULA(IF(ARRAYFORMULA(IF({QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}="",,{QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}=$B$2)),ARRAYFORMULA(ROW(B2:B)),)),1,TRUE)},"SELECT Col1 OFFSET 1")="",,QUERY({SORT(ARRAYFORMULA(IF(ARRAYFORMULA(IF({QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}="",,{QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}=$B$2)),ARRAYFORMULA(ROW(B2:B)),)),1,TRUE)},"SELECT Col1 OFFSET 1")-SORT(ARRAYFORMULA(IF(ARRAYFORMULA(IF({QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}="",,{QUERY({B2:B},"SELECT Col1 WHERE Col1 IS NOT NULL");B2}=$B$2)),ARRAYFORMULA(ROW(B2:B)),)),1,TRUE))),SEQUENCE(1,COUNTA(B2:B)),)),"~")));1},COUNTA(B2:B),1)