简体   繁体   中英

VLOOKUP with two criteria

I have two tables, Purchases and Sales:

Purchases:

SKU SID NID Cost
001 A1 9A $1.07
001 A2 4A $1.07
002 A1 5B $2.24

Sales:

SKU SID NID Sale
001 A2 $10.99
001 A1 $9.99

So, I want to do a VLOOKUP in the "NID" column of Sales to get the "NID" from the Purchases sheet where the "SKU" and "SID" from the Purchases sheet match the "SKU" and "SID" in the Sales sheet.

I can match on one of the two. For example, I could do this to match on the SKU alone:

VLOOKUP(A2,Purchases!$A:$D,3,FALSE)

But, I'd need to also match on "SID" since there are different values for "NID" based on a combo of "SKU" and "SID." If there's a better option than VLOOKUP, that's fine too. Thanks!

To achieve this without a helper column:

=INDEX(Purchases!C:C;MATCH(1;(Purchases!A:A=A2)*(Purchases!B:B=B2);0))

(You will need to confirm this with Ctrl+Shift+Enter in Excel; in Google Sheets it will work "as is" due to the fact that the INDEX function itself enables array calculations.)

Or a Google Sheets-specific solution:

=INDEX(FILTER(Purchases!C:C;Purchases!A:A=A2;Purchases!B:B=B2);1)

Vlookup is fine. Just make an additional column in both tables that's a combination of the 2 fields and do your Vlookup using that. You can call it "SKUSID":

SKUSID
001A1
001A2
002A1

(If the column is visually unappealling, you can also hide it from view. The calculation will still work.)

=IF(AND(Sales!A3=Purchases!A3,Sales!B3=Purchases!B3),Purchases!C3,"NO MATCH")

-- I would let my answer there just in case you're not concerning about the comments below.

You can follow this practical tutorial. It does a work-around solution using Match and Index.

Regards

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