Search code examples
regexgoogle-sheetssumarray-formulasgoogle-sheets-query

How to use REGEXEXTRACT only on rows that contain a cell which contain a string within a SUM?


If my data set is:

|                  H              |   U  |   |   |   |
| 100 Main St, Berkeley, CA       | $200 |   |   |   |
| 200 Other St, San Francisco, CA | $400 |   |   |   |
| 300 This St, Oakland, CA        | $250 |   |   |   |
| 111 That St, San Francisco, CA  | $90  |   |   |   |

How would I get the total sum of all values in column D, for rows which contain the word "San Francisco"? This dataset is on Sheet1, while my formula is on Sheet2 Currently, I use this formula, to sum up all rows, regardless of their city.

=ArrayFormula(sum(iferror(REGEXEXTRACT(Sheet1!U2:U918&".","\$?([0-9]+)[.,]")*1)))

But I tried using SUMIF, and Regexmatch as the first argument to ArrayFormula and neither of those worked.

UPDATE:

player0 gave me the answer(I added a sum around it to get it exactly to output what I want).

=SUM(ARRAYFORMULA(QUERY({Onfleet!H2:H, REGEXEXTRACT(TO_TEXT(Onfleet!U2:U), "\d+")*1}, "select sum(Col2) where lower(Col1) contains 'oakland' group by Col1 label sum(Col2)''")))

Solution

  • use this formula:

    =ARRAYFORMULA(QUERY({Sheet1!H2:H, REGEXEXTRACT(TO_TEXT(Sheet1!U2:U), "\d+")*1}, 
     "select Col1,sum(Col2) 
      where lower(Col1) contains 'san francisco' 
      group by Col1 
      label sum(Col2)''"))
    

    enter image description here