Forum Discussion
Text Join - Return Unique Text Only
In the example you share below for Dogs and Cats, I am trying to have an outcome of what pet types and what colors they are available in - both unique values based on the data in the 'original table'
Dog Black, Brown
Cat Ginger, Black, White
I would normally recommend a new question but, since the test data already exists, I will try to answer here. What you require is an array of comma-separated lists which is supported in Excel 365, if a little complicated. In the example, the first task of creating an array of distinct species is straightforward.
= LET(
species, UNIQUE(Pet),
species
)To continue the formula to colours can be achieved by defining a Lambda function that returns the list of colours for a single species.
= LAMBDA(s,
LET(
distinctColour, UNIQUE(FILTER(Colour, Pet = s)),
TEXTJOIN(", ", , distinctColour)
)
)To test the function
= SpeciesColoursλ("Dog")
returns
"Black, Brown"
This allows the initial formula to be extended using MAP and the new Lambda function
= LET(
species, UNIQUE(Pet),
colours, MAP(species, SpeciesColoursλ),
HSTACK(species, colours)
)
Things get harder if the lists are expected to be returned as row arrays giving a ragged 2D array!