Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract two sets of values in Google Spreadsheets?

I have two sets of values in A:A and B:B. How to make a set difference between them (preferably using a formula), i.e. get those values from A:A that do not exist in B:B?

like image 619
mik Avatar asked Jun 06 '16 08:06

mik


People also ask

How do you subtract a series of numbers in Google Sheets?

The syntax for the formula is MINUS(value1, value2) where both arguments are required. To subtract numbers, such as 20 minus 15, you would enter the following formula and press Enter. To subtract values within cells, like A2 minus A3 , you would enter this formula and press Enter.

How do I find the difference between two numbers in Google Sheets?

Use the Minus Sign to Subtract in Google Sheets When you subtract on paper, you use the minus ( - ) symbol between the numbers, which is also how Google Sheets does it. The only difference is that you refer to cells instead of numbers. For example, to subtract two numbers on paper, you'd write something like 45-17.

How do you add and subtract in Google Sheets?

To sum and subtract in Google Sheets, use the formula =SUM(x:y) and =MINUS(x,y) in the desired cells, then input the relevant values.


2 Answers

Such a formula does this, and is quite fast:

=filter(A:A,countif(B:B,A:A)=0)
like image 82
mik Avatar answered Sep 18 '22 14:09

mik


Here’s a spreadsheet showing how set operations can be done easily:

Set Operations in Google Sheets

Union is ={setA; setB}

Difference (setA-setB) is =filter(setA, iserror(MATCH(setA, setB, false)))

Intersection is =filter(setA; MATCH(setA, setB, false))

Explanation setA and setB can be named ranges or you can use normal range notation. Named ranges just make this clearer.

Union is just a new range made by juxtaposing both ranges.

Intersection (next easiest) depends on looking for indices of setA in setB where they exits, and filtering setA by that.

Difference is similar, but filters setA to pick out only members where finding the index in setB is not found.

Extra Credit

Union with duplicate elimination is just setA + (setB-setA), so by the above

={setA;filter(setB, iserror(MATCH(setB,setA,false)))}
like image 32
marc meyer Avatar answered Sep 17 '22 14:09

marc meyer