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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
KG1
Resolver I
Resolver I

Extracting Numbers from Text

Hi

I need to be able to extract the order reference number from a text column

 

The description contains the order reference number I need

 

I have had a look at similar threads and found this solution 

 

= Text.Select([String],{"0".."9"})

 

Because there are more numbers in the description field this doesnt give me the result I need

 

Any help would be greatly appreciated - thank you in advance

 

DescriptionResultRequired Result
Road Closure 1234569 Any Lane HF1 9JA - Date 06/01/211234569190601211234569
Unplanned Road Closure 1234569 Nowhere Lane HF7 4JA1234569741234569
Emergency Road Closure 1234569 Somewhere Lane HF16 5JA12345691651234569
1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @KG1 ,

 

The whole M code is shown below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bcw7C4MwGIXhv3LI3KJJNMEx9EKR0qGlkziE+nkBTYqtiP++Ii4Fx/fAc7KM3b0tcGj9Z+gJXMgoVgmMm3C1jnA5cySpwR5H+yWEKgh5IDjLdxl7undrnaMCmx83P9Y09/qjEaVmcaeO+orca9p2D9/Rn+QK8UpLglAx6qEhSL1MWqGq5VwRmqEZWZ7/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Description", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Description", "Description - Copy"),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Duplicated Column", "Description - Copy", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Description - Copy.1", "Description - Copy.2", "Description - Copy.3", "Description - Copy.4", "Description - Copy.5", "Description - Copy.6", "Description - Copy.7"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Split Column by Character Transition",{"Description", "Description - Copy.1", "Description - Copy.2"}),
    #"Split Column by Character Transition1" = Table.SplitColumn(#"Removed Other Columns", "Description - Copy.2", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Description - Copy.2.1", "Description - Copy.2.2"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Split Column by Character Transition1",{"Description", "Description - Copy.2.1"})
in
    #"Removed Other Columns1"

Extract number.PNG

 

Actually, if the first string is always end with Closure, you could simply use this M syntax:

= Table.AddColumn(#"Changed Type", "Text Between Delimiters", each Text.BetweenDelimiters([Description], "Closure ", " "), type text)

Delimiters.PNG

Or  the length of order reference number is fixed——always 7 ,then you could use LEFT() to extract the Result like this:

LEFT.PNG

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-eqin-msft
Community Support
Community Support

Hi @KG1 ,

 

The whole M code is shown below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bcw7C4MwGIXhv3LI3KJJNMEx9EKR0qGlkziE+nkBTYqtiP++Ii4Fx/fAc7KM3b0tcGj9Z+gJXMgoVgmMm3C1jnA5cySpwR5H+yWEKgh5IDjLdxl7undrnaMCmx83P9Y09/qjEaVmcaeO+orca9p2D9/Rn+QK8UpLglAx6qEhSL1MWqGq5VwRmqEZWZ7/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Description", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Description", "Description - Copy"),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Duplicated Column", "Description - Copy", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Description - Copy.1", "Description - Copy.2", "Description - Copy.3", "Description - Copy.4", "Description - Copy.5", "Description - Copy.6", "Description - Copy.7"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Split Column by Character Transition",{"Description", "Description - Copy.1", "Description - Copy.2"}),
    #"Split Column by Character Transition1" = Table.SplitColumn(#"Removed Other Columns", "Description - Copy.2", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Description - Copy.2.1", "Description - Copy.2.2"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Split Column by Character Transition1",{"Description", "Description - Copy.2.1"})
in
    #"Removed Other Columns1"

Extract number.PNG

 

Actually, if the first string is always end with Closure, you could simply use this M syntax:

= Table.AddColumn(#"Changed Type", "Text Between Delimiters", each Text.BetweenDelimiters([Description], "Closure ", " "), type text)

Delimiters.PNG

Or  the length of order reference number is fixed——always 7 ,then you could use LEFT() to extract the Result like this:

LEFT.PNG

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

TomMartens
Super User
Super User

Hey @KG1 ,

 

you can use this M snippet, this M snippet creates a new column called "Custom".
Paste this snippet in your query using the Advanced Editor (if you never used the Advanced Editor before, make a copy of your pbix).
Close the last line above the final in with a comma.

Paste the code snippet above the last in in your query. 

As the step is called #"Added Custom", don't forget to use this name after the final in.

// the result column that contains the 1st number from the column description
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", 
        // passes the current row to an inline function
        (row) => 
        let 
            // splits the column description into a list, the items are separated by a space
            DescToList = Text.Split( row[Description] , " " ),
            // counts the items o the list
            NoOfItems = List.Count( DescToList ) - 1,
            // extracts the first number of the list of numbers
            TheFirstNumber =
                // removes the nulls from the inner list
                List.RemoveNulls(
                    // the inner, an interation across the items  
                    List.Generate(
                        () => [x  = 0 , y = try Number.From( DescToList{ 0 } ) otherwise null ]
                        , each [x] < NoOfItems
                        , each [x = [x] + 1 , y = try Number.From( DescToList{ [x] + 1 } ) otherwise null ]
                        , each [y]
                    )
                ){0}
            in TheFirstNumber
    )
in
    #"Added Custom"

Here is a screenshot of the result based on the sample data you provided:

image.png

Hopefully, this is what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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