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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors