Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
joeywong
Helper I
Helper I

Use wildcard to extract words

Hi all,

 

I have a column with unstructured words. I need to extract the words.

AA

AA

AAgotozoo

AA

.AA

AA

AA$$

AA

Zoneedto goAA

AA

$$AA

AA

 

I tried to add a condition column, but it only extracted certain words but not the complete list. Does anyone know how to use wildcards to extract the text?

 

joeywong_0-1670166791693.png

 

Thanks!

7 REPLIES 7
hnguy71
Memorable Member
Memorable Member

Hi @joeywong,

Is it a list of text string? Or a very specific wildcard pattern?



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Hi Hnguy71,

 

It list a of text string. 

 

Unpredictable nature is a pattern. It can be in front or behind at different times. The words can join together some sometimes. Below are some of the samples.

 

joeywong_0-1670170300162.png

 

Thanks!

 

 

Hi @joeywong ,


To accomplish this, you'll first need a list of keywords to provide such as this:

hnguy71_0-1670173922730.png

 

And then create a custom column to check if the sub-string matches any in the list.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WciwoyElVitUBsgICfFzBLBUkQRBLwdQUzDE1VUCTSc8vya/KzwcL6CEMALMUVFTAHKB8XmpqSkl+er4CVEksAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Words Pattern" = _t]),
    
    // Retrieve list of text strings
    ListPatterns = List.Buffer(KeywordList),
    
    // Find pattern, return first found string, ignoring case-sensitivity.
    ReturnFoundString = Table.AddColumn(Source, "Extract Words", each 
    List.First(
        List.Transform( 
            ListPatterns , (n) => if Text.Contains([Words Pattern], n, Comparer.OrdinalIgnoreCase) then n else null
        )
    )
    ,
    type text)
in
    ReturnFoundString

 

Expected Output:

hnguy71_1-1670174086949.png

 

I've included a sample pbix for reference.



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Hi hnguy71,

 

Thank you so much for your input.

I am quite new to M-code. When I tried to apply the code to the real case, there was an expression error.

 

I import the excel file into Power Bi desktop without putting the code.... "let_t=((type nullable text) meta [seralized.Text=true) in type table....."  Can you elobrate more on this code? Thanks!

joeywong_2-1670671964413.png

 

 

 

 

 

Hi @joeywong ,

 

The source originated from when you click "Enter Data". A json document is generated and then converted into a table for additional ETL. Since you already have your own "source", you may either omit or substitue it with your own.

 

You should ideally start at the ListPatterns step and all the way down. 

 

If you're having further issues, copy and paste your query here and I can help modify it for you to copy and paste back.



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Hi @hnguy71 ,

 

Thank you so much for your kind gesture.

 

I tried to input the suggested M-code but encountered the following errors:

joeywong_0-1672216185672.png

 

Below is the M-code that I used:

 

let
Source = Excel.Workbook(Web.Contents("https://smrtcorp-my.sharepoint.com/Documents/Desktop/Dylan%20Files/LBS%20settlement%20(working%20fil..."), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Words Pattern", type text}}),
ListPatterns = List.Buffer(KeywordList),
// Find pattern, return first found string, ignoring case-sensitivity.
ReturnFoundString = Table.AddColumn(Source, "Extract Words", each
List.First(
List.Transform(
ListPatterns , (n) => if Text.Contains([Words Pattern], n, Comparer.OrdinalIgnoreCase) then n else null
)
)
,
type text),
// Find pattern, return first found string, ignoring case-sensitivity.
Table1_Table1 = ReturnFoundString{[Item="Table1",Kind="Table"]}[Extract Words]
in
Table1_Table1

 

Once again, thank you so much for your help & I really appreciate it.

HI @joeywong ,

 

The error it is stating is that there's not a column called "Word Patterns" which you're attempting to modify or reference. I've changed to the column "Remarks" and this should work for your scenario: 

let

Source = Excel.Workbook(Web.Contents("https://smrtcorp-my.sharepoint.com/Documents/Desktop/Dylan%20Files/LBS%20settlement%20(working%20files)/Joyce%20share%20file/Words%20Patterns.xlsx"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
ListPatterns = List.Buffer(KeywordList),

// Find pattern, return first found string, ignoring case-sensitivity.
ReturnFoundString = Table.AddColumn(Table1_Table, "Extract Words", each
List.First(
    List.Transform(
        ListPatterns , (n) => if Text.Contains([Remarks], n, Comparer.OrdinalIgnoreCase) then n else null
        )
    ), type text
)

in ReturnFoundString

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.