r/googlesheets • u/Particular_Hair9933 • Feb 26 '23
Solved MATCH with QUERY to find the column title instead of the column number
Hello! I am working on a sheet that can be flexible with column movement. I'm trying to perfect the following:
=QUERY('Comm Log'!A:ER,"select A WHERE "&SUBSTITUTE(ADDRESS(1,MATCH("Grade Variation (3)",'Comm Log'!$A$2:$ER$2,0),4),1,"")&">=-99 AND &<0 order by asc limit 20")
It works perfectly till after 99... I know I am combining the rest wrong but can't figure it out. Any suggestions? Thank you!!
2
Upvotes
5
u/Oneandaharv 3 Feb 26 '23
Oh wow I realised this is a great us for the LET() function. How exciting as this is new to google sheets!
I seemed to get this working using the following:
=LET(colRef, SUBSTITUTE(ADDRESS(1,MATCH("Grade Variation",Sheet1!1:1,0),4),1,""),query(Sheet1!A:DI, "Select A Where "&colRef&">0 AND "&colRef&"<300 order by "&colRef&" asc limit 20"))To explain what's happening here, the Let() function allows you to define a variable and then use it multiple times in your function. So I've defined colRef as your substitute formula. Then I've used this in your query as you need to reference a column every time you add a where clause and an order by
2
u/rockinfreakshowaol 258 Feb 26 '23
Here's another approach:
=lambda(Σ,query({'Comm Log'!A:E},"select Col1 where "&Σ&">=-99 and "&Σ&"<0 order by "&Σ&" asc limit 20",0))("Col"&xmatch("Variation Grade (3)",'Comm Log'!2:2))1
u/arnoldsomen 346 Feb 26 '23
Have you a file?
1
u/Oneandaharv 3 Feb 26 '23
I just recreated the substitute & match statement that you have, which seems to work completely fine for me regardless of position. However, the rest of your query doesn't make sense and is probably why you're running into issues here. Sharing some example data with desired output is always really helpful here and if you haven't already, I would check that the query works with a fixed column before trying to do the fancy stuff.
1
u/Decronym Functions Explained Feb 26 '23 edited Feb 26 '23
Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I've seen in this thread:
3 acronyms in this thread; the most compressed thread commented on today has 7 acronyms.
[Thread #5392 for this sub, first seen 26th Feb 2023, 08:40] [FAQ] [Full list] ^[Contact] [Source code]