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
Doro
Frequent Visitor

replace one column value based on other column criteria

Hello, need help with my data how can I replace all cels to "d11" value in column Goods that have positive Quantity and starts not from "d"? Thank you in advance!

QuantityGoods
1b
0q1
0w
0s
0d
0s4
200d1
0a
0d
0f4
0a
300d2
0v
0b4
0s
302d4
0b
0s4
300s
0b
0b
300d1
0b
0e4
0e
153d3
0o3
0l
0k
150c1
1 ACCEPTED SOLUTION
tackytechtom
Super User
Super User

Hi @Doro ,

 

How about this:

tomfox_0-1645045506582.png


There are two ways of doing it:

a) You replace every value in [Goods], use this code

= Table.ReplaceValue(#"Changed Type",each [Goods], each if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods],Replacer.ReplaceText,{"Goods"})

 

The code in advanced editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY5LDoAgDAXv0jULSvE0hIUobjQxhkSvLx/TGsJu8jp9rXOAoCCAVw50pgsZH6bEtEpmKxpdY9maB+5muzG1LcPxzRRsd5a0Ka7Eof+htaWBEP7XcCBEqY2VcKLiEsen4MG0f27hJff6Fw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Quantity = _t, Goods = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Quantity", Int64.Type}, {"Goods", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Goods], each if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods],Replacer.ReplaceText,{"Goods"})
in
    #"Replaced Value"

 

b) Create a custom column, drop the old column and rename the new one to Goods. The custom column uses this code bit:

if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods]

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY5LDoAgDAXv0jULSvE0hIUobjQxhkSvLx/TGsJu8jp9rXOAoCCAVw50pgsZH6bEtEpmKxpdY9maB+5muzG1LcPxzRRsd5a0Ka7Eof+htaWBEP7XcCBEqY2VcKLiEsen4MG0f27hJff6Fw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Quantity = _t, Goods = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Quantity", Int64.Type}, {"Goods", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Goods"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Goods"}})
in
    #"Renamed Columns"

 

Does this one work for you? 🙂 

 

/Tom

https://www.tackytech.blog

https://www.instagram.com/tackytechtom

 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

6 REPLIES 6
tackytechtom
Super User
Super User

Hi @Doro ,

 

How about this:

tomfox_0-1645045506582.png


There are two ways of doing it:

a) You replace every value in [Goods], use this code

= Table.ReplaceValue(#"Changed Type",each [Goods], each if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods],Replacer.ReplaceText,{"Goods"})

 

The code in advanced editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY5LDoAgDAXv0jULSvE0hIUobjQxhkSvLx/TGsJu8jp9rXOAoCCAVw50pgsZH6bEtEpmKxpdY9maB+5muzG1LcPxzRRsd5a0Ka7Eof+htaWBEP7XcCBEqY2VcKLiEsen4MG0f27hJff6Fw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Quantity = _t, Goods = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Quantity", Int64.Type}, {"Goods", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Goods], each if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods],Replacer.ReplaceText,{"Goods"})
in
    #"Replaced Value"

 

b) Create a custom column, drop the old column and rename the new one to Goods. The custom column uses this code bit:

if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods]

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY5LDoAgDAXv0jULSvE0hIUobjQxhkSvLx/TGsJu8jp9rXOAoCCAVw50pgsZH6bEtEpmKxpdY9maB+5muzG1LcPxzRRsd5a0Ka7Eof+htaWBEP7XcCBEqY2VcKLiEsen4MG0f27hJff6Fw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Quantity = _t, Goods = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Quantity", Int64.Type}, {"Goods", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Quantity] > 0 and not Text.StartsWith([Goods], "d") then "d11"  else [Goods]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Goods"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Goods"}})
in
    #"Renamed Columns"

 

Does this one work for you? 🙂 

 

/Tom

https://www.tackytech.blog

https://www.instagram.com/tackytechtom

 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Hello again , and what if i would like to replace all that not starts from "d" and "d1"? 

Hi @Doro 

 

Please share some sample data and expected result based on your new requirement. I think values that don't start from "d" already include values that don't start from "d1". And what is the new value to replace them? Do we need to take positive Quantity into account?

 

Best Regards,
Community Support Team _ Jing

Hello, hope you ingood mood and health, well basicaly sample of data same as in top of post, but task slightly diffrent, replace all cels to "d11" value in column `Goods` that have positive `Quantity` and starts not from "d" and  "d1"...

Hi @Doro 

 

I think the current solution already meets your need. Goods that don't start from "d" include goods that don't start from "d1". So you don't need to modify it. Based on the sample data, only values in the highlighted rows below need to be replaced, right? 

vjingzhang_1-1645693687539.png

 

Regards,

Jing

Thank you for quick reply and your rime, work flawless!

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