Forum Discussion

craig811's avatar
craig811
Helper III
5 years ago
Solved

Delete all rows from text: Item

Hi ,

 

I want to delete all rows when it finds the text 'Item' under the Column: Item.

 

So when I find the first record under the column:item called item I want to delete all of the rows after it.

 

Example:

Item

12345

36665

fgtttu

gthjk

item - I want to delete all records from the text 'Item'

hgghj

  • Hello craig811 

     

    the simplest way to do that is to use Table.FirstN

    Here a example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjFVitWJVjI2MzODsNLSS0pKSsHM9JKMrGwwK7MkNRfMyEhPz8hSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}}),
        DeleteAfterItem = Table.FirstN
        (
            #"Changed Type",
            each Text.Lower([Item])<>"item"
        )
    in
        DeleteAfterItem

    before

    after

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

     

2 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello craig811 

     

    the simplest way to do that is to use Table.FirstN

    Here a example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjFVitWJVjI2MzODsNLSS0pKSsHM9JKMrGwwK7MkNRfMyEhPz8hSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}}),
        DeleteAfterItem = Table.FirstN
        (
            #"Changed Type",
            each Text.Lower([Item])<>"item"
        )
    in
        DeleteAfterItem

    before

    after

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

     

  • I am sure this can be optimized but here is one approach:

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjFVitWJVjI2MzODsNLSS0pKSsHM9JKMrGwwK7MkNRfMyEhPz8hSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Delete", each List.Accumulate({0..[Index]},0,(state,current)=> state + (if #"Added Index"{current}[Item]="item" then 1 else 0))),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each [Delete] < 1)
    in
    #"Filtered Rows"

     

    BEWARE: Power Query is case sensitive.  "Item"  and "item" are not the same thing.