Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Remove initial zero values in a column

I have a table with values starting from zero and then reaching a non-zero value. I want to remove only the initial rows with zero values. Have tried a lot of solutions with List.Positionof bu...
  • ppm1's avatar
    3 years ago

    Here's one way to do it in the query editor with GroupKind.Local.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlDSUUpUitXBzjKEs4yArCQwyxjOMsDBSlaKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"AllRows", each _, type table}}, GroupKind.Local),
        #"Removed Top Rows" = Table.Skip(#"Grouped Rows",1),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Top Rows", "AllRows", {"Column2"}, {"Column2"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Expanded AllRows",{{"Column2", type text}})
    in
        #"Changed Type1"

     

     

    Or, here is how to do it with List.PositionOf

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlDSUUpUitUhxDICspLALGM4ywAHK1kpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}}),
        Custom1 = List.PositionOf(#"Changed Type"[Column1], 1, Occurrence.First, (x,y)=> if x<>0 then true else false),
        Custom2 = Table.Skip(#"Changed Type", Custom1)
    in
        Custom2

     

     

    If you try both and they both work, please report back which is more performant.

     

    Pat