简体   繁体   中英

Compare multiple columns as pair-wise for Excel/Google Sheets

I am new to Excel/Google Sheets. I have a difficulty of writing a formula to compare columns as a pair-wise since the formula would be so big as the day goes.

For example, there're 2 main columns Foo and Bar . I want to find the total number of days that Foo and Bar are equal so the current formula is =IF(A3 = G3, 1, 0)+IF(B3 = H3, 1, 0)+IF(C3 = I3, 1, 0)+...

But this is kind of tedious because there're ~40 days to compare with. Are there any other alternatives to write a formula in efficient way? Either Google-App-Scripts or Excel Formula is appreciated.

Cheers!

在此处输入图像描述

Give a try on below google-sheet formula. Adjust ranges as you need.

=ArrayFormula(SUM(IF(A3:E3=G3:K3,1,0)))

Assuming that you're needing to get such a total for each row and not merely a single row, try this:

=ArrayFormula(IF(A3:A="",,MMULT(IF(A3:F=G3:L,1,0),SEQUENCE(COLUMNS(A:F),1,1,0))))

Of course you will need to adjust the three ranges to match your own FOO and BAR ranges.

This one formula will produce all results for all rows.

The MMULT function is tricky to explain to those as yet unfamiliar with it. But it's a powerful tool. I'll add a picture I created that may best explain what it does:

在此处输入图像描述

By making the second matrix a simple SEQUENCE of 1s as long as the other matrix is wide, we wind up multiplying everything by 1 before adding together. And since anything multiplied by 1 is itself, this combination serves only to do a row-by-row add.

Things to keep in mind with MMULT :

1.) Every cell in every matrix must be a number or it will produce an error.

2.) As in the above formula, there are ways to use either/or conditions to turn every cell in a matrix into a number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM