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
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
Super User
Super User

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
Super User
Super User

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
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