Forum

search text or phra...
 
Notifications
Clear all

search text or phrases from a string in Table A and match with another list and return the values

22 Posts
3 Users
0 Reactions
347 Views
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Power Query Guru

See attached,   is there a way to search text or phrases from another List and return the values 

 

in excel need to use

=IFERROR(LOOKUP(2^15,SEARCH('Contents Lib'!$A$2:$A$8,A2),'Contents Lib'!$B$2:$B$8),"")

and is tedious

 

thank you 

 
Posted : 12/11/2019 2:36 am
(@mynda)
Posts: 4761
Member Admin
 

Hi Chris,

Yes, you can extract the camp from the text string and then use Merge Tables to bring in the Region column. See example attached.

Mynda

 
Posted : 12/11/2019 7:47 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Mynda,

Thanks,  this is for delimited that are consistent throughout 

in my real case scenario,  the description can be free form see attached (illustration)

is there a equivalent excel array in Power Query or there are other simple way (using M language)

 
Posted : 13/11/2019 12:28 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Mynda,

what version of excel you are using

= Table.AddColumn(#"Changed Type", "Text Between Delimiters", each Text.BetweenDelimiters([Folder path], "/", "/", 2, 0), type text)

my excel 2016 don't have the Text Between Delimiters option,  mine office is professional plus 2016

 
Posted : 13/11/2019 12:42 am
(@mynda)
Posts: 4761
Member Admin
 

Hi Chris,

I'm using Office 365. You can try updating your version of Excel to see if you get the new functions.

In regards to your messy data. Even with formulas there are limits. CampAB without a space will not be found because it doesn't exist in your Camp Regions table.

For the other data, you could split it by 'Camp' as the delimiter and then remove the text after the last forward slash and the .... and then do a merge on the Camp name. It's all possible, except for the CampAB without the space.

Mynda

 
Posted : 13/11/2019 2:26 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Mynda,

Actually,  the actual datasets are proj description and was trying to do text analytics, and there is a table (List) that store all the translation,  for e.g. if Camp A - Fleet Department

In the past I use combination of few excel commands

=IFERROR(LOOKUP(2^15,SEARCH('Contents Lib'!$A$2:$A$8,A2),'Contents Lib'!$B$2:$B$8),"")

 

$A$2:$A$8 = Camp AA,  Camp BB .....    (this one will grow)

$B$2:$B$8 = 2nd column that returning back for analysis

Therefore,  I was wondering is there a better way to use M language combine with PQ editor features to ease user's experience haha,  as for using delimiter need to build a lot of scenario as we wouldn't know the pattern of the data input from users (free play), those are legacy system fields

Thank you

 
Posted : 13/11/2019 2:59 am
(@mynda)
Posts: 4761
Member Admin
 

This thread might give you some ideas. You'll still need to fix any data that doesn't conform to your list e.g. CampAB without the space.

 
Posted : 13/11/2019 7:07 am
(@catalinb)
Posts: 1937
Member Admin
 

Hi Chris,

You can transform the lookup table query into a function, after adding a new column to check for a match on each row.

Note that there cannot be perfect matches, as for example //Level 1/Camp ABC/Bunk can match with Camp ABC -region A but also with Camp AB-region C.

The solution from the attached file will take only the first match in such cases.

You might be able to deal with such cases if your lookup table contains values in this format:

/Camp ABC/

/Camp AB/

 
Posted : 13/11/2019 8:12 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

thank you so much Mynda for your tips

as for the CampAB without space,  user will need to constantly update their library

 
Posted : 14/11/2019 10:48 pm
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Thank you so much Catalin,  this is the first time I see function and parameter in PQ,   after adding CampAB = C and refresh it returns correctly,  amazing !!!

Can I confirm your function will look out any key words, phrases in the matching table and return column 2 for any form of verbatim texts

 

Thank you !!

 
Posted : 14/11/2019 10:50 pm
(@catalinb)
Posts: 1937
Member Admin
 

Well, that's what your test just did, if the keyword is in lookup table, there will be a value return,  it will display null if no match found.

It is case sensitive, so you have to be careful with text case, or make the query case insensitive to make it work with "Camp AB" and "camp ab" as well.

As mentioned before, if there are multiple matches, only the first will be returned. It is possible though to return a list of all matches in this case, like "A, C, D" if there are 3 matches for example.

 
Posted : 14/11/2019 11:51 pm
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin

for the match function,  you are using Advanced editor to input M code directly right,  or UI ?

= (Text)=>
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Camp", type text}, {"Region", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Match", each Text.Contains(Text,[Camp])),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Match] <> false)),
#"Removed Other Columns" = try Table.SelectColumns(#"Filtered Rows",{"Region"}){0} otherwise null
in
#"Removed Other Columns"

which line should I amend if I want to expand my table 2 to include other keywords other than Camp **

and each time we entered an invalid text it will involve the result in the query listing,  can we just display out it is null at the preview area ?

 

Sorry to ask so many questions,  all along i was no good in coding   🙁

 
Posted : 15/11/2019 12:44 am
(@catalinb)
Posts: 1937
Member Admin
 

First, I add the lookup table as a query, just like any other table, ad I perform the operations I need to get the result I need: added a column with a simple formula to check if my custom text is in the Camp column, filter out false matches, remove other columns than "Region".

Only at this point I open the advanced editor, find below in red the only changes I made to convert the query to a function that takes a text variable:

(Text)=>
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Camp", type text}, {"Region", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Match", each Text.Contains(Text,[Camp])),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Match] <> false)),
#"Removed Other Columns" = try Table.SelectColumns(#"Filtered Rows",{"Region"}){0} otherwise null
in
#"Removed Other Columns"

The (Text)=> part is what makes it a function, you just need to place that above the existing query.

 

To add more keywords, just add them to the lookup table from sheet, there is nothing else you need to do in query, it will just read the updated table.

The rows with no match will display null in both query editor and result table.

 
Posted : 15/11/2019 5:23 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi catalin,

so each time I append new phrases and the result,   the function code will automatically update right ?

 
Posted : 15/11/2019 9:18 am
(@catalinb)
Posts: 1937
Member Admin
 

Yes.

 
Posted : 15/11/2019 10:58 am
Page 1 / 2
Share: