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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
josephrandall
Regular Visitor

Remove row with incorrect value

See picture below. There is one row (highlighted) that the value is wrong. Is there anyway I can remove this row via formula?

Basically if the values of the row above and below are equal to each other but different to a cell inbetween, remove that row.

Thanks for any input.

Screenshot 2023-03-04 132903.png

1 ACCEPTED SOLUTION
artemus
Microsoft Employee
Microsoft Employee

Here is a function you can use to add the next and previous row values as columns to your table.

You would invoke it like:

Table_Window(MyQuery, {-1, 1}, {"id})

This will add "id-1" and "id+1" columns

// Table_Window
let fn = (table as table, offsets as anynonnull, columns as anynonnull)  =>
    let columns = if not (columns is list) then {columns} else columns in
    let indexColumnName = Text.NewGuid() in
    let table = Table.AddIndexColumn(table, indexColumnName, 0) in
    let withIndexFn = (table as table, offset as anynonnull)  =>
        if offset = {} then
            table
        else if offset is list then
            @withIndexFn(@withIndexFn(table, offset{0}), List.Skip(offset))
        else
            let indexColumnName2 = Text.NewGuid() in
            let table2 = Table.SelectColumns(table, columns) in
            let table2 = Table.AddIndexColumn(table2, indexColumnName2, -offset) in
            let newColumnNames = List.Transform(columns, each _ & (if offset > 0 then "+" else "") & Number.ToText(offset)) in
            let table2 = Table.RenameColumns(table2, List.Zip({columns, newColumnNames})) in
            let joinedTable = Table.Join(table, {indexColumnName}, table2, {indexColumnName2}, JoinKind.LeftOuter) in
            let joinedTable = Table.RemoveColumns(joinedTable, {indexColumnName2}) in
            let joinedTable = Table.Sort(joinedTable, indexColumnName) in
            joinedTable
    in
    let table = withIndexFn(table, offsets) in
    let table = Table.RemoveColumns(table, {indexColumnName}) in
    table
in
   fn

 

View solution in original post

3 REPLIES 3
KeyurPatel14
Responsive Resident
Responsive Resident

If you dont want to make a custom function then you can try the below M Code also it will also work if the data increases in future.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjEwNDE0NTM1MTJWitUZSL4JnfmmFPLN6Mw3p7J+UvkWSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Pvalue", each try #"Added Index" [Data] {[Index] - 1} otherwise 0),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Nvalue", each try #"Added Index" [Data] {[Index] + 1} otherwise 0),
#"Added Custom2" = Table.RemoveColumns(Table.SelectRows(Table.AddColumn(#"Added Custom1", "Result", each if [Pvalue] = [Nvalue] and [Pvalue] <> [Data] then 0 else [Data]),each ([Result] <> 0)),{"Index", "Data", "Pvalue", "Nvalue"})
in
#"Added Custom2"

artemus
Microsoft Employee
Microsoft Employee

Here is a function you can use to add the next and previous row values as columns to your table.

You would invoke it like:

Table_Window(MyQuery, {-1, 1}, {"id})

This will add "id-1" and "id+1" columns

// Table_Window
let fn = (table as table, offsets as anynonnull, columns as anynonnull)  =>
    let columns = if not (columns is list) then {columns} else columns in
    let indexColumnName = Text.NewGuid() in
    let table = Table.AddIndexColumn(table, indexColumnName, 0) in
    let withIndexFn = (table as table, offset as anynonnull)  =>
        if offset = {} then
            table
        else if offset is list then
            @withIndexFn(@withIndexFn(table, offset{0}), List.Skip(offset))
        else
            let indexColumnName2 = Text.NewGuid() in
            let table2 = Table.SelectColumns(table, columns) in
            let table2 = Table.AddIndexColumn(table2, indexColumnName2, -offset) in
            let newColumnNames = List.Transform(columns, each _ & (if offset > 0 then "+" else "") & Number.ToText(offset)) in
            let table2 = Table.RenameColumns(table2, List.Zip({columns, newColumnNames})) in
            let joinedTable = Table.Join(table, {indexColumnName}, table2, {indexColumnName2}, JoinKind.LeftOuter) in
            let joinedTable = Table.RemoveColumns(joinedTable, {indexColumnName2}) in
            let joinedTable = Table.Sort(joinedTable, indexColumnName) in
            joinedTable
    in
    let table = withIndexFn(table, offsets) in
    let table = Table.RemoveColumns(table, {indexColumnName}) in
    table
in
   fn

 

thank you, kindly.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors