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
Danielnir
Helper II
Helper II

Extract values from a text, based on condition

Hi,

 

I have a dataset with item descriptions. Within those, there is information about volumes. How to extract just them? 

Sample photo showing how the description works:

 

Danielnir_0-1660218170760.png

 

As you can see, volume data can be placed randomly within the descriptions. Also, it can be "L" for litres instead of "ml" but it's optional to include in the solution. It might be a calculated column written in Dax if there's no other option.

 

May I ask for your assistance?

2 REPLIES 2
Vijay_A_Verma
Super User
Super User

Assuming the column name is Data, use below formula in a custom column

= List.Select(Text.Split([Data]," "),(x)=> try Value.Is(Number.From(Text.Split(Text.Replace(Text.Lower(x),"ml","l"),"l"){0}), type number) otherwise false and Text.Split(Text.Replace(Text.Lower(x),"ml","l"),"l"){1}=""){0}

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs5JTcwrTlVwS0xOVQhPLM5QMDIwyM1R8A9yVYrViVbySMxLUXAuSk3MVTAFioPFggPcjA0UfPJLMvPzwKIKugpu/kEKvv4uYPlksHIjM3MfhZLyUrCQhamBr48CWEIpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.Select(Text.Split([Data]," "),(x)=> try Value.Is(Number.From(Text.Split(Text.Replace(Text.Lower(x),"ml","l"),"l"){0}), type number) otherwise false and Text.Split(Text.Replace(Text.Lower(x),"ml","l"),"l"){1}=""){0}, type text)
in
    #"Added Custom"

 

I'd recommend using List.First instead of {0} to avoid errors if the list is empty.

 

Here's a slightly simpler version:

List.First(
    List.Select(
        Text.Split([Data]," "), each
            try Value.Is(Number.From(Text.Remove(Text.Lower(_), {"m","l"})), type number)
            otherwise false
    )
)

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.

Top Solution Authors
Top Kudoed Authors