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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
davidleal
Frequent Visitor

Replace duplicated values with null in the same column per a given list of columns

I found several solutions for a similar problem, but it creates an additional column or does find and replace value which is not what I need. Let's say I have a Name column with the following values:

name1
name1
name2
name2
name1

and I would like to have the following output (the same previous column but with some values transformed):

name1
null
name2
null
null

In this question: Testing a query function to replace duplicated values with null, but I am getting errors  @wdx223_Daniel provided the following solution that returns a list with the expected output:

let
Source = Excel.CurrentWorkbook(){[Name="TB_source"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Amount", Int64.Type}}),
RemoveDups = (lst as list)=>List.Accumulate(lst,{},(x,y)=> x&{ if List.Contains(x,y) then null else y}),
replaceValues = RemoveDups(Table.Column(#"Changed Type", "Name"))
in
replaceValues

but I was not able to assign this list to the existing column Name. The question: Find and Replace text string in one table using value from another table does a find and replace, I was trying to customize it, but it doesn't work because for every value in the Find will be replaced wil null. Because it does a search (not by index position).

What I would need is to loop for all values of the a given column and replace with the corresponding value at the same index position of #"uniqueList". In other languages it would be a simple assignment: a=b, but I was not able to find a way in Power Query.

I was trying the following for example:

Table.ReplaceValue(#"Changed Type", each [Name], each replaceValues,Replacer.ReplaceValue,{"Name"})

but it return a the list of value on each row.

 

For the general case I would like to do the following transformation in #"Changed Type": For a list of selected columns replace  per column duplicated values with null

 

Thanks

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @davidleal ,
utilizing on what you have already, I would suggest the following approach:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45W8kvMTTVU0lFyziypBNGGBkqxOpjCRghhI6gwiDbGLmyCKWwMpE2BwrEA",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [Name = _t, City = _t, Amount = _t]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {{"Name", type text}, {"City", type text}, {"Amount", Int64.Type}}
    ),
    ListOfColumns = {"Name", "City"},
    RemainingColumns = List.Difference(Table.ColumnNames(#"Changed Type"), ListOfColumns),
    RemoveDups = (lst as list) =>
        List.Accumulate(lst, {}, (x, y) => x & {if List.Contains(x, y) then null else y}),
    replaceValues = List.Transform(ListOfColumns, each RemoveDups(Table.Column(#"Changed Type", _))),
    Custom1 = Table.FromColumns(
        replaceValues & Table.ToColumns(Table.SelectColumns(#"Changed Type", RemainingColumns)),
        ListOfColumns & RemainingColumns
    )
in
    Custom1



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

3 REPLIES 3
ImkeF
Community Champion
Community Champion

Hi @davidleal ,

not with the native Table.ReplaceValue or Table.TransformColumns- functions, as they don't support recursion.

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

davidleal
Frequent Visitor

Thanks, it works, so it looks like that the only we to do it is to recreate a new table again, but not replacing the existing column values with the new value list. 

ImkeF
Community Champion
Community Champion

Hi @davidleal ,
utilizing on what you have already, I would suggest the following approach:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45W8kvMTTVU0lFyziypBNGGBkqxOpjCRghhI6gwiDbGLmyCKWwMpE2BwrEA",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [Name = _t, City = _t, Amount = _t]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {{"Name", type text}, {"City", type text}, {"Amount", Int64.Type}}
    ),
    ListOfColumns = {"Name", "City"},
    RemainingColumns = List.Difference(Table.ColumnNames(#"Changed Type"), ListOfColumns),
    RemoveDups = (lst as list) =>
        List.Accumulate(lst, {}, (x, y) => x & {if List.Contains(x, y) then null else y}),
    replaceValues = List.Transform(ListOfColumns, each RemoveDups(Table.Column(#"Changed Type", _))),
    Custom1 = Table.FromColumns(
        replaceValues & Table.ToColumns(Table.SelectColumns(#"Changed Type", RemainingColumns)),
        ListOfColumns & RemainingColumns
    )
in
    Custom1



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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.