Forum Discussion

niark's avatar
niark
Frequent Visitor
9 years ago
Solved

Combine columns if not null or empty

Hi there,

 

I have some difficulties to combine text columns  with the result I want. If someone could help me to point me to the right direction :)

 

So I have this table :

Col1         Col2       Col3      Col4       Std Combine        Wanted

ABCDA & B & C & DA & B & C & D
 B  & B & &B
A C A & & C &A & C
A   A & & &A
 B D& B & & DB & D

 

if I combine the columns with the std feature , you can see that empty ones are surrounded by unnecessary "&" char.

what I would like is the "Wanted" column result.

 

My ideas were to create a list of not empty columns and combine them but I'm not good enough to create a custom function for that...

 

Thanks.

  • niark's avatar
    niark
    9 years ago

    Good catch Marcel but I have other columns :)

     

    Thanks for your help, you pointed me to the right way !

     

    here the solution :

    = Table.AddColumn(#"Reordered Columns", "Personnalisé", each Text.Combine(List.Select(Record.FieldValues(Record.FromList({[Col1],[Col2],[Col3]}, type [Col1 = text,Col2 = text,Col3 = text])), each _<> "" and _ <> null)," & "))

     

    Thanks again

  • Pleased you solved your own issue.

     

    I'd rather had shortened the code a bit:

     

    = Text.Combine(List.Select({[Col1],[Col2],[Col3],[Col4]}, each _<> "" and _ <> null)," & ")

19 Replies

  • MarcelBeug's avatar
    MarcelBeug
    Community Champion

    If you have no other columns, you use the query editor to add a custom column with formula:

     

    = Text.Combine(List.Select(Record.FieldValues(_), each _<> "" and _ <> null)," & ")

     

    • niark's avatar
      niark
      Frequent Visitor

      Good catch Marcel but I have other columns :)

       

      Thanks for your help, you pointed me to the right way !

       

      here the solution :

      = Table.AddColumn(#"Reordered Columns", "Personnalisé", each Text.Combine(List.Select(Record.FieldValues(Record.FromList({[Col1],[Col2],[Col3]}, type [Col1 = text,Col2 = text,Col3 = text])), each _<> "" and _ <> null)," & "))

       

      Thanks again

      • MarcelBeug's avatar
        MarcelBeug
        Community Champion

        Pleased you solved your own issue.

         

        I'd rather had shortened the code a bit:

         

        = Text.Combine(List.Select({[Col1],[Col2],[Col3],[Col4]}, each _<> "" and _ <> null)," & ")
  • Anonymous's avatar
    Anonymous
    Not applicable
    each _<> "" and _ <> null

    Can anyone point me to the meaning of each _<> "" and _ <> null?

     

    I am trying to combine columns as well. 

     

    Thank you.

     

    Laurel

  • My solution

     

    Text.Combine(List.Select({[ddd_1],[telefone_1]}, each _<> "" and _ <> null)," - ")