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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
bc2022
Frequent Visitor

How to update a cell value based on the conditional result of another column

I come across a situation which I need to update the cell value with the correspondent value of other column based on the conditions.

 

Here is an exmple:  

 

codecategoryvalue
abaa1
abde2
abcc5
abec5
abcd3
abfb 

 

I want to update the value of code abf with the correspondent value of category c which is 5. I was trying to use Table.ReplaceValue function but it seems somehow I need a column or variable to store the value (5) first. So I can use the following formula:

Table.ReplaceValue(#"Prior Step", each [value], each if [code] = "abf" then column/variable).

 

But I am not sure how to generate this column or variable. Or maybe I need to create a table which only has category C then merge it with the original one?

 

Any suggestions would be greatly appreciated.

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @bc2022 ,
you can do it without additional merging like so:

let
  Source = Table.Buffer( Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45WSkxKVNJRAmFDpVgdED8FyE4FYiMoPxnIBmFTKD8VjQ9ig/QYQ/lpQHYSECsoxcYCAA==", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [code = _t, category = _t, value = _t]
  )), 
  #"Replaced Value" = Table.ReplaceValue(
    Source, 
    each [value], 
    each 
      if [code] = "abf" then
        Table.SelectRows(Source, (x) => x[category] = "c"){0}[value]
      else
        [value], 
    Replacer.ReplaceValue, 
    {"value"}
  ), 
  #"Changed Type" = Table.TransformColumnTypes(
    #"Replaced Value", 
    {{"code", type text}, {"category", type text}, {"value", Int64.Type}}
  )
in
  #"Changed Type"

For performance reasons, buffering the Source-table is strongly recommended, as it will be referenced in each row of the new table during the replacement operation.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
ImkeF
Community Champion
Community Champion

Hi @bc2022 ,
you can do it without additional merging like so:

let
  Source = Table.Buffer( Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45WSkxKVNJRAmFDpVgdED8FyE4FYiMoPxnIBmFTKD8VjQ9ig/QYQ/lpQHYSECsoxcYCAA==", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [code = _t, category = _t, value = _t]
  )), 
  #"Replaced Value" = Table.ReplaceValue(
    Source, 
    each [value], 
    each 
      if [code] = "abf" then
        Table.SelectRows(Source, (x) => x[category] = "c"){0}[value]
      else
        [value], 
    Replacer.ReplaceValue, 
    {"value"}
  ), 
  #"Changed Type" = Table.TransformColumnTypes(
    #"Replaced Value", 
    {{"code", type text}, {"category", type text}, {"value", Int64.Type}}
  )
in
  #"Changed Type"

For performance reasons, buffering the Source-table is strongly recommended, as it will be referenced in each row of the new table during the replacement operation.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

bc2022
Frequent Visitor

Many thanks, @ImkeF!

 

Really appreciate your quick response.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Kudoed Authors