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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors