Search sheet for multiple matches in a single row - google-sheets

I would like to modify the following QUERY, or some formula variation that accomplishes the same result, to only be entered a single time at the top of the column and fill the cells below using ARRAYFORMULA
=IFERROR(QUERY(Items!$A$2:$T,"SELECT E,F,G,H,I,J,K,L,M,N,O,P,Q,R where A = '"&Estimate!A2&"' and B = '"&Estimate!B2&"' and C = '"&Estimate!C2&"' "&if(Estimate!D2<>"", "and D = '"&Estimate!D2&"'",)&" AND A is not null LIMIT 1", 0),"")
My formula is in Estimate!G2 of my spreadsheet.

this answer is pretty much correct, tho the exact formula would be:
=ARRAYFORMULA(IF(LEN(A2:A), VLOOKUP(A2:A&B2:B&C2:C&IF(LEN(D2:D), D2:D, "*"),
{Items!A2:A&Items!B2:B&Items!C2:C&Items!D2:D,Items!A2:T},
{6,7,8,9,10,11,12,13,14,15,16,17,18,19}, 0), ))

I think you'd have to use Vlookup, which isn't quite as convenient as Query. This is what I have so far, but it might need a bit more work to get exactly the same results as query in all cases (owing to the ORDER statement within the query):
=ArrayFormula(if(A2:A="","",vlookup(A2:A&B2:B&C2:C&if(D2:D="","*",D2:D),{Items!A2:A&Items!B2:B&Items!C2:C&Items!D2:D,Items!A2:T},{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21},false)))

Related

SUM function works fine in one sheet, but not the other?

I asked this before but I can't for the life of me find that question, so I just have to assume it dissolved into the ether.
I have this SUM function =SUM(QUERY($A$1:$B, "SELECT B WHERE A = '" & "s" & "'", 0)) that works just fine when used on one sheet, but not on another sheet in the same spreadsheet. In the other spreadsheet, it returns a zero rather than the total that it's supposed to. Through some experimenting, I've found that reducing the query search range seems to fix the problem, but that is unhelpful to me as the new range is smaller than what I need. If anyone knows anything about this or any possible fixes, some help would be super appreciated!
Here is a copy of the spreadsheet in question, all cells containing the formula are highlighted yellow: https://docs.google.com/spreadsheets/d/1bNmDYdUw-FU4wWTShV1cijtOt9l7McvktgAoDELbwQ8/edit?usp=sharing
If any more info is needed feel free to ask! Thank you!
Problem is in your K Column formula. The formula returning text instead of numeric number which causing trouble QUERY() formula. Change this formula =IF((E2*F2*H2)>0, Roundup(((E2/F2)+(E2/H2))/2, 3), "-") to
=IF((E2*F2*H2)>0, Roundup(((E2/F2)+(E2/H2))/2, 3), 0)
You can directly use avg() function to query formula to get average. Try-
=QUERY($B$2:$M, "SELECT avg(K) WHERE B = 's' label avg(K) ''", 0)
use:
="Average: "&AVERAGEIF(B2:B, "s", K2:K)

How to import multiple column values with one condition?

I am having problems in getting the values. I need to get the values of July 10, 2020 to July 25, 2020 under column TL "June Troy". I have tried to do query with importrange and filter with importrange. But I cannot get it right. Please help.
If I understand your question, the following query should work for you:
=QUERY(ARRAYFORMULA(TO_TEXT({importrange("https://docs.google.com/spreadsheets/d/1CQkhI5dZoIUfoKF1aQ8lm1Y8rmOOZapaoYBJw8BJTSE/edit?usp=sharing","Attendance!A1:BC99")})),
"select Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25 where Col7 = 'June Troy' ",1)
Note that since your test data has June Troy on every row, this ends up selecting every row.
More importantly, your "value" columns have mixed data types, both numeric and string values, and QUERY ignores the minority data types and returns blanks for those values. So I included the TO_TEXT function to convert individual cells to text before passing them to the QUERY. And to make the TO_TEXT act on every cell in the range, it is wrapped in an ARRAYFORMULA.
Let us know if this works for you.
UPDATED: To correct formula. Sorry about that.
This is an answer to your second question, which should perhaps be a separate from the first part, since it is a different issue. But yes, you should be able to connect two IMPORTRANGE queries. Consider this formula:
={
QUERY(ARRAYFORMULA(TO_TEXT({importrange("https://docs.google.com/spreadsheets/d/1CQkhI5dZoIUfoKF1aQ8lm1Y8rmOOZapaoYBJw8BJTSE/edit?usp=sharing","Attendance!A1:BC99")})),
"select Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15
where Col7 = 'June Troy' ",1),
QUERY(ARRAYFORMULA(TO_TEXT({importrange("https://docs.google.com/spreadsheets/d/1CQkhI5dZoIUfoKF1aQ8lm1Y8rmOOZapaoYBJw8BJTSE/edit?usp=sharing","Attendance!A1:BC99")})),
"select Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25
where Col7 = 'June Troy' ",1)
}
Basically, you would have two very similar queries. In my example, I point them both at the same sheet, but you can point to a different link, for one of the queries.
They are wrapped in braces, "{...}", to form a new array. And most importantly, the first query has a comma, ",", after it, to force the result of the second query to be in adjacent columns, on the same rows. If you separate the two queries with a semi-colon, ";", the result of the second query would be added as rows underneath the first query, not in columns beside it.
HOWEVER, I think this causes an error if the two queries don't both return the same number of rows. So that will depend on your data. But since you are getting related columns, I'm assuming they should return the same number of rows. If not, share the data from your two sample sheets, and what the desired outcome should look like.

COUNTIFS for Last 10 Matching Entries

Spreadsheet
Tab Ladder - Col J has a COUNTIFS formula that calculates % of matching criteria within the last 24hrs. I would like to limit the calculation to the last 10 matching entries within the 24hr window.
I have a stub formula in J35 outlining how I would like it structured. I have done a lot of searching, and while I have found a few examples of "Last x rows" - I haven't been able to translate those concepts to my specific needs.
Thank you in advance for your help!
instead of your:
=COUNTIFS($A$18:$A, ">"&Dates!$C$4+TIME(Dates!$C$3,0,0)-1,
$D$18:$D, $D35,
$H$18:$H, $H35,
$Y$18:$Y, "")
use:
=COUNTA(IFERROR(QUERY(FILTER(ROW($A$18:$A),
$A$18:$A>Dates!$C$4+TIME(Dates!$C$3,0,0)-1,
$D$18:$D=$D35,
$H$18:$H=$H35,
$Y$18:$Y=""), "limit 10")))
I have also discovered that the following nested QUERY would work - though I would have to restructure the sheet a little bit so that the formula was outside of the bounds of the QUERY so not to trigger a circular dependency.
=QUERY(QUERY($A$18:$Y, "Select T Where H = '"&$H35&"' and D = '"&$D35&"' and E = '"&$E35&"' Order by C desc limit 10", 0), "select avg(Col1) label avg(Col1)''", 0)

Google Sheets - Query - Running Total below dynamic results

Testing Sheet:
Wondering if there is a witty way to add a Total to the last row +1 of
a Query result.
See Sheet 'Lookup' for a static example of what I am asking for.
I don't know if there is a way to have a hidden column that calculates
transposed only under the last row of a query, or if there is a smart
way to work Query for this answer.
All great answers. Each on very useful in its use case.
Макс Махров gets the answer with using a query statement.
Now I was not keen on having an extra sheet to hold the totals so I added a row at the top which I can simply hide and used this formula:
query({Orders!A:E;A1:E1},"select Col1, Col3, Col4 where Col2 = '"&C3&"' order by Col4",1)
Only problem I have is trying to figure out how to add TEXT to the bottom row, it seems to only want numerical input.
How do I fix this? What am I glitching?
Thanks !
Mars
The trick is to make second query and count totals for selected product.
Plan of actions:
add new sheet with query on it, something like this: =QUERY(Orders!A:E,"select B, 0, sum(D) where B like '"&Lookup!C2&"' Group by B",0)
Prepare arrayformula which combines data in Lookup sheet: = ArrayFormula({Importrange(1),Importrange(2)}) Note that number of columns must retain the same.
Edit query so it takes Col1, Col2, Col3... instead of A, B, C...
Make word 'total' visible instead of zero. Set number format: 0;0;total Set it for range B9:B on Lookup sheet
Make Conditional Formatting with formula =and($B4 =0,isnumber($B4)) for range A4:C on Lookup sheet.
That's seems have to complete the task.
Hope it Helps!
Your Example
Working example.
Here is one way:
Put TOTAL way down in row 1000
Select the range A3:C999. Select data > filter to create filters
Select C3, set the filter to hide all blanks
A second way is to limit the query result to show only the top 8 results:
Change your query to =query(Orders!A:E, "select A, C, D where B = '"&C2&"' order by D desc limit 8",1) It will reverse-order column D (largest first), and set row limit to 8.
Change the formula of your TOTAL to =sumif(Orders!B:B,C2,Orders!D:D)
Try this formula in the column adjacent to your query:
=ArrayFormula({$C$4:offset($C$4,count($C$4:$C),0,1,1);sum($C$4:offset($C$4,count($C$4:$C),0,1,1))})
It duplicates your column of values (I haven't figured out a way around that yet) and then adds a total to the bottom of that column, and changes dynamically with the range from your query.
Here's a working version.
Interesting challenge! It got the old grey matter turning... ;)
Thanks,
Ben

pulling row number into query google spreadsheet

I have a data set that looks like this: starting on A1 with "1"
1 a
2 b
3 c
4 d
Column A is an arrayformula =arrayformula(row(b1:b))
Column B is manual input
i want to query the database and finding the row of the item by match column B so i have code as such
=query("A1:B","select A where B like '%c%')
this should give me "3"
My question:
is there a way to pull the 1-4 numbers into the query line? with something like array formula row(b1:b). I don't want to waste an extra column on column A
so basically I want just the manual input and when i query it gives me the row number.
No script code please.
I've tried a few things and it didn't work.
Looking for a solutions that starts with
=query()
You can also use a formula to pull in more than one row in the dataset which matches the condition, if this is important to you:
=arrayformula(filter(row(B:B); B:B="c"))
And you can have wildcard type operators, under certain circumstances (you are going to match text or items that can look like text (so numbers can be treated as text - but boolean will need more steps); that the dataset is not huge), using regular expressions. e.g.
=arrayformula(filter(row(B:B); regexmatch(B:B, "(c|d)")))
You could also use standard spreadsheet wildcard operators, e.g.
=arrayformula(filter(row(B:B); countif(B:B, "*c*")))
Explanation: In this case, the filter will be true when countif is greater than zero, i.e. when it sees something with a letter c in it, since spreadsheets see a value greater than zero as a boolean true and so, for that row where there is a countif match, there will be a a filter match, and so it will display that row (indeed, it is a similar situation with the regexmatch creating a true when there is a match of either c or d, in the case above).
Personally, I wanted to learn regex a bit, so I would go towards the regexmatch option. But that is your choice.
You can also, of course, create the match outside of the cell. This makes it easy to create a list of matches that you want to satisfy elsewhere on the sheet. So you could have a column of words or parts of words, from Z2 downwards, and then join them together in cell Z1 for example like this
="("&join("|",filter(Z2:Z50,len(Z2:Z50)))&")"
Then your filter function would look like this:
=arrayformula(filter(row(B:B), regexmatch(B:B, Z1)))
If you want to use like operator in the query function, you can try something like this:
=arrayformula(query(if({1,0}, B:B,row(B:B)),"select Col2 where Col1 like '%c%' "))
You can also use the regular expressions in the query function, for example:
=arrayformula(query(if({1,0}, B:B,row(B:B)),"select Col2 where Col1 matches '(.*c.*|.*d.*)' "))
I'm not entirely clear on the question, but as I understand it, you want to be able to enter a formula, and have it return the row number of the matched item in a range? I'm not sure where array formulas come in.
If I've understood your question correctly, this should do the trick:
=MATCH("C",B1:B,0)
In your example, this returns 3.
Please forgive me if I've misunderstood your question.
Note: If there are multiple matches, this will return the row number for the first instance of your search.
=QUERY({A1:A,ARRAYFORMULA(ROW(A1:A))},"SELECT Col2 WHERE Col1 LIKE '%c%'")

Resources