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

Google Sheets, how to subtract one array from another?


I've got two arrays, and I want to subtract array2 from the other array1 to form sorted result array. Wondering if it's even possible. I've tried to search everywhere, but haven't found the solution that I know would know how to implement.

array1
NAME  DATA1   DATA2   DATA3
MATT    6       2       4
ROBERT  3       2       1
JAKE    2       2       0
PETER   3       1       2
CHARLES 3       1       2
array2
NAME  DATA1   DATA2   DATA3
MATT    6       2       4
JAKE    2       2       0
ROBERT  2       2       0
CHARLES 2       0       2
result array
NAME  DATA1   DATA2   DATA3
PETER   3       1       2
CHARLES 1       1       0
ROBERT  1       0       1
MATT    0       0       0
JAKE    0       0       0

Solution

  • try:

    =ARRAYFORMULA({A1:D1; QUERY(QUERY({A2:D6; IFERROR(A9:D12*-1, A9:D12)},
     "select Col1,sum(Col2),sum(Col3),sum(Col4) 
      where Col1 is not null 
      group by Col1"), 
     "offset 1", 0)})
    

    0


    0