Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
vinayaka123
New Member

vlookup for keyword in power query

Hi all,

 

This is my first time posting, pls help me

 

i have imported two tables into power query table1 field1 has string/sentence table2 has keywords and categorization. I need to add an another column as categorization which should take value from table2, categorization based on any word matches in keywords list.

eg. Table1

Field1(String)                 categorization (expected to pick from table 2)

sitting chair for office     Furniture

 

table2

keywords                      categorization

Chair                             Furniture

Table                              Furniture

1 ACCEPTED SOLUTION
Omid_Motamedise
Memorable Member
Memorable Member

Initially load table 1 into power query

Omid_Motamedise_0-1724887408785.png

and also table 2 as categories

Omid_Motamedise_1-1724887429731.png

 

 

then select table 1, from add column tab, select custom column anduse this formula in it.

 

List.Accumulate(Table.ToRows(Table2),"",(a,b)=> if Text.PositionOf(Text.Lower([Field]),Text.Lower(b{0}))>=0 then a & b{1} else a)

 

it result in what you whant as below.

 

Omid_Motamedise_2-1724887488693.png

 

 

download the solution from here

https://docs.google.com/spreadsheets/d/1V-gvjQwTghDNqB3l2x3bDOfHP2y3tNLY/edit?usp=sharing&ouid=10517...

 

 

View solution in original post

5 REPLIES 5
Omid_Motamedise
Memorable Member
Memorable Member

Initially load table 1 into power query

Omid_Motamedise_0-1724887408785.png

and also table 2 as categories

Omid_Motamedise_1-1724887429731.png

 

 

then select table 1, from add column tab, select custom column anduse this formula in it.

 

List.Accumulate(Table.ToRows(Table2),"",(a,b)=> if Text.PositionOf(Text.Lower([Field]),Text.Lower(b{0}))>=0 then a & b{1} else a)

 

it result in what you whant as below.

 

Omid_Motamedise_2-1724887488693.png

 

 

download the solution from here

https://docs.google.com/spreadsheets/d/1V-gvjQwTghDNqB3l2x3bDOfHP2y3tNLY/edit?usp=sharing&ouid=10517...

 

 

Thank you so much it works perfectly!! just made a small change.. as it was showing picking two times same item. but not sure what is a and b in this..  List.Accumulate(Table.ToRows(keyword),"",(a,b)=> if Text.PositionOf(Text.Lower([merge text]),Text.Lower(b{0}))>=0 then b {1} else a)

Here I bring a short description, but for depth explanation, please chek my video on youtube:
https://youtu.be/G8PRbWuDcmQ?si=V_dmzlSdw3r8WVT4


generaly (a,b)=> described a custom function with two arguemnt (input parameters) and in the third argument of List.Accumulate, we have to defie a custom function with two arguments.

in this example, the result of Table.ToRows(keyword) will be {{"Chair","Furniture"},{ "Table","Furniture"}} and [merge text] for the first row is equal to "sitting chair for office", so 

List.Accumulate(Table.ToRows(keyword),"",(a,b)=> if Text.PositionOf(Text.Lower([merge text]),Text.Lower(b{0}))>=0 then b {1} else a)


by replacing these values, the formula can be rewriten as:


List.Accumulate({{"Chair","Furniture"},{ "Table","Furniture"}},"",(a,b)=> if Text.PositionOf(Text.Lower("sitting chair for office"),Text.Lower(b{0}))>=0 then b {1} else a)


if we imaging all the texts on both the table are in lower case, to simplify the formula, we can remove Text,Lower on the formula and rewrite it as:

List.Accumulate({{"Chair","Furniture"},{ "Table","Furniture"}},"",(a,b)=> if Text.PositionOf("sitting chair for office",b{0})>=0 then b {1} else a)



in this formula, List.Accumulate make a loop over the items in the first input.
so in hte first iteration a="" (initial value of a is the second argument in the function) and b is equal to first item in the list inserted in the first argument and equal to  {"Chair","Furniture"}, so b{0} "Chair" and b{1} is "Furniture".

so 
if Text.PositionOf("sitting chair for office",b{0})>=0 then b {1} else a
for the first itteration is equal to 

if Text.PositionOf("sitting chair for office","Chair")>=0 then  "Furniture" else "".

as the condition of this if statemnt is true, so it return "Furniture".
the result of this stattement will be consider as a new value for a in the next iteration.
so in the second iteration a is "Furniture" and b is { "Table","Furniture"}


this loop will be continue

jgeddes
Super User
Super User

I have attached a file that shows one way of doing this. 
Starting with tables...

jgeddes_0-1724872099389.png

jgeddes_1-1724872109701.png

and ending up with...

jgeddes_2-1724872134156.png

 

 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





AlienSx
Super User
Super User

@vinayaka123 

1. Create 2 lists in your code (and buffer them!): table2[keywords] and table2[categorization]

2. Add custom column to your table1 with a function that looks up a position of keyword in keywords list (List.PositionOf with option Occurrence.First and comparison Text.Contains) and use that position to grab an item from categorization list.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors