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

Testing a query function to replace duplicated values with null, but I am getting errors

I am trying to test a simple function that replace duplicated values with null in Name column:

let
Source = Excel.CurrentWorkbook(){[Name="TB_source"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Amount", Int64.Type}}),
RemoveDups = (aTable as table, colNames as text) =>
let
dupsNull = List.Generate(
()=>[v=aTable[colNames]{0}, idx=0],
each [idx]<Table.RowCount(aTable),
each [v=if List.PositionOf(aTable[colNames],aTable[colNames]{[idx]+1},Occurrence.First) = [idx]+1
then aTable[colNames]{[idx]+1} else null, idx=[idx]+1],each [v])
in
dupsNull,
#"listUnique" = RemoveDups(#"Changed Type", "Name")
in
#"listUnique"

A get a List but with errors:

davidleal_0-1666315744377.png

when I click on the error I am getting the following information:

davidleal_1-1666315785205.png

If I click on Go to Error I am getting the following:

davidleal_2-1666315826727.png

I was trying to define the ColNames input argument as a list but I was not able to make it work, I am getting erros of with different information.

 

I was able to make work a similar peace of code, but without invoking a function as follow:

let
Source = Excel.CurrentWorkbook(){[Name="TB_source"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Amount", Int64.Type}}),
dupsNull = List.Generate(
()=>[v=#"Changed Type"[Name]{0}, idx=0],
each [idx]<Table.RowCount(#"Changed Type"),
each [v=if List.PositionOf(#"Changed Type"[Name],#"Changed Type"[Name]{[idx]+1},Occurrence.First) = [idx]+1
then #"Changed Type"[Name]{[idx]+1} else null, idx=[idx]+1],
each [v])
in
dupsNull

and I am getting the expected result for the list:

davidleal_3-1666316825959.png

 

Any help is appreciated, please take into account I am new to Power Query and M.

 

Thanks,

 

David

 

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

try to change all "aTable[colNames]" to "Table.Column(aTable,colNames)"

or try this code

fx=(lst as list)=>List.Accumulate(lst,{},(x,y)=> x&{ if List.Contains(x,y) then null else y})

View solution in original post

2 REPLIES 2
davidleal
Frequent Visitor

Thanks @wdx223_Daniel it tested both options and both of them work. Thanks! Here the source code updated for further reference. In block comment the first option (larger):

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}),
/*
RemoveDups = (aTable as table, colNames as text) =>
let
dupsNull = List.Generate(
()=>[v=Table.Column(aTable,colNames){0}, idx=0],
each [idx]<Table.RowCount(aTable),
each [v=if List.PositionOf(Table.Column(aTable,colNames),
Table.Column(aTable,colNames){[idx]+1},Occurrence.First) = [idx]+1
then Table.Column(aTable,colNames){[idx]+1} else null, idx=[idx]+1],each [v])
in
dupsNull,
#"runiqueList" = RemoveDups(#"Changed Type", "Name")
*/
#"runiqueList" = RemoveDups(Table.Column(#"Changed Type","Name"))
in
#"runiqueList"

 Thanks again

wdx223_Daniel
Super User
Super User

try to change all "aTable[colNames]" to "Table.Column(aTable,colNames)"

or try this code

fx=(lst as list)=>List.Accumulate(lst,{},(x,y)=> x&{ if List.Contains(x,y) then null else y})

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.