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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Michael_HERE
Regular Visitor

How to replace a value in multiple columns when the columns have to be dynamically selected?

I need to replace a value in a set of columns (in this case null value with 0), but I cannot hard-code the columns. Instead, I need to determine the columns dynamically: only columns with a specific name pattern (e.g. starting with "Invoiced_") shall be considered.

 

I tried to pass the list of columns to the ReplaceValue function, but somehow my code doesn't work.  

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FilteredList = List.Select(Table.ColumnNames(Table1), each Text.StartsWith( _ , "Invoiced_")),
    #"Replaced Value" = Table.ReplaceValue(Table1,null,0,Replacer.ReplaceValue, FilteredList)
in
    #"Replaced Value"

 

 

I get this error: 

Expression.Error: A cyclic reference was encountered during evaluation.

 

What did I do wrong? Or how can I solve this problem? 

Thanks  

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FilteredList = List.Buffer(List.Select(Table.ColumnNames(Source), each Text.StartsWith( _ , "Invoiced_"))),
    #"Replaced Value" = Table.ReplaceValue(Source,null,0,Replacer.ReplaceValue, FilteredList)
in
    #"Replaced Value"

View solution in original post

6 REPLIES 6
wdx223_Daniel
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FilteredList = List.Buffer(List.Select(Table.ColumnNames(Source), each Text.StartsWith( _ , "Invoiced_"))),
    #"Replaced Value" = Table.ReplaceValue(Source,null,0,Replacer.ReplaceValue, FilteredList)
in
    #"Replaced Value"

Wow, this worked. Thank you so much! 
If I'm reading this right, the trick was to create a new List instance, right? 

guess your code error came from the reference of Table1,

i just changed it to Source

Vijay_A_Verma
Super User
Super User

Use this

    let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FilteredList = List.Select(Table.ColumnNames(Table1), each Text.StartsWith( _ , "Invoiced_")),
    #"Replaced Value" = Table.FromRecords(
    Table.TransformRows(Source, (r) => List.Accumulate(FilteredList, r, (s,c)=> 
        Record.TransformFields(s,{{c, each if _ = null then 0 else _}})))
        , Value.Type(Source)))
in
    #"Replaced Value"

Unfortunately, I get the same error. It occurs already in the second line. 

Maybe I should have mentioned that I am using PowerQuery within Excel, not Power BI (if that matters). 

Besides, the last closing parenthesis in your code is too much. 

Is it possible to post an Excel file having 2-3 rows of dummy data to Onedrive / Google drive and share the link here?

PQ in Excel or Power BI behave the same so that won't make a difference.

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!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

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

Top Solution Authors