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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
mwild
Frequent Visitor

String Extract based on criteria

I have a table and one column contains free text.  I'm looking to add a column which will search the free text and extract anything which has a letter followed by 5 numbers.

 

For example - the below would be extracted:


D98765

d12345

D 54821

d 65411

This could be anywhere within the string of text.  Is this possible?

1 ACCEPTED SOLUTION
moizsherwani
Continued Contributor
Continued Contributor

@mwild  ***Made a tweak to the code***

The following works. I will let you tweak the code further if you need to so I am not doing all your homework 😂

 

StringMatch = 
VAR NoBlankFreeText =
    SUBSTITUTE ( 'Sample Data'[FreeText], " ", "", 1 )
VAR UpperD =
    FIND ( "D", NoBlankFreeText, 1, BLANK () )
VAR LowerD =
    FIND ( "d", NoBlankFreeText, 1, BLANK () )
VAR PositionD =
    IF ( ISNUMBER ( UpperD ), UpperD, LowerD)
VAR CheckNumbers =
    ISERROR (
        ISNUMBER ( VALUE ( MID ( NoBlankFreeText, PositionD + 1, 5 ) ) )
    )
RETURN 
    IF (
        NOT ( CheckNumbers ),
        MID ( NoBlankFreeText, PositionD, 6),
        "No Match"
    )

 

 

moizsherwani_0-1632236672765.png

 

 

Thanks,

Moiz
Was I able to answer your question? Mark my post as a solution to help others. Kudos if you liked the solution.

View solution in original post

7 REPLIES 7
mh2587
Super User
Super User

With combination of LEFT or RIGHT function


Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



mh2587
Super User
Super User

Or you can use CONTAINSSTRING function e.g 

MyCalculatedColumn = If(CONTAINSSTRING([TARGETCOLUMN];"searchforthis");TRUE();FALSE())

 


Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



moizsherwani
Continued Contributor
Continued Contributor

I am not sure how this will work when the end goal is "any letter followed by any numbers" when the letter and the numbers could be anything, e.g. it can be "Z 98765" or "b34561" or worse "!@#@!D48202@#!@".

Thanks,

Moiz
Was I able to answer your question? Mark my post as a solution to help others. Kudos if you liked the solution.

The letter will always be "D" or "d"
There may be a space (or may not be) and then 5 numbers between 0 and 9

moizsherwani
Continued Contributor
Continued Contributor

@mwild  ***Made a tweak to the code***

The following works. I will let you tweak the code further if you need to so I am not doing all your homework 😂

 

StringMatch = 
VAR NoBlankFreeText =
    SUBSTITUTE ( 'Sample Data'[FreeText], " ", "", 1 )
VAR UpperD =
    FIND ( "D", NoBlankFreeText, 1, BLANK () )
VAR LowerD =
    FIND ( "d", NoBlankFreeText, 1, BLANK () )
VAR PositionD =
    IF ( ISNUMBER ( UpperD ), UpperD, LowerD)
VAR CheckNumbers =
    ISERROR (
        ISNUMBER ( VALUE ( MID ( NoBlankFreeText, PositionD + 1, 5 ) ) )
    )
RETURN 
    IF (
        NOT ( CheckNumbers ),
        MID ( NoBlankFreeText, PositionD, 6),
        "No Match"
    )

 

 

moizsherwani_0-1632236672765.png

 

 

Thanks,

Moiz
Was I able to answer your question? Mark my post as a solution to help others. Kudos if you liked the solution.
mh2587
Super User
Super User

You can use CONTAIN function


Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



moizsherwani
Continued Contributor
Continued Contributor

You can do this using regular expression (using R or Python) in the query editor, Curbal here explains it really well

 

https://www.youtube.com/watch?v=9E3VsvFAge4

 

Regards,

 

Moiz

If this post helps, please "Accept" it as Solution to help other members find it.

Thanks,

Moiz
Was I able to answer your question? Mark my post as a solution to help others. Kudos if you liked the solution.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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