How to search a table and return concatenated values based on certain criteria?

clarky4

New Member
Joined
Oct 14, 2020
Messages
12
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Please help!

I have a table with column headers (a,b,c) and row footers (x,y,z):

abc
1​
2​
3​
x
1​
2​
y
1​
2​
z

I'm trying to lookup up all instances of each number in the table, and return text that would concatenate the corresponding footer values with the corresponding header values:

Example 1:

- if I searched for 1's, I would expect the text: "x z a, y b"

Example 2:

- if I searched for 2's, I would expect the text: "x b, y z c"

Example 3:

- if I searched for 3's, I would expect the text: "x c"

Can this be done? If so, how?

Thanks! :)
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
How about
+Fluff v2.xlsm
LMNO
9abc
10123x
1112y
1212z
13
142xb, yc, zc
15
Main
Cell Formulas
RangeFormula
M14M14=TEXTJOIN(", ",,IF(L10:N12=L14,O10:O12&L9:N9,""))


This will work in 2019, but not 2016.
 
Upvote 0
working on both versions
Power Query:
// Query1
let
    Search = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    UOC = Table.UnpivotOtherColumns(Source, {"d"}, "Attribute", "Value"),
    TCC = Table.CombineColumns(UOC,{"d", "Attribute"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    Group = Table.Group(TCC, {"Value"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group, "result", each [Count][Merged]),
    Extract = Table.TransformColumns(List, {"result", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    Result = Table.NestedJoin(Search,{"search"},Extract,{"Value"},"Table",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Result, "Table", {"result"}, {"result"}),
    RC = Table.RemoveColumns(Expand,{"search"})
in
    RC
Table3Table4
abcdsearchresult
123x1xa,yb,za
12y
12z
 
Upvote 0
This is close, but is it possible not to repeat the header columns in the result? Or would this require a second step following the above mentioned results?
 
Upvote 0
it is not known to whom your post is...

anyway, something like this?
Table4
searchresult
1ax,z,by

Table4
searchresult
2bx,cy,z

Table4
searchresult
3cx
 
Upvote 0
sry, my post was to either you or Fluff...

...but to answer your question, something more like this:

Search: 1
Result: x z a, y b

Search: 2
Result: x b, y z c

Search: 3
Result: x c
 
Upvote 0
Yes, what you have (above) is correct.

A more complete set of examples would be as follows:

TABLE:
abc
123x
12y
12z

SEARCH RESULTS:
searchresult
1x z a, y b
2x b, y z c
3x c


I just wanted to posting using the fancy lil add-in! ;)
 
Upvote 0
I just wanted to posting using the fancy lil add-in! ;)
sure, no problem (y)

here is M (not optimised) for Power Query
Power Query:
let
    Search = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    UOC = Table.UnpivotOtherColumns(Source, {"d"}, "Attribute", "Value"),
    ExtractD = Table.TransformColumns(Table.AddColumn(Table.Group(UOC, {"Attribute", "Value"}, {{"Count", each _, type table}}), "Custom", each List.Distinct([Count][d])), {"Custom", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    TCC = Table.CombineColumns(ExtractD,{"Custom", "Attribute"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
    RC = Table.RemoveColumns(TCC,{"Count"}),
    ExtractR = Table.TransformColumns(Table.AddColumn(Table.Group(RC, {"Value"}, {{"Count", each _, type table}}), "result", each [Count][Merged]), {"result", each Text.Combine(List.Transform(_, Text.From), ", "), type text}),
    RC1 = Table.RemoveColumns(ExtractR,{"Count"}),
    TNJ = Table.NestedJoin(Search,{"search"},RC1,{"Value"},"Query1",JoinKind.LeftOuter),
    TSC = Table.SelectColumns(Table.ExpandTableColumn(TNJ, "Query1", {"result"}, {"result"}),{"result"})
in
    TSC
Table3Table4
abcdsearchresult
123x2x b, y z c
12y
12z

change the name of the tables in the code according to yours
 
Last edited:
Upvote 0
I guess today's the day I learn about Power Query! ;)

At first pass, this looks to be doing exactly what I hoped it would.

Many thanks!
 
Upvote 0

Forum statistics

Threads
1,215,545
Messages
6,125,448
Members
449,227
Latest member
Gina V

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top