Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Cleansing URL paths - stuck

I have spreadsheet that has an assortmant of URLs in one column.  I've tried to split the column by the / but the length of the URLs varies so I can't just elelminate the parts I don't want. I only w...
  • Vijay_A_Verma's avatar
    3 years ago

    Use this in a custom column where [Text] should be replaced with your column name

    [a = List.LastN(Text.Split([Text], "/"), 3),
    b = if Text.StartsWith(a{2}, "?") then List.RemoveLastN(a, 1) else if a{1} = "#" then a else List.RemoveFirstN(a, 1), result = Text.Combine(b, "/")][result]

    A sample complete code for testing

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WStZX1nfWD9aP0vfU91eK1UGI2BfaJiUmgYX0g1IL8otKioEyUBYOYX3H5OT80rySzLx0pdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text = _t]),
        #"Added Custom" = Table.AddColumn(Source, "Custom", each [a = List.LastN(Text.Split([Text], "/"), 3),
    b = if Text.StartsWith(a{2}, "?") then List.RemoveLastN(a, 1) else if a{1} = "#" then a else List.RemoveFirstN(a, 1), result = Text.Combine(b, "/")][result])
    in
        #"Added Custom"