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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
rchamberlain
Frequent Visitor

Create appended calculated column values based on specific text strings in another column

Hello -

 

I am seeking help on creating an appended calculated column (Column B) using data from another column's (Column A) values.

 

I need a way to search a column for a specific text string (I.e. "Dep_" in below example) and add what follows it in the new column.  However, in many cases there could be more than one instance of that string value.  If this happens, I would like to seperate them by comma's as noted below.

 

Also, it needs to work independent of case (I.e. "Dep_" or "dep_").

 

Example

Column AColumn B (New)
Dep_Orange, Pear, Dep_Apple, GrapOrange, Apple
Dep_Grape, Apple, Orange, Dep_PearGrape, Pear
Apple, Orange, Grape, Pear 
Dep_Pear, Grape, Orange, ApplePear

 

This may be super easy for you super users so any help is appreciated!

 

Thanks,

Rich

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @rchamberlain ,

You can achieve it by creating a calculated column:

Column B = 
VAR TextString = "Dep_"
VAR ColumnAValues =
    SUBSTITUTE ( [Column A], TextString, TextString )
VAR ExtractedValues =
    CONCATENATEX (
        FILTER (
            ADDCOLUMNS (
                GENERATESERIES ( 1, LEN ( ColumnAValues ) - LEN ( TextString ) + 1 ),
                "Substring", MID ( ColumnAValues, [Value], LEN ( TextString ) ),
                "FollowingText",
                    MID (
                        ColumnAValues,
                        [Value] + LEN ( TextString ),
                        FIND ( ",", ColumnAValues & ",", [Value] + LEN ( TextString ) )
                            - ( [Value] + LEN ( TextString ) )
                    )
            ),
            [Substring] = TextString
        ),
        [FollowingText],
        ", "
    )
RETURN
    IF ( ExtractedValues = "", BLANK (), ExtractedValues )

vyiruanmsft_1-1739416548766.png

Best Regards

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @rchamberlain ,

You can achieve it by creating a calculated column:

Column B = 
VAR TextString = "Dep_"
VAR ColumnAValues =
    SUBSTITUTE ( [Column A], TextString, TextString )
VAR ExtractedValues =
    CONCATENATEX (
        FILTER (
            ADDCOLUMNS (
                GENERATESERIES ( 1, LEN ( ColumnAValues ) - LEN ( TextString ) + 1 ),
                "Substring", MID ( ColumnAValues, [Value], LEN ( TextString ) ),
                "FollowingText",
                    MID (
                        ColumnAValues,
                        [Value] + LEN ( TextString ),
                        FIND ( ",", ColumnAValues & ",", [Value] + LEN ( TextString ) )
                            - ( [Value] + LEN ( TextString ) )
                    )
            ),
            [Substring] = TextString
        ),
        [FollowingText],
        ", "
    )
RETURN
    IF ( ExtractedValues = "", BLANK (), ExtractedValues )

vyiruanmsft_1-1739416548766.png

Best Regards

Thank you!  This works exactly as I need.

 

I'm not as familiar with Powerquery at this time, but I am sure the other solutions would have worked as well.

Anonymous
Not applicable

Hi @rchamberlain ,

You can add a custom column in Power Query Editor to get it:

= Text.Combine(
    List.Transform(
        List.Select(
            Text.Split([Column A], ", "), 
            each Text.StartsWith(Text.Lower(_), "dep_")
        ), 
        each Text.AfterDelimiter(_, "_")
    ), 
    ", "
)

vyiruanmsft_0-1739414062278.png

The full applied codes as below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckktiPcvSsxLT9VRCEhNLNJRAIk4FhTkAAXcixILlGJ1IKpAHKAYVAqmByQD0gdWhiYH1QGXhqmFy8AUgvUpxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column A", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Column B (New)", each Text.Combine(
    List.Transform(
        List.Select(
            Text.Split([Column A], ", "), 
            each Text.StartsWith(Text.Lower(_), "dep_")
        ), 
        each Text.AfterDelimiter(_, "_")
    ), 
    ", "
))
in
    #"Added Custom"

Best Regards

hnguy71
Super User
Super User

Hi @rchamberlain

This is easier to achieve in PowerQuery. I'll provide a solution for that:


Essentially, you would need to create a list of values that you want to search and extract from:

hnguy71_0-1739396453390.png


Next, you would call that list in your main table and build a new custom table from that using the following applied steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckktiPcvSsxLT9VRCEhNLNJRAIk4FhTkAAXcixILUpVidSDKwDwdBagcTBNIBqQRrAxNDqoDLg1TC5eBKQTrU4qNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t]),

    // Build a list of strings to search for...
    GetList = List[Value],

    // Let's create a function to extract matching values
    fxExtractValues = 
    
    (txt as text) as text =>
        let
            // Split the original text into a list
            Items = Text.Split(txt, ", "),
            
            // Keep only items with string that starts with value within list of matching values
            MatchingItems = 
                List.Select(
                    Items, each List.AnyTrue(List.Transform(GetList, (p) => Text.StartsWith(_, p)))
                ),
            
            // Remove the prefix from each matching value
            CleanedItems = 
                List.Transform(
                    MatchingItems, each List.First(List.RemoveFirstN(Text.Split(_, "_"), 1))
                ),

            // Combine the results back into a single text string
            Result = Text.Combine(CleanedItems, ", ")
        in
            Result,

    ExtractValues = Table.AddColumn(Source, "Column B", each fxExtractValues([Column A]), type text)
in
    ExtractValues

 
and if done correctly, it should net you the following results:

hnguy71_1-1739396554959.png

 


I have included a sample pbix for you to follow along and reference in your actual model.



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.