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
?
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.
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.
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.
Such a formula does this, and is quite fast:
=filter(A:A,countif(B:B,A:A)=0)
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)))}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With