Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Concatenate specific columns with prefix without null

I got a table where values are stored in columns with the Prefix "SEARCH_" (number and name of the columns may change) and want to add a concatenated column "FOUND". The null values should be ignored...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous ,

     

    If you could count how many the columns that without "SEARCH_"  prefix and make sure they are sorted before columns that with "SEARCH_"  prefix, I'd suggest you use CNENFRNL 's method by just change the number in List.Skip function, as shown below. I think it's the most efficient.

     

     

    Otherwise, you may follow my workaround which may be  a little complex.

    1. Add a Index column to the original table

    2. Duplicate the original table to add a new table --> Select Index column, unpivot other columns

    3. Filter all rows when Attribute column contains "SEARCH_" and Value <>"null" , then only remains Index and Value columns

    4.Group Values by Index column and then renamed "FOUND":

    Here is the whole M syntax of the New Table:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJUKCzNTM5WSCrKL8/T09MDChkY6gGRkYGREZCTV5qTg0bF6kQrORGv1c3QyNgEpNzJGazVmQitjlisdCHBSmMTM1O43lgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Topic = _t, Summary = _t, Date = _t, SEARCH_A = _t, SEARCH_my_value = _t, SEARCH_text = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Topic", type text}, {"Summary", type text}, {"Date", type date}, {"SEARCH_A", type text}, {"SEARCH_my_value", type text}, {"SEARCH_text", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
        #"Added Custom1" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each Text.Contains([Attribute],"SEARCH") and [Value]<>"null"),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Custom] = true)),
        #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Custom","Attribute"}),
        #"Grouped Rows" = Table.Group(#"Removed Columns1", {"Index"}, {{"Count", each Text.Combine([Value] , ","), type text}}),
        #"Renamed Columns" = Table.RenameColumns(#"Grouped Rows",{{"Count", "FOUND"}})
    in
        #"Renamed Columns"

    5. Back to the original table, Merge Queries --> Expand columns:

     

    Final output:

     

     

    Best Regards,
    Eyelyn Qin
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.