Forum Discussion

max_mustermann's avatar
max_mustermann
Frequent Visitor
4 years ago
Solved

How to remove rows based on text length

I want to delete all rows of Column "test" which contain less than 4 characters.

 

E.g. "132" should be removed, "testtest" should not be removed.

 

I have tried the following in M but there is a syntax error:

 

= if Text.Length([test]) <4 then Table.RemoveRows(Table.FromRecords,1)

 

  • Hi max_mustermann ,

     

    I can just tell you alternate way, since you are already checking Text.Length, just filtering records <4 or >3 will serve the purpose:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ2UorViVYqSS0uAWGl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [test = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"test", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Test-Length", each Text.Length([test])),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Test-Length", Int64.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type1", each [#"Test-Length"] > 3)
    in
        #"Filtered Rows"

1 Reply

  • mahenkj2's avatar
    mahenkj2
    Solution Sage

    Hi max_mustermann ,

     

    I can just tell you alternate way, since you are already checking Text.Length, just filtering records <4 or >3 will serve the purpose:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ2UorViVYqSS0uAWGl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [test = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"test", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Test-Length", each Text.Length([test])),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Test-Length", Int64.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type1", each [#"Test-Length"] > 3)
    in
        #"Filtered Rows"