SOLVED

Power Query: If (nested or second table) contains statement

Copper Contributor

Hi everyone,

 

I am using the latest version of excel and I am trying to create multiple lookups using power query (it has to be in power query for speed reasons). what I am envisioning is Having my main table look to a lookup table and then if column in main table(keywords) contains a value in lookup table column(contain) and does not contain a value in lookup table column(not contain) then return result from lookup table column(result) else return main table(Prediction).

 

I thought nesting the lookup table into the main table would work but I cannot figure out how to make a lookup statement without expanding the columns, and expanding the columns is not an option as it will create way too many duplicates. Thank you for your help!

 

Main Table

KeywordsPredictionColumn to create
CucumberVeggieVeggie
Lemon, OrangesCitrusCitrus
Lemon, SugarCitrusLemonade

 

 

Lookup Table

ContainNotContainResult
Lemon AND sugarOrangeLemonade
   
   
   
10 Replies

@Joe_JL 

Could you please clarify data structuring. In main table you have "Lemon, Sugar", in lookup table "Lemon AND sugar". Is that intentionally, or in lookup table it could "Lemon AND sugar", or "Lemon, sugar" or "Lemon, Sugar", whatever. What is exact format if you have it?

@Sergei Baklan Thank you for looking at this!

 

What I was saying by that structure was, I would like to be able to have the statement say: it say if it contains Lemon and Sugar but does not contain Orange then result is lemonade. 

 

However, in retrospect I think it would be best formatted this way.

 

ContainAlsoContainNotContainResult
LemonSugarOrangeLemonade
    

@Joe_JL 

Thank you. I hope I understand the idea, but depends on how your data is represented Power Query could be totally different.

I appreciate your help, would it be helpful If I created a demo table structure in Power Query?

@Joe_JL 

Yes, that will be great. Sample file is always welcome.

@Sergei Baklan 

 

I have attached a sample file below (with the end results filled in, but obviously remove those as needed). Thank you again!

best response confirmed by Joe_JL (Copper Contributor)
Solution

@Joe_JL 

That could be like this, our queries are

image.png

Lookup keep as it is.

In Main query add column with list of keywords by Text.Split() of Keywords field.

Reference Lookup and create Not Contains keeping only Result and Not Contain columns, unpivot the latest, Group by Result without aggregation keeping only Values column. Thus we have list of Not Contain words for each Result (if exists)

image.png

Similar for Contains

image.png

Merge both above to have both lists in one List Combined table

image.png

Reference Main query as Final, add above Lookup Combined table to new column here

image.png

and expand it. 

Add new custom column to check KeywordsList against Contains/Not Contains

image.png

Group by initial columns without aggregation keeping in resulting table of each group only rows where previous value is true.

Expand Result from it

image.png

and finally combine with Prediction

image.png

Details are in attached file.

Thank you! Let me try this and I will let you know how it works. Thank you again for your help, really appreciate it!
@Sergei Baklan This worked great, I made a few minor tweaks and it ended up being exactly what I needed. Thank you!

@Joe_JL , you are welcome, glad it helped

1 best response

Accepted Solutions
best response confirmed by Joe_JL (Copper Contributor)
Solution

@Joe_JL 

That could be like this, our queries are

image.png

Lookup keep as it is.

In Main query add column with list of keywords by Text.Split() of Keywords field.

Reference Lookup and create Not Contains keeping only Result and Not Contain columns, unpivot the latest, Group by Result without aggregation keeping only Values column. Thus we have list of Not Contain words for each Result (if exists)

image.png

Similar for Contains

image.png

Merge both above to have both lists in one List Combined table

image.png

Reference Main query as Final, add above Lookup Combined table to new column here

image.png

and expand it. 

Add new custom column to check KeywordsList against Contains/Not Contains

image.png

Group by initial columns without aggregation keeping in resulting table of each group only rows where previous value is true.

Expand Result from it

image.png

and finally combine with Prediction

image.png

Details are in attached file.

View solution in original post