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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
FD5
Frequent Visitor

Replace values in a column if it contains a certain text

Hi everyone,

 

I hope that someone can help me solve my query as I am half way there I think!

 

I have merged two columns in a table but now I need to replace the values to make more sense of the descriptions. I don't want to go through the list in that column one by one as it is a long list but the description contains some key words that I could use to replace in bulk. Some examples below:

 

The list below, I want to replace them all with a word 'PFI':

Private Finance Initiative-Holding Company

Private Finance Initiative-Limited Company

Private Finance Initiative-Limited Partnership

Private Finance Initiative-Limited Liability Partnership

Private Finance Initiative-Partnership

 

I started working on a DAX but I am stuck. Here is my formula (my column header is Industry & Org Type):

= Table.ReplaceValue(#"Inserted Merged Column",
each [Industry & Org Type],
each if List.Contains(Private, [Industry & Org Type]) then "PFI" else [Industry & Org Type],
Replacer.ReplaceText,{"Industry & Org Type"})

 

Any idea where I am going wrong? Any help will be appreciated!

Many thanks

 

1 ACCEPTED SOLUTION
AntrikshSharma
Super User
Super User

@FD5 There are multiple ways to do this:

 

Single replacement:

let
    Source = Table,
    ReplacedValue = Table.ReplaceValue (
        Source,
        each [Column1],
        each if Text.Contains ( [Column1], "Private" ) then "PFI" else [Column1],
        Replacer.ReplaceValue,
        { "Column1" }
    )
in
    ReplacedValue

Single Replacement with custom function:

let
    Source = Table,
    ReplacedValue = Table.ReplaceValue (
        Source,
        each [Column1],
        each "PFI",
        ( OriginalText, Check, ReplacementText) =>
            if Check = true
            then ReplacementText
            else OriginalText,
        { "Column1" }
    )
in
    ReplacedValue

 

Multiple Replacements:

let
    Source = Table,
    AddedCustom = Table.AddColumn (
        Source,
        "Replacements",
        ( x ) =>
            List.Select (
                Replacements,
                ( y ) => Text.Contains ( x[Column1], y{0}, Comparer.OrdinalIgnoreCase )
            ){0}?{1}?
    )
in
    AddedCustom

 

Multiple Relacements with Table.TransformRows 

let
    Source = Table.FromRecords (
        Table.TransformRows (
            Table,
            ( x ) =>  [
                Column1 = x[Column1],
                NewText = List.Select (
                    Replacements,
                    ( y ) => Text.Contains ( x[Column1], y{0}, Comparer.OrdinalIgnoreCase )
                ){0}?{1}?
            ]
        )
    )
in
    Source

 

Refer to the file attached below:

 

 

View solution in original post

2 REPLIES 2
AntrikshSharma
Super User
Super User

@FD5 There are multiple ways to do this:

 

Single replacement:

let
    Source = Table,
    ReplacedValue = Table.ReplaceValue (
        Source,
        each [Column1],
        each if Text.Contains ( [Column1], "Private" ) then "PFI" else [Column1],
        Replacer.ReplaceValue,
        { "Column1" }
    )
in
    ReplacedValue

Single Replacement with custom function:

let
    Source = Table,
    ReplacedValue = Table.ReplaceValue (
        Source,
        each [Column1],
        each "PFI",
        ( OriginalText, Check, ReplacementText) =>
            if Check = true
            then ReplacementText
            else OriginalText,
        { "Column1" }
    )
in
    ReplacedValue

 

Multiple Replacements:

let
    Source = Table,
    AddedCustom = Table.AddColumn (
        Source,
        "Replacements",
        ( x ) =>
            List.Select (
                Replacements,
                ( y ) => Text.Contains ( x[Column1], y{0}, Comparer.OrdinalIgnoreCase )
            ){0}?{1}?
    )
in
    AddedCustom

 

Multiple Relacements with Table.TransformRows 

let
    Source = Table.FromRecords (
        Table.TransformRows (
            Table,
            ( x ) =>  [
                Column1 = x[Column1],
                NewText = List.Select (
                    Replacements,
                    ( y ) => Text.Contains ( x[Column1], y{0}, Comparer.OrdinalIgnoreCase )
                ){0}?{1}?
            ]
        )
    )
in
    Source

 

Refer to the file attached below:

 

 

Many thanks. I will have a go at this solution.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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.

Top Solution Authors