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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
onedayover
Helper III
Helper III

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"

 

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.