0

I have a category list as shown below; PAYEE contains comma-separated values for each cell value in EXPENSE CATEGORIES:

1

How can I use VLOOKUP or XLOOKUP to match the values in Merchant Name to PAYEE and populate CATEGORY with the corresponding EXPENSE CATEGORY values?

2

UPDATE: I tried using XLOOKUP with wildcard search. It does work partially. Can't figure out why it fails otherwise.

3

4
  • This is going to be hard to do with pure Excel, as it looks like you will need some regex/fuzzy matching support, which Excel does not have out of the box. You might want to do this in a SQL database. Commented Jan 5 at 3:30
  • @TimBiegeleisen I tried XLOOKUP with wildcard search, it does work partially. I cant figure out why it doesn't seem to work other times yet. Commented Jan 5 at 4:04
  • 1
    I would suggest normalising your data with Power Query within Excel which would solve all of your problems immediately. Commented Jan 5 at 5:12
  • 2
    Please do not upload images of code/data/errors. Commented Jan 5 at 5:25

2 Answers 2

1

This formula searches the payee items in column A.
Place it in B2 cell and drag down.
=LET(pay,TRIM(TEXTSPLIT(TEXTJOIN(",",TRUE,SUBSTITUTE(D$2:D$13,",","ß"&RIGHT("0000"&ROW(D$2:D$13),5)&",")),",",,TRUE)), IFERROR(INDEX(C$1:C$13,TEXTJOIN("",FALSE,IF(IFERROR(SEARCH(LEFT(pay,LEN(pay)-6),A2),0)>0,RIGHT(pay,5),"")),0),""))

The row pointer separator not necessary can be left out.

=LET(pay,TRIM(TEXTSPLIT(TEXTJOIN(",",TRUE,SUBSTITUTE(D$2:D$13,",",RIGHT("0000"&ROW(D$2:D$13),5)&",")),",",,TRUE)), IFERROR(INDEX(C$1:C$13,TEXTJOIN("",FALSE,IF(IFERROR(SEARCH(LEFT(pay,LEN(pay)-5),A2),0)>0,RIGHT(pay,5),"")),0),"")) enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Excel has a fuzzy lookup add-in, but Power Query is built-in and has fuzzy matching options.

To use Power Query

  • Put your data into two tables (and name them appropriately)

keyTable
enter image description here

merchantTable
enter image description here

  • Select a cell in one table and read it into PQ Data => Get and Transform => from Table/Range

  • Then Close and Load To and select Connection Only

  • Repeat for the other table.

  • in the PQ interface, selet the keyTable query, then Home=>Advanced Editor and paste the code below into the window that opens. (If you have named the tables as I showed, you won't need to change anything)
    Read the code comments for information as to the algorithm.

let
    Source = Excel.CurrentWorkbook(){[Name="keyTable"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EXPENSE CATEGORIES", type text}, {"PAYEE", type text}}),

//Need to trim of any trailing or leading spaces or commas
//  and replace and `comma-space` with `comma` only.
//  Then split on the commas and expand the table.
    #"Split Payees" = Table.TransformColumns(#"Changed Type",
        {"PAYEE", (p)=> if p = null then null
            else let 
                    p = Text.Trim(p,{","," "}),
                    q = Text.Replace(p,", ",",")
            in Text.Split(q,","), type {text}}),
    #"Expanded PAYEE" = Table.ExpandListColumn(#"Split Payees", "PAYEE"),

//remove categories that do not have any matching merchants.
//This will make the fuzzy matching more reliable.
    #"Filtered Rows" = Table.SelectRows(#"Expanded PAYEE", each ([PAYEE] <> null))
in
    #"Filtered Rows"

The result should look like:

enter image description here

  • Right click in the Queries pane and select New Query - Other Sources - Blank Query
  • With that query selected, open the Advanced Editor and paste the code below into the window that opens:
    (Note the Threshold parameter. This is a trial and error thing and 0.60 seems to work best for your data as posted, but you may need to adjust it
let
    Source = Table.FuzzyNestedJoin(
                merchantTable, {"Merchants"}, 
                keyTable, {"PAYEE"}, "keyTable", 
                JoinKind.LeftOuter, 
                [IgnoreCase=true, IgnoreSpace=true, Threshold=.60]),
                
    #"Expanded keyTable" = Table.ExpandTableColumn(Source, "keyTable", {"EXPENSE CATEGORIES"})
in
    #"Expanded keyTable"

Result
enter image description here

Note: The merged query can be created from the UI, but it is more difficult to describe the method in this answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.