Forum

what is the syntax ...
 
Notifications
Clear all

what is the syntax for exact match in Text.Contains("LMS800", [Document])

15 Posts
4 Users
0 Reactions
559 Views
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi,

What is the Syntax for Text.Contains M code in Power Query

i.e.  LMS800 = LMS800,  LMS800C4 = LMS800C4

If Document =  "....................LMS800C4.........."   then return LMS800C4 exactly instead of two outputs (See my attachment)

Thank you!

 
Posted : 07/03/2020 2:49 am
(@mynda)
Posts: 4761
Member Admin
 

Hi Chris,

There are some suggestions here.

Mynda

 
Posted : 08/03/2020 1:14 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Mynda,

what they meant by "wrapping the string text in single spaces (b4 and after). Did the same with the list of lookup values.................."

thank you !

 
Posted : 08/03/2020 1:45 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

place a space before and after the text, however only one found don't know why ?

 
Posted : 08/03/2020 3:11 am
(@mynda)
Posts: 4761
Member Admin
 

Hmmm, because the documents that don't have 'Project' at the end don't have a space after the project code. Why don't you split the Project... off the end of the documents so they're consistent, then it may work better. See attached.

Mynda

 
Posted : 09/03/2020 6:14 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Treacy,   in my real data source is not so "structured" meaning,  the project number can appear anywhere in a verbatim,  so there is no way to split it until so clean,  that was why I use the function match that uses Text.Contains( )

 
Posted : 09/03/2020 11:24 am
(@debaser)
Posts: 836
Member Moderator
 

You could add a query that calculates the highest length match for a given document, and then only return the rows that match on both document name and match length?

 
Posted : 10/03/2020 8:49 am
(@catalinb)
Posts: 1937
Member Admin
 

Your Match function needs a minor tweak, just add a space after the Document text:

each Text.Contains(Text & " ",[Project])

 
Posted : 11/03/2020 1:06 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin,  thanks for your tips,  this works for Project number infront with blank

for this type Document 898 LS(T)800/2890DOC,   it will not work,  does it mean that I need to add a  "/" to original value, that will be tedious

if there any other workaround

Thank you !

 
Posted : 11/03/2020 9:04 am
(@catalinb)
Posts: 1937
Member Admin
 

The solution is in front of you 🙂

Select the Document column, replace "/" with a space, before using the Match function.

 
Posted : 12/03/2020 12:12 am
(@catalinb)
Posts: 1937
Member Admin
 

There is a way to use regular expressions to match complex patterns in power query, if you have messy data.

Here is the function I am using:

(Text as text, Pattern as text)=>
let
RunScript = Web.Page(
"<script>
document.write(
"""&Text&""".match(
new RegExp('"&Pattern&"','g')
).join(';')
);
</script>"
),
Matches = try RunScript{0}[Data]{0}[Children]{1}[Children]{0}[Text] otherwise null
in
Matches

The query is simple, just take the text from a table, the pattern from another table and pass them to the function:

let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
Pattern = Excel.CurrentWorkbook(){[Name="Pattern"]}[Content]{0}[Pattern],
#"Added Custom" = Table.AddColumn(Source, "Matches", each FindMatches([Text],Pattern))
in
#"Added Custom"

You can specify in the pattern lots of conditions like: text can be at the beginning, at the end, might be delimited by some chars, and lots of complex scenarios.

 
Posted : 12/03/2020 12:59 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Chris Yap said
Hi Catalin,  thanks for your tips,  this works for Project number infront with blank

for this type Document 898 LS(T)800/2890DOC,   it will not work,  does it mean that I need to add a  "/" to original value, that will be tedious

if there any other workaround

Thank you !  

W

Catalin Bombea said
There is a way to use regular expressions to match complex patterns in power query, if you have messy data.

Here is the function I am using:

<br />
<br />
(Text as text, Pattern as text)=&gt;<br />
let<br />
RunScript = Web.Page(<br />
""<br />
),<br />
Matches = try RunScript{0}[Data]{0}[Children]{1}[Children]{0}[Text] otherwise null<br />
in<br />
Matches<br />
<br />

The query is simple, just take the text from a table, the pattern from another table and pass them to the function:

<br />
<br />
let<br />
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],<br />
Pattern = Excel.CurrentWorkbook(){[Name="Pattern"]}[Content]{0}[Pattern],<br />
#"Added Custom" = Table.AddColumn(Source, "Matches", each FindMatches([Text],Pattern))<br />
in<br />
#"Added Custom"<br />
<br />

You can specify in the pattern lots of conditions like: text can be at the beginning, at the end, might be delimited by some chars, and lots of complex scenarios. 

Catalin Bombea said
There is a way to use regular expressions to match complex patterns in power query, if you have messy data.

Here is the function I am using:

<br />
<br />
(Text as text, Pattern as text)=&gt;<br />
let<br />
RunScript = Web.Page(<br />
""<br />
),<br />
Matches = try RunScript{0}[Data]{0}[Children]{1}[Children]{0}[Text] otherwise null<br />
in<br />
Matches<br />
<br />

The query is simple, just take the text from a table, the pattern from another table and pass them to the function:

<br />
<br />
let<br />
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],<br />
Pattern = Excel.CurrentWorkbook(){[Name="Pattern"]}[Content]{0}[Pattern],<br />
#"Added Custom" = Table.AddColumn(Source, "Matches", each FindMatches([Text],Pattern))<br />
in<br />
#"Added Custom"<br />
<br />

You can specify in the pattern lots of conditions like: text can be at the beginning, at the end, might be delimited by some chars, and lots of complex scenarios.  

wow Catalin,  this is too complicated for you,  that is simple for you

if you have time appreciate you can help to code it in my example and I will learn from there,    just like previously I learned the Text function on search substrings from you,  very useful

at the meant time will use replace "/" with ""  haha

 
Posted : 12/03/2020 2:37 am
(@catalinb)
Posts: 1937
Member Admin
 

This is the pattern I used:

[A-Z]{2,3}\(?[A-Z]{1}\)?[0-9]{3}([A-Z]{1})?([0-9]{1})?

File attached.

 
Posted : 12/03/2020 6:56 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

wow Catalin,  amazing,  can explain the pattern in layman term,  really want to learn from you

 
Posted : 12/03/2020 8:21 am
(@catalinb)
Posts: 1937
Member Admin
 

If you want to learn regular expressions, you can start here: https://regex101.com/r/wQ4mK9/1

They provide also a playground for JavaScript expressions, used in the power query function.

[A-Z]{2,3}\(?[A-Z]{1}\)?[0-9]{3}([A-Z]{1})?([0-9]{1})?

You can see the groups in different colors (i used 5 groups):

first group says that there can be 2 or 3 uppercase letters

second group says that there can be an uppercase letter in paranthesis, the ? says that it might be missing

third group: 3 digits always

4-th group: there can be an uppercase letter, might me missing on some entries (that's what ? symbol means)

group 5: there can be 1 uppercase letter, but might be missing sometimes (?)

 
Posted : 12/03/2020 8:43 am
Share: