How to create a loop to change newValue in Table.ReplaceValue function

Rettel98

New Member
Joined
Aug 11, 2023
Messages
5
Office Version
  1. 2021
Platform
  1. Windows
In main query table, there are a number of columns with null that need to be replaced with the name of the column. It can be done one by one, but I am trying to create one step that handles this.

So far I have this M code (2 of the column names have been placed as an example but the list of columns is longer):

Power Query:
List_nullCategories =
      {"Age", "Race"}


    FillDown_Categories =

      Table.ReplaceValue(Source,
        each [Age] = null,
        List_nullCategories{x},

        
        (currentValue, condition, newValue) =>
        if condition then newValue
        else currentValue,


        List_nullCategories
      )
in
    FillDown_Categories


I would like the x in List_nullCategories{x} to start off as a 0 and be replaced with the count of each list item until the end of the list.

Is there a way to wrap the whole Table.ReplaceValue function inside another function that loops through the list and changes the value of x? If there is a better way to do this iteration, then I am open to that as well.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi Rettel98,
try

Power Query:
List.Accumulate(Table.ColumnNames(Source), Source, (s,c)=> Table.TransformColumns(s, {{c, each Replacer.ReplaceValue(_,null, c)}}))

regards,
 
Upvote 0
Solution
Hi Rettel98,
try

Power Query:
List.Accumulate(Table.ColumnNames(Source),  Source, (s,c)=> Table.TransformColumns(s, {{c, each Replacer.ReplaceValue(_,null, c)}}))

regards,
Thanks, Alex! Your code did the trick. I replaced Table.ColumnNames() with my custom list to limit which columns had the null values replaced. I was trying to use the List. Accumulate function but didn't place it within the transform columns function. In the transform operation, is the extra set of curly brackets used because the column names (and their values) are gotten from a list?
 
Upvote 0
You 're right, the second set of curly brackets was not required!

Another way to solve your problem:

Power Query:
Table.TransformColumns(Source, List.Transform(Table.ColumnNames(Source), each let ColName = _ in {_, each Replacer.ReplaceValue(_,null,ColName)}))

Regards,
 
Upvote 0
You 're right, the second set of curly brackets was not required!

Another way to solve your problem:

Power Query:
Table.TransformColumns(Source, List.Transform(Table.ColumnNames(Source), each let ColName = _ in {_, each Replacer.ReplaceValue(_,null,ColName)}))

Regards,
Thanks, Alex. I'm going to stick with the first one because I understood that one instantly. I will have to study this second one a bit. :)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top