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
Petjak
New Member

Rename content of a column if a certain content in another column exists

A little tricky Subject Headline... Since im quite new to the Power BI game. I'd like to know if there is a way to rename contents of for example column "b" if a certain content in column "a" exists. Somehow i dont find a fitting solution with the Query Editor. 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Yes, Table.Replace value is used for this. An example below

=Table.ReplaceValue(#"Changed Type",each [b],each if [a] ="abc" then "x" else [b],Replacer.ReplaceValue,{"b"})

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKk8tUtJRKihQitWJVkpMSgZyCgvBnPKSysoSILeoCEmuuFgpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [a = _t, b = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"a", type text}, {"b", type text}}),
    Custom1 = Table.ReplaceValue(#"Changed Type",each [b],each if [a] ="abc" then "x" else [b],Replacer.ReplaceValue,{"b"})
in
    Custom1

View solution in original post

2 REPLIES 2
Vijay_A_Verma
Super User
Super User

Yes, Table.Replace value is used for this. An example below

=Table.ReplaceValue(#"Changed Type",each [b],each if [a] ="abc" then "x" else [b],Replacer.ReplaceValue,{"b"})

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKk8tUtJRKihQitWJVkpMSgZyCgvBnPKSysoSILeoCEmuuFgpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [a = _t, b = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"a", type text}, {"b", type text}}),
    Custom1 = Table.ReplaceValue(#"Changed Type",each [b],each if [a] ="abc" then "x" else [b],Replacer.ReplaceValue,{"b"})
in
    Custom1
ronrsnfld
Super User
Super User

You can do it but you have to get into the Advanced Editor.

This code provides an example of one way to do this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxKVtJRMlSK1YlWSklNA7KNwOz0jEwg2xjMzsrOAbJNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [a = _t, b = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"a", type text}, {"b", Int64.Type}}),

    #"Look for in col A" = "ghi",
    #"Conditional Rename" = if List.Contains(#"Changed Type"[a],#"Look for in col A") 
        then Table.RenameColumns(#"Changed Type",{"b","New Col Name"}) 
        else #"Changed Type"

in
    #"Conditional Rename"

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
Top Kudoed Authors