Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenating multiple cells in one column based on condition on list of strings in another

Given a spreadsheet where we have columns as follows:

A,     ..., P,     ..., S,                   ...
Test1, ..., Name1, ..., Test1, Test2, Test3, ...
Test2, ..., Name2, ..., Test3,               ...
Test3, ..., Name3, ..., Test1, Test3,        ...

I would like to create a list of values from Column P where A is in the list S1. For example, for the column A value Test1, a new column T would contain the following: Name1, Name3 because Test appears in both rows in S.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Excel MS365 formula:

I feel like the following could point you in the right direction:

enter image description here

Formula in D1:

=MAP(A1:A3,LAMBDA(a,TEXTJOIN(", ",,REPT(B1:B3,ISNUMBER(FIND(", "&a&",",", "&C1:C3&","))))))

PowerQuery:

If you don’t have access to above mentioned functions, consider to work your way around this using PowerQuery, a powerfull tool to do this quite elegantly too:

enter image description here

Here the column ‘custom’ is created through:

= let s=[Column1], c2=Table.Column(Source, "Column2"), c3=Table.Column(Source, "Column3") in Text.Combine(List.Transform(List.Numbers(0,List.Count(c2)), each if List.Contains(Text.Split(c3{_},", "),s) then c2{_} else null),", ")

The completed M-code could look like:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Ans = Table.AddColumn(Source, "Custom", each let s=[Column1], c2=Table.Column(Source, "Column2"), c3=Table.Column(Source, "Column3") in Text.Combine(List.Transform(List.Numbers(0,List.Count(c2)), each if List.Contains(Text.Split(c3{_},", "),s) then c2{_} else null),", "))
in
    Ans

Obviously I’ve used fictional column names, just make sure to edit the above to suitable named table and columns.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading