Forum Discussion
Search of multiple values connecting multiple tables
- May 26, 2022
ema_rz Now, I didn't recreate your tables, 6 levels deep, but did a mock-up at a smaller scale in Power Query. Although you mentioned not to be "practical" with PQ, you might be able to follow the steps in the attached (not so elegant) example. With a number of consecutive merge and expand steps, starting with the last table, you will end up with the sequence of items from the first column in the first table that correspond to the item selected from the second column of the last table.
Having been, shall we say, somewhat discouraging, I used Riny_van_Eekelen 's setup to scan through the array of tables, picking up a selection at each stage. I always searched column 2 to return column 1 (array calculation has problems with irregular patterns, so I switched columns in the second table).
I used REDUCE to scan the tables by index and generate the output selection from each input.
= IFERROR(
TEXTJOIN(", ",,
SORT(
REDUCE(Selection,{1,2,3,4}, LAMBDA(tgt,tbl, NextTargetλ(tgt, tbl)))
)
),
"")I was surprised that it worked. The complexity came when I needed to trap errors that occurred when there were no matches.
NextTargetλ = LAMBDA(tgt, tblNum,
DROP(
UNIQUE(
REDUCE("",tgt,
LAMBDA(list,t,
LET(
returnlist,TblColλ(tblNum, 1),
lookupList,TblColλ(tblNum, 2),
IF(
AND(t<>"?",OR(lookupList=t)),
VSTACK(list,FILTER(returnlist, lookupList=t,"?"),
list)
)
)
)
),
),
1)
);Now I suppose I should do the decent thing and ensure Riny's solution works with my altered data.
[p.s. I relinked the joins but, oddly, I finished up with a situation in which Refresh resets the selection to "Sophie"]
Amazing to see what you came up with. Note that I didn't make the selection dynamic. In one of the queries I just hard-coded (my bad) the 7th entry from the last table, and used that to work my way back to the first table. Both the Selection and the end Results are loaded back to Excel in B19 and B20. That's why the drop down you created in B19 would reset itself to Sonia, every time you refreshed PQI
Made the selection dynamic now (attached) and it seems that there is a mismatch between PQ and the Lambdas in the cases for Anne and Amber. I'm showing Amber in the attached file where the Lambda doesn't pick-up number 1. The red highlights are done with CF, the yellow ones are done manually :), just to illustrate this case.
- PeterBartholomew1May 28, 2022Silver Contributor
Thanks for that. You are correct that my solution is missing a term. It appears that the #CALC! error, caused by filtering using a criterion that is never met, isn't being trapped correctly. The message for me is that I need to build the error trapping into the solution from the outset and not add it as an afterthought. Whereas PQ handles non-existent joins gracefully by returning a null, Excel creates an error, so life gets a little harder.
Thanks for explaining why Sonia was so persistent, I did wonder whether I had introduced the fault when adjusting the joins.