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
Anonymous
Not applicable

Replace value in each row based on text value of another column

Hello,

I have the following table in Power Query and i am trying to add a step to update the values in Paid amount - Original and Paid amount - Fixed columns based on the following conditions, for each row:

1. If the Invoice value is "No", then change the Paid amount - Original and Paid amount - Fixed values to negative.

2. If the Invoice value is "Yes", then no change.

Any help is much appreciated!

 

Document IDInvoicePaid amount - OriginalPaid amount - FixedUpdate DateProcess StatusStatus Code
3998Yes04619531.2803/01/2022Processed On Time1
4050Yes017852.402/01/2023Processed On Time1
4050Yes6949.156949.1509/01/2023Not Processed3
4060No757.16757.1610/01/2022Not Processed3
4139Yes4598459817/01/2022Not Processed3
4145Yes142854.11142854.1116/01/2023Not Processed3
4149No0104557.0624/01/2022Processed Late2
4157No428274282731/01/2022Not Processed3
4160Yes393.12393.1207/02/2022Not Processed3
4160Yes2840.22840.214/02/2022Not Processed3
4164Yes1220122021/02/2022Not Processed3
4166Yes206002060028/02/2022Not Processed3
4187Yes13423.8513423.8507/03/2022Not Processed3
4187Yes522.89522.8914/03/2022Processed On Time1
4187Yes467.69467.6921/03/2022Processed On Time1
4187Yes12012028/03/2022Processed On Time1
4248Yes63663604/04/2022Processed On Time1
4314Yes80080011/04/2022Processed On Time1
4474Yes1660166018/04/2022Not Processed3
4481Yes20893620893625/04/2022Processed On Time1
4487Yes1292129202/05/2022Processed On Time1
4495Yes1292129209/05/2022Processed On Time1
4685Yes542.05542.0516/05/2022Processed On Time1
4688Yes235952359523/05/2022Not Processed3
4691Yes916861.32916861.3230/05/2022Not Processed3
4813Yes185581.42185581.4206/06/2022Processed On Time1
4832Yes515503.82515503.8213/06/2022Processed On Time1
4997Yes766120.66766120.6620/06/2022Processed On Time1
1 ACCEPTED SOLUTION

Hi @Anonymous ,

According to your description, you can do it in two steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZTNblshEIVfxbrriMw/M+8QJVlkU1lZVK0XXbSRYr+/AuTaYKuW8cLM4VrzwZwBttuFI3x5WH7s9mWE8hPDUMZE9TPwI+AjAVGZvH5+/Nrt97vfm5d/m7c/f3flGy7vD9tFQOEMgtmVktQZrQSeJ1hIJNQzBTFwnj8OmxOrzHllGLQ/y5A1J7RRIAylXAEgx2kTos2YNWCeyBY9ZaOQqyTES223y0CJYxnNSxAtRUAtguS//Xj6eahW0pqv+ZhfVqY8RMaJMqx3goMT0iigGEF3EMgFEo0CZYYg3Uoi6IFwJtv6+uVQwBh9It9zX52FOLmey+oC30FRouQxiuoCT9yskSKWk8Uoqhv3UnC18+jGTD5JfySM7TRCqUIm8hl7P7314XtEnMuXPJwHa8frGHwgXOmEOA7nwaNtvQud3MOZh0E91DdOZwih1wgxRzDvBBVKoKOoj8scpXeTWNuuehwYV/y06H4GmhsmpgvNcJvjyN0PV3VMQhcaSk02UZO3VVdnUBXKTaULjTzHiuidzmblqqT2ooya4Dbr/Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Document ID" = _t, Invoice = _t, #"Paid amount - Original" = _t, #"Paid amount - Fixed" = _t, #"Update Date" = _t, #"Process Status" = _t, #"Status Code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Document ID", Int64.Type}, {"Invoice", type text}, {"Paid amount - Original", type number}, {"Paid amount - Fixed", type number}, {"Update Date", type text}, {"Process Status", type text}, {"Status Code", Int64.Type}}),
    #"ReplaceValue"= Table.ReplaceValue(#"Changed Type", each [#"Paid amount - Original"], each if [Invoice]="Yes" then [#"Paid amount - Original"] else[#"Paid amount - Original"]*-1, Replacer.ReplaceValue,{"Paid amount - Original"}),
    #"ReplaceValue2"=Table.ReplaceValue(#"ReplaceValue", each [#"Paid amount - Fixed"], each if [Invoice]="Yes" then [#"Paid amount - Fixed"] else[#"Paid amount - Fixed"]*-1, Replacer.ReplaceValue,{"Paid amount - Fixed"})
in
    #"ReplaceValue2"

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

According to your description, here's my solution.

Table.ReplaceValue(#"Changed Type", each [#"Paid amount - Original"], each if [Invoice]="Yes" then [#"Paid amount - Original"] else[#"Paid amount - Original"]*-1, Replacer.ReplaceValue,{"Paid amount - Original"})

Here's the whole code of my sample, you can copy-paste it in a blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZTNblshEIVfxbrriMw/M+8QJVlkU1lZVK0XXbSRYr+/AuTaYKuW8cLM4VrzwZwBttuFI3x5WH7s9mWE8hPDUMZE9TPwI+AjAVGZvH5+/Nrt97vfm5d/m7c/f3flGy7vD9tFQOEMgtmVktQZrQSeJ1hIJNQzBTFwnj8OmxOrzHllGLQ/y5A1J7RRIAylXAEgx2kTos2YNWCeyBY9ZaOQqyTES223y0CJYxnNSxAtRUAtguS//Xj6eahW0pqv+ZhfVqY8RMaJMqx3goMT0iigGEF3EMgFEo0CZYYg3Uoi6IFwJtv6+uVQwBh9It9zX52FOLmey+oC30FRouQxiuoCT9yskSKWk8Uoqhv3UnC18+jGTD5JfySM7TRCqUIm8hl7P7314XtEnMuXPJwHa8frGHwgXOmEOA7nwaNtvQud3MOZh0E91DdOZwih1wgxRzDvBBVKoKOoj8scpXeTWNuuehwYV/y06H4GmhsmpgvNcJvjyN0PV3VMQhcaSk02UZO3VVdnUBXKTaULjTzHiuidzmblqqT2ooya4Dbr/Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Document ID" = _t, Invoice = _t, #"Paid amount - Original" = _t, #"Paid amount - Fixed" = _t, #"Update Date" = _t, #"Process Status" = _t, #"Status Code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Document ID", Int64.Type}, {"Invoice", type text}, {"Paid amount - Original", type number}, {"Paid amount - Fixed", type number}, {"Update Date", type text}, {"Process Status", type text}, {"Status Code", Int64.Type}}),
    #"New"= Table.ReplaceValue(#"Changed Type", each [#"Paid amount - Original"], each if [Invoice]="Yes" then [#"Paid amount - Original"] else[#"Paid amount - Original"]*-1, Replacer.ReplaceValue,{"Paid amount - Original"})
in
    #"New"

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

@v-yanjiang-msft Thanks for your reply! Is it possible to edit the code to allow both the Paid amount - Original and Paid amount - Fixed values to change based on the Invoice text?

 

Here is my attempt but it is not working:

= Table.ReplaceValue(#"Changed Type", each [#"Paid amount - Original"] && [#"Paid amount - Fixed"], each if [Invoice]="Yes" then [#"Paid amount - Original"] && [#"Paid amount - Fixed"] else[#"Paid amount - Original"]*-1 && [#"Paid amount - Fixed"]*-1, Replacer.ReplaceValue,{"Paid amount - Original", "Paid amount - Fixed"})

 

Hi @Anonymous ,

According to your description, you can do it in two steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZTNblshEIVfxbrriMw/M+8QJVlkU1lZVK0XXbSRYr+/AuTaYKuW8cLM4VrzwZwBttuFI3x5WH7s9mWE8hPDUMZE9TPwI+AjAVGZvH5+/Nrt97vfm5d/m7c/f3flGy7vD9tFQOEMgtmVktQZrQSeJ1hIJNQzBTFwnj8OmxOrzHllGLQ/y5A1J7RRIAylXAEgx2kTos2YNWCeyBY9ZaOQqyTES223y0CJYxnNSxAtRUAtguS//Xj6eahW0pqv+ZhfVqY8RMaJMqx3goMT0iigGEF3EMgFEo0CZYYg3Uoi6IFwJtv6+uVQwBh9It9zX52FOLmey+oC30FRouQxiuoCT9yskSKWk8Uoqhv3UnC18+jGTD5JfySM7TRCqUIm8hl7P7314XtEnMuXPJwHa8frGHwgXOmEOA7nwaNtvQud3MOZh0E91DdOZwih1wgxRzDvBBVKoKOoj8scpXeTWNuuehwYV/y06H4GmhsmpgvNcJvjyN0PV3VMQhcaSk02UZO3VVdnUBXKTaULjTzHiuidzmblqqT2ooya4Dbr/Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Document ID" = _t, Invoice = _t, #"Paid amount - Original" = _t, #"Paid amount - Fixed" = _t, #"Update Date" = _t, #"Process Status" = _t, #"Status Code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Document ID", Int64.Type}, {"Invoice", type text}, {"Paid amount - Original", type number}, {"Paid amount - Fixed", type number}, {"Update Date", type text}, {"Process Status", type text}, {"Status Code", Int64.Type}}),
    #"ReplaceValue"= Table.ReplaceValue(#"Changed Type", each [#"Paid amount - Original"], each if [Invoice]="Yes" then [#"Paid amount - Original"] else[#"Paid amount - Original"]*-1, Replacer.ReplaceValue,{"Paid amount - Original"}),
    #"ReplaceValue2"=Table.ReplaceValue(#"ReplaceValue", each [#"Paid amount - Fixed"], each if [Invoice]="Yes" then [#"Paid amount - Fixed"] else[#"Paid amount - Fixed"]*-1, Replacer.ReplaceValue,{"Paid amount - Fixed"})
in
    #"ReplaceValue2"

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Chew_WenJie
Resolver II
Resolver II

Hello, you may create a conditional column which
                    IF Invoice = "No" then -1 else if Invoice = "Yes" then 1.

after that just use the Paid amount - Original and Paid amount - Fixed values multiply with the conditional column.

 

hope this can help you.

Anonymous
Not applicable

@Chew_WenJie Thanks for your reply!

I was hoping for one or more steps to fix the Paid amount - Original and Paid amount - Fixed values without duplicating these columns. Is that possible?

Hihi, you can multiply the Paid amount - Original & Paid amount - Fixed and delete the orginal column and replace the name to the new column, if you wish to make modifications to the column and it cannot be transform by one step, you must create a new column.

You may wait for other power bi specialists to come up with a better method for making the calculation.

 

Hope this helpful to you.

Anonymous
Not applicable

I tried this step but it is giving an error:

 

 

= Table.ReplaceValue(#"Changed Type", each [Paid amount - Original], each if [Invoice] = "No" then -[Paid amount - Original] else [Paid amount - Original], Replacer.ReplaceValue, {"Paid amount - Original"})

 

 

MakeItReal_0-1661861823174.png

Is it possible to do the changes for both columns in the same step?

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