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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric 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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

Top Solution Authors
Top Kudoed Authors