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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
ruthe23
Frequent Visitor

Return a character before a delimiter but only if its a number else just return a 1.

I have a column that has product names and sometimes they contain #pk indicating they have a multipack.  To accurately count the # of products in the package I need to parse this column and find the # before "PK" and put it in a new column so I can calculate with it.  If the column contains a letter before PK as it does in some of the other parts of a product name ignore and put a 1 in the new column.  If the column doesn't contain any PK then also put a 1 in the column.  I have tried all sorts of Text.Contains, Value.Is, Text,End, Text.Beforedelimiter but I either get Errors, Or I get whatever is before the PK in some cases it's the # and sometimes it's a letter which screws up the new column.   I could probably do antoher step to get rid of the letters in the new column but I thought they might be away to do it all in one.  

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

You were close.  There is a very powerful function that allows you to define your very own transition rules.

 

lbendlin_0-1709341019390.png

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @ruthe23 ,

You can try this way:

 

let 
TextValue = [Column1],
PKIndex = Text.PositionOf(TextValue, "pk"),
CharBeforePK = if PKIndex > 0 then Text.Middle(TextValue, PKIndex - 1, 1) else null,
IsNumber = CharBeforePK <> null and List.Contains({"0".."9"}, CharBeforePK)
in 
if IsNumber then CharBeforePK else "1"

 

Use this M function to create a custom column:

vjunyantmsft_0-1709518346990.png


Here is the whole M function in Advanced Editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMirIVkhMTFSK1YlWSizIBtMmQDEos6CgIDsbwoSpMoRKGRoaAoUU0mFcI2PFguzEpGQwz9LSEqjTBCQXCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let 
TextValue = [Column1],
PKIndex = Text.PositionOf(TextValue, "pk"),
CharBeforePK = if PKIndex > 0 then Text.Middle(TextValue, PKIndex - 1, 1) else null,
IsNumber = CharBeforePK <> null and List.Contains({"0".."9"}, CharBeforePK)
in 
if IsNumber then CharBeforePK else "1")
in
    #"Added Custom"

 

NOTE: Since I'm not sure if your data will contain more than one "pk", I'll default to the first one that comes up.

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

lbendlin
Super User
Super User

You were close.  There is a very powerful function that allows you to define your very own transition rules.

 

lbendlin_0-1709341019390.png

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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