Search code examples
google-sheetssumgoogle-sheets-formulaarray-formulasgoogle-query-language

Maths based on dynamic groups


I am creating a sheet where data in column A will be input using a barcode scanner, and I want the sheet to recognize where one group of scans ends and the next begins, which will not be the same number of rows. Then do maths based on what comes after the first scan. For example, A1 will be an item barcode, A2 might be a quantity code (+/-), A3 might be either another quantity code or it could be the next item code. All of the item barcodes start with the same digits; how can I get sheets to recognize the start of a new "group" of items and do maths based on only that group? I've included a sample sheet - hope this makes sense. Prefer to do this using a formula and not VBS.

Thanks in advance!

https://docs.google.com/spreadsheets/d/1798XZ8csvQbQpAvoQGS1vJJ4gsWcYSLVXXwzhm22AmY/edit?usp=sharing


Solution

  • try:

    =ARRAYFORMULA(IFNA(VLOOKUP(IF(REGEXMATCH(A2:A, "^06767.+"), "×"&ROW(A2:A), ), 
     QUERY({VLOOKUP(ROW(A2:A), IF(REGEXMATCH(A2:A, "^06767.+"), {ROW(A2:A), 
     "×"&ROW(A2:A)}), 2, 1), IF(ISNUMBER(C2:C), C2:C*1, )*1}, 
     "select Col1,sum(Col2) 
      where Col2 is not null 
      group by Col1  
      label sum(Col2)''", 0), 2, 0)))
    

    enter image description here