Search code examples
google-sheetscountuniquearray-formulascountif

Increment Number By Value With Condition - Array Formula


i'm confused with the following condition, simply i want to have arrayformula or maybe a custom-formula to increment number in a way bound by specific condition based on value in other column, put it simply :

if the group doesn't change and sub-group is different increment number by 1
else if the group doesn't change and sub-group is doesn't change (same) hold value by previous
else if the group change regardless sub-group value reset number back to 1

for ilustration

** notes Number is the result that i want, in example i fill it manually

Group Sub-Group Animal Number
Land poisonus snake 1
land friendly dog 2
land friendly cat 2
land scary lion 3
aquatic friendly nemo fish 1
aquatic predator shark 2

UPDATE (dummy file link) :
https://docs.google.com/spreadsheets/d/1DAPf-DvWz50_DJ0IqAoSHbfEnfg_mN1lNXHcCjkj27M/edit#gid=0


Solution

  • try:

    =INDEX(IF(A4:A="",,VLOOKUP(A4:A&B4:B, {UNIQUE(A4:A&B4:B), COUNTIFS(
     REGEXEXTRACT(UNIQUE(A4:A&"×"&B4:B), "(.*)×"), 
     REGEXEXTRACT(UNIQUE(A4:A&"×"&B4:B), "(.*)×"), 
     SEQUENCE(COUNTA(UNIQUE(A4:A&"×"&B4:B))), "<="&
     SEQUENCE(COUNTA(UNIQUE(A4:A&"×"&B4:B))))}, 2, 0)))
    

    enter image description here