Check if cell contents match items in a list

rilzniak

Active Member
Joined
Jul 20, 2012
Messages
288
I've tried to utilize ChatGPT and Google Gemini but the results just aren't what I'm looking for. I'm close, but just not sure how to tweak the formula to resolve my issue.

I have two tables: T1 and T2. In T2 I have a list of "cleaned" item numbers. In T1 I have a list of item numbers with additional characters - the number of characters will vary - before and/or after the item number. I want to use PQ to look in T2 to see if any of the results match what exists in T1 and add a column with that information if there is a match. Here's an example:

1712933197023.png


The result the engines give me is:
Excel Formula:
 = List.Count(List.Intersect(Text.ToList([ItemNumber]), Text.ToList(Text.Combine(T2[CleanedItemNumber])))) > 0
This results in an error saying that the data is not in a list format. I believe the issue is related to the List.Count and List.Intersect functions but I'm not sure how to correct. Any suggestions on how I can resolve this?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi Rilzniak,

Try something like this,

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Result = Table.AddColumn(T1, Table.ColumnNames(T2){0}, each try List.RemoveNulls(List.Accumulate(Table.ToList(T2),{},(s,c)=> s & {if Text.Contains(Record.Field(_,Table.ColumnNames(T1){0}), c) then c else null })){0} otherwise null)
in
    Result

Regards,
 
Upvote 1
Solution
Hi Rilzniak,

Try something like this,

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Result = Table.AddColumn(T1, Table.ColumnNames(T2){0}, each try List.RemoveNulls(List.Accumulate(Table.ToList(T2),{},(s,c)=> s & {if Text.Contains(Record.Field(_,Table.ColumnNames(T1){0}), c) then c else null })){0} otherwise null)
in
    Result

Regards,
Holy crap! Alex, you're amazing.

Is there any chance you could break down how this works?
 
Upvote 0
Added a column and compared each item of column “ItemNo” with “Cleaned values” with Text.Contains function into list. Accumulate (powerfull function for looping).

List.Accumulate goes through each item in a list, in this case your table T2 – Cleaned values.
Seed value is in this case an empty list.

Try … otherwise statement allows to deal with error value.

Column names were replaced by Table.ColumnNames(X) to make query dynamic.

Hope this helps.

Regards,
 
Upvote 0
This makes sense now looking at the formula but to do it on my own is not possible based on my current skill set. I have a favour to ask: I want to add a second column to T2 adjacent to the 'Cleaned' column and pull that into the "Desired Future" T1 table, how could I modify the code to accomplish that?

I modified the code for T2 slightly:

Power Query:
Table.TransformColumnTypes(Table.SelectColumns(Excel.CurrentWorkbook(){[Name="T2"]}[Content],{"Cleaned","Lookup"}),{{"Cleaned", type text}})

And here is the updated Result table code:

Power Query:
Table.TransformColumnTypes(Table.AddColumn(T1, Table.ColumnNames(T2){0}, each try List.RemoveNulls(List.Accumulate(Table.ToList(T2),{},(s,c)=> s & {if Text.Contains(Record.Field(_,Table.ColumnNames(T1){0}), c) then c else null })){0} otherwise null),{{"Cleaned", type text}})

I tried changing a few of the {0} statements to {0,1} and {{0},{1}} but that just generated errors. I can complete steps in PQ and make minor changes but this isn't straight-forward to me on where to adjust...
 
Upvote 0
I'm not sure to fully understand your request. Could you provide a example.
 
Upvote 0
I'm not sure to fully understand your request. Could you provide a example.
Below is what I was looking for. I could just merge queries and look it up after but thought if there was a way to do it in that one step it would be easier.

1713038062640.png

Also, if I wanted to look for the 'Cleaned' codes AND the 'Style' codes in the T1 table, how could I modify the code to accomplish that? I ask because sometimes we have have DEF1 or DEF2 in a column and it would be helpful to add logic to the current code that looked for both.

Thanks in advance.
 
Upvote 0
Hi Rilzniak,

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Result = Table.ExpandRecordColumn(Table.ExpandListColumn(Table.AddColumn(T1,"Custom", each List.RemoveNulls(List.Accumulate(Table.ToRows(T2) , {}, (s,c)=> 
    s & {if Text.Contains(Record.Field(_,Table.ColumnNames(T1){0}),c{0}) then Record.FromList({c{0},c{1}}, Table.ColumnNames(T2)) else null}))), "Custom"),"Custom",Table.ColumnNames(T2))
in
    Result

Regards,
 
Upvote 1
Hi Rilzniak,

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Result = Table.ExpandRecordColumn(Table.ExpandListColumn(Table.AddColumn(T1,"Custom", each List.RemoveNulls(List.Accumulate(Table.ToRows(T2) , {}, (s,c)=>
    s & {if Text.Contains(Record.Field(_,Table.ColumnNames(T1){0}),c{0}) then Record.FromList({c{0},c{1}}, Table.ColumnNames(T2)) else null}))), "Custom"),"Custom",Table.ColumnNames(T2))
in
    Result

Regards,
You're the best!

How did you learn all of this? I need to up my game.
 
Upvote 0
Hi Rilzniak,

Better way.

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Result = Table.ExpandTableColumn(Table.AddColumn(T1, "Custom", each let x = Record.Field(_,Table.ColumnNames(T1){0}) in 
    Table.SelectRows(T2, each Text.Contains(x, Record.Field(_, Table.ColumnNames(T2){0})))), "Custom",Table.ColumnNames(T2))
in
    Result

Regards,
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,661
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top