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

Extracting part of a string - before, after and in between characters

Hi

I have a table with a field "source_id". Can anyone help me with how to create a new column "new_source_id" extracting the data as it appears in the example below. Data will vary for each source id but this is pretty much all there is in terms of combinations:

source_idnew_source_id
delete_A1 
delete_EAI_A_1 
delete_sandbox 
01070107
0107-sandbox0107
0107-imports0107
0107-QTeachPD0107
0107-teaching0107
0107-profdev0107
delete_01070107
delete_0107-imports0107
delete_0107-sandbox0107
delete_0107-teaching0107
delete_0107-QTeachPD0107
23512351
2351-sandbox2351
2351-imports2351
2351-QTeachPD2351
2351-teaching2351
2351-profdev2351
delete_23512351
delete_2351-imports2351
delete_2351-sandbox2351
delete_2351-teaching2351
delete_2351-QTeachPD2351
A182A182
A182-sandboxA182
A182-importsA182
A182-QTeachPDA182
A182-teachingA182
delete_A182A182
delete_A182-importsA182
delete_A182-sandboxA182
delete_A182-teachingA182
delete_A182-QTeachPDA182
0018-0107-profdev0018-0107
A092-A092-profdevA092-A092

Thanks 🙂

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @onedayover 
If you wish to create a new column using dax you can use the following code (attached sample file)

1.png2.png

new_source_id = 
VAR Digits = SELECTCOLUMNS ( GENERATESERIES ( 0, 9, 1 ), "@Digit", [Value] & "" )
VAR String = Data[source_id]
VAR Items1 = SUBSTITUTE ( String, "_", "|" )
VAR Items2 = SUBSTITUTE ( Items1, "-", "|" )
VAR Length = PATHLENGTH ( Items2 )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "@Item", PATHITEM ( Items2, [Value] ) )
VAR T3 = 
    FILTER ( 
        T2,
        VAR Length2 = LEN ( [@Item] )
        VAR T4 = GENERATESERIES ( 1, Length2, 1 )
        VAR T5 = ADDCOLUMNS ( T4, "@Letter", MID ( [@Item], [Value], 1 ) )
        VAR T6 = FILTER ( T5, [@Letter] IN Digits )
        RETURN  
            COUNTROWS ( T6 ) > 1
    )
RETURN
    CONCATENATEX ( T3, [@Item], "-" )

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @onedayover 
If you wish to create a new column using dax you can use the following code (attached sample file)

1.png2.png

new_source_id = 
VAR Digits = SELECTCOLUMNS ( GENERATESERIES ( 0, 9, 1 ), "@Digit", [Value] & "" )
VAR String = Data[source_id]
VAR Items1 = SUBSTITUTE ( String, "_", "|" )
VAR Items2 = SUBSTITUTE ( Items1, "-", "|" )
VAR Length = PATHLENGTH ( Items2 )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "@Item", PATHITEM ( Items2, [Value] ) )
VAR T3 = 
    FILTER ( 
        T2,
        VAR Length2 = LEN ( [@Item] )
        VAR T4 = GENERATESERIES ( 1, Length2, 1 )
        VAR T5 = ADDCOLUMNS ( T4, "@Letter", MID ( [@Item], [Value], 1 ) )
        VAR T6 = FILTER ( T5, [@Letter] IN Digits )
        RETURN  
            COUNTROWS ( T6 ) > 1
    )
RETURN
    CONCATENATEX ( T3, [@Item], "-" )

Hi tamerj1, this is great work, thank you. Just one thing, how would I ensure that the leading zero isn't dropped in the new column? For example, row 4 the 107 should be 0107 as will all souce ids that start with 0.

Hi @onedayover 

leading zero is never dropped in this solution. Row 4 originally does not contain a leading zero. 

amitchandak
Super User
Super User

@onedayover , Try this code, paste this in a blank query in power bi, This will work only for numbers.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZJRC4IwFIX/Suw5YRqRPQ7qobeC3kTEcpVQKirRz0/NbefqhV4c9/vGztkwikSmn7rVifLFUixEvLRkrw6JSma4SYvsUn4slr7cdMOwmNlzmyjPX1VZt82Mn846vT6Ou5loe54X95mo6vKW6TfyseC0EGAuHzVTGzVXBj13i2C17p9wWMwMOZS7epTDwVRAIyrc81g+Np0WAszlo2Zqo+bKoOduofww6OZhMTPkUO7qUQ4HUwGNrLD/Ow0GzOWgZuqh/hPKtpXSD73pf23Y7z5yG3jDx+2wTMTxFw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [source_id = _t, new_source_id = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"source_id", type text}, {"new_source_id", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "source_id", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"source_id.1", "source_id.2", "source_id.3"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"source_id.1", type text}, {"source_id.2", type text}, {"source_id.3", type text}}),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "source_id.1", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"source_id.1.1", "source_id.1.2", "source_id.1.3", "source_id.1.4"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"source_id.1.1", type text}, {"source_id.1.2", type text}, {"source_id.1.3", type text}, {"source_id.1.4", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type2", "Custom", each Text.Combine ({if (try Number.FromText([source_id.1.1]) otherwise null) <> null then [source_id.1.1] else null ,
if (try Number.FromText([source_id.1.2]) otherwise null) <> null then [source_id.1.2] else null,
if (try Number.FromText([source_id.1.3]) otherwise null) <> null then [source_id.1.3] else null,
if (try Number.FromText([source_id.1.4]) otherwise null) <> null then [source_id.1.4] else null,
if (try Number.FromText([source_id.2]) otherwise null) <> null then [source_id.2] else null,
if (try Number.FromText([source_id.3]) otherwise null) <> null then [source_id.3] else null

}, "-"))
in
#"Added Custom"

 

 

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