

August 21, 2019

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


August 21, 2019

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


July 16, 2010

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


August 21, 2019

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


July 16, 2010

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.


November 8, 2013

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/

Answers Post


August 21, 2019

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 !!


November 8, 2013

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.


August 21, 2019

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 🙁


November 8, 2013

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:
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.


August 21, 2019

Catalin,
How about the questions on not showing the invoked function in the queries list ? that is under the Text (optional) enter a text then click involve, it will generate a query in the query listing, I do not want PQ to generate, is it possible
and for this function, is zit possible to search multiple and return multiple results
for e.g. //Level/Camp CC/Tuckshop
the lookup table Camp CC = C, Tuckshop = Canteen
the result will be
//Level/Camp CC/Tuckshop D
//Level/Camp CC/Tuckshop Canteen
See attached
Thank you !


August 21, 2019

Catalin Bombea said
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:
<br />
<span style="color: #ff0000;"> (Text)=></span><br />
let<br />
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],<br />
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Camp", type text}, {"Region", type text}}),<br />
#"Added Custom" = Table.AddColumn(#"Changed Type", "Match", each Text.Contains(<span style="color: #ff0000;">Text</span>,[Camp])),<br />
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Match] <> false)),<br />
#"Removed Other Columns" = <span style="color: #ff0000;">try</span> Table.SelectColumns(#"Filtered Rows",{"Region"})<span style="color: #ff0000;">{0} otherwise null</span><br />
in<br />
#"Removed Other Columns"<br />
<br />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.
what is the original word for Text (highlighted in red) ? I am trying to understand your code from #Added custom to Removed other columns, you mentioned you use the Power query UI
or can you provide me the original code before amending to function i.e. replace add code (those in red)
Thank you


November 8, 2013

If you want to have multiple matches, remove the {0} from the function, this is what refers to the first match only.
When you work on the lookup table, you can use any text you want to add the custom column.
You can use any static text you want, "Camp AB" for example:
#"Added Custom" = Table.AddColumn(#"Changed Type", "Match", each Text.Contains("Camp AB",[Camp])),
Then, after you're done with the processing, add the parameter you want above the first line of the query:
(ParameterName)=>
and replace "Camp AB" with ParameterName.
I used Text as parameter name.


August 21, 2019

Hi Catalin,
Attached, how to return in this format, possible ?
Description of text verbatim | Region |
//Level 1/Camp ABC/Bunk | A |
//Level 1/Camp DD/Tuckshop | D |
//Level 1/Camp DD/Tuckshop | Canteen |
There is a pipe burst in Camp BB | B |
d:\folder1\Camp\CampAB | C |
----------- Camp CC ……………….. | D |
1 Guest(s)
