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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Aasozab
Regular Visitor

How to delete only the first occurrence of a value in a column

How to delete only the first occurrence of a value in a column

Screenshot_3.png

2 ACCEPTED SOLUTIONS

You can do this via M but the results will be very surprising, and I am sure this is not what you actually need.

 

 

let
    Source = {1..100},
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each Character.FromNumber(65+Int32.From(Number.Random()*26))),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Custom"}, {{"Rows", each Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type), type table [Column1=number, Custom=text, Index=Int64.Type]}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Column1", "Index"}, {"Column1", "Index"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Rows",each [Custom],each if [Index]=0 then null else [Custom],Replacer.ReplaceValue,{"Custom"}),
    #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"Column1", Order.Ascending}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows",{"Custom"})
in
    #"Removed Other Columns"

 

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

View solution in original post

dufoq3
Super User
Super User

Hi @Aasozab, check this:

 

dufoq3_0-1714418906599.png

 

You have to define column to replace here:

dufoq3_1-1714418945315.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxU0lEyVIrVgTKNwMykJCDTGME0QTBNwczkZCDTDME0RzAtEExLpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    ColumnToReplace = "Column1",
    GroupedRows = Table.Group(Source, {ColumnToReplace}, {{"All", each 
        [ a = Table.AddIndexColumn(_, "Index", 0, 1),
          b = Table.ReplaceValue(a, each [Index] = 0, each null, (x,y,z)=> if y then z else x, {ColumnToReplace}),
          c = Value.ReplaceType(b, Value.Type(a)),
          d = Table.RemoveColumns(c, {"Index"})
        ][d], type table}}),
    All = Table.Combine(GroupedRows[All])
in
    All

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

4 REPLIES 4
dufoq3
Super User
Super User

Hi @Aasozab, check this:

 

dufoq3_0-1714418906599.png

 

You have to define column to replace here:

dufoq3_1-1714418945315.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxU0lEyVIrVgTKNwMykJCDTGME0QTBNwczkZCDTDME0RzAtEExLpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    ColumnToReplace = "Column1",
    GroupedRows = Table.Group(Source, {ColumnToReplace}, {{"All", each 
        [ a = Table.AddIndexColumn(_, "Index", 0, 1),
          b = Table.ReplaceValue(a, each [Index] = 0, each null, (x,y,z)=> if y then z else x, {ColumnToReplace}),
          c = Value.ReplaceType(b, Value.Type(a)),
          d = Table.RemoveColumns(c, {"Index"})
        ][d], type table}}),
    All = Table.Combine(GroupedRows[All])
in
    All

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

lbendlin
Super User
Super User

why not delete row 147 too?

Actually, my question should be how to replace only the first occurrence of a value of a column? It doesn't matter to replace row 147 or row 169. Basic question is can we replace only first ocuurence or any number of occurrence of a value of a column using GUI options or M code?

You can do this via M but the results will be very surprising, and I am sure this is not what you actually need.

 

 

let
    Source = {1..100},
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each Character.FromNumber(65+Int32.From(Number.Random()*26))),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Custom"}, {{"Rows", each Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type), type table [Column1=number, Custom=text, Index=Int64.Type]}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Column1", "Index"}, {"Column1", "Index"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Rows",each [Custom],each if [Index]=0 then null else [Custom],Replacer.ReplaceValue,{"Custom"}),
    #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"Column1", Order.Ascending}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows",{"Custom"})
in
    #"Removed Other Columns"

 

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Kudoed Authors