QUERY with AND criteria not returning expected results - google-sheets

I have a QUERY that seems to be treating AND more like OR. In other words, when the value of Col11=TRUE and the value of Col12=7, the results are displayed as though Col12=8. Am I missing something? I've tried adding quotes around the variables, parentheses around the two criteria. Adding spacing around the =. What else is there?
Col11 is only TRUE or FALSE values and Col12 is only numeric values from 1-8.
=QUERY({$A$3:$AJ},"SELECT Col3,Col10 where Col11=TRUE and Col12=8",0)
Here's a link to my sheet. It's buried in a larger formula in AK2
AK6 is a good example. It shows U U. It should only show U. It is treat X6 as though it's value is 8 when it is actually 7.

I believe I worked out what is happening.
You are getting two 'U's because I think your inner array is returning multiple rows for Col3='R2-D2', one row where Col23=TRUE and Col24=8, and then another row where Col27=TRUE and Col28=8.
I'm not positive, but I think the values in AK don't relate specifically to the values in that specific row, but instead relate to an array queried across all of your data rows. So as the outer ArrayFormula works down the column, the inner array (with multiple VLOOKUP/ArrayFormula/Queries) is still a large subset of the whole data range. That's assuming I've understood your complex formula correctly - my apologies if I've misunderstood something.
I've added a Heroes-TEST sheet to your sheet. It only has ten rows, all of the R2-D2 data from your Heroes tab. The columns are collapsed for visibility. See what happens when you highlight all the row data below Row3 and press delete - and then UNDO. The two 'U's in column AK become one, because there is only one row of data to query through now.
Your original formula is in AK2.
Let me know if this has helped.

Related

Combining Arrayformula with textjoin to eliminate delimiters

I am trying to use an arrayformula to join a string of cells in the same row as per picuture. This formula needs to automatically copy down to any new rows that are added. The formula I have so far seems to be the only one that returns at least something. I have googled and tried all sorts of join, textjoin to eliminate delimiters when a cell is empty but I keep getting all sorts of errors.
Also, note in column L I only want part of the cell, the city name, not state and zip code, which is why I added the Left function.
I would also like a header in row D1 "Summary".
Also, another problem I am having with the Arrayformula, is that any new rows that are submitted by my script, get pasted way down the bottom of the sheet, skipping blank rows. I think it sees the array formula as part of the last row even though those rows are blank. The only workaround I have so far is to delete all empty rows after my table, so I have the little [add 1000 more rows at bottom] after my last data row. But this then effects the filter range. The filter range does not seem to update automatically when new rows added. Maybe I need to address each of these issues as a new question in Stakoverflow?
Jobs Database
Delete everything from 'Jobs Database'!D:D (you can always undo it if you aren't happy with the formulas proposed). Then place one of the following two formulas in D1, though I recommend the second of the two.
The following formula will produce results in keeping with what you showed in your post and what is currently in the sheet (though my formula creates a cleaner result, because it addresses more potential issues than your original formula):
=ArrayFormula(FILTER(IF(ROW(A:A)=1,"Summary",B:B&IF(C:C="",," - "&REGEXREPLACE(TO_TEXT(C:C),"^-",""))&" - "&IF(H:H="",,H:H&" x ")&G:G&IF(I:I="",," - "&I:I)&" - "&IF(J:J="",,REGEXREPLACE(J:J,",$","")&" - ")&IFERROR(REGEXEXTRACT(L:L,"(.+)[\s\w]{8}")&" - ")&"ID: "&A:A),A:A<>""))
This formula is written to accommodate the fact that you've got column-level sorting in place. The FILTER clause surrounding everything inside also assures that the results only run as far down as you have data in Column A. This should eliminate the issue you were having with new rows being added after blanks.
I recommend the layout produced by this second formula, however:
=ArrayFormula(FILTER(IF(ROW(A:A)=1,"Summary",B:B&IF(C:C="",,CHAR(10)&TO_TEXT(C:C))&CHAR(10)&IF(H:H="",,H:H&" x ")&G:G&IF(I:I="",,CHAR(10)&I:I)&CHAR(10)&IF(J:J="",,REGEXREPLACE(J:J,",$","")&CHAR(10))&IFERROR(REGEXEXTRACT(L:L,"(.+)[\s\w]{8}")&CHAR(10))&"ID: "&A:A),A:A<>""))
In my opinion, the use of line breaks between information is cleaner and therefore easier to read.
Both formula options would require selecting all rows, right clicking, choosing "Resize rows" and selecting "Fit to data" for full viewing.
I have added two sheet ("Erik Help 1" and "Erik Help 2") with each of the above formulas in place, respectively).

Is there a way to specify an input is a single cell in Google Sheets?

I want to iterate over an array of cells, in this case B5:B32, and keep the values that are equal to some reference text in a new array.
However, SPLIT nowadays accepts arrays as inputs. That means that if I use the array notation of "B5:B32" within ARRAYFORMULA or FILTER, it treats it as a range, rather than the array over which we iterate one cell at a time.
Is there a way to ensure that a particular range is the range over which we iterate, rather than the range given at once as an input?
What I considered was using alternative formulations of a cell, using INDEX(ROW(B5), COLUMN(B5)) but ROW and COLUMN also accept array values, so I'm out of ideas on how to proceed.
Example code:
ARRAYFORMULA(
INDEX(
SPLIT(B5:B32, " ", 1), 1
) = "Some text here"
)
Example sheet:
https://docs.google.com/spreadsheets/d/1H8vQqD5DFxIS-d_nBxpuwoRH34WfKIYGP9xKKLvCFkA/edit?usp=sharing
Note: In the example sheet, I can get to my desired answer if I create separate columns containing the results of the SPLIT formula. This way, I first do the desired SPLITS, and then take the values I need from that output by specifying the correct range.
Is there a way to do this without first creating an output and then taking a cell range as an input to FILTER or other similar functions?
For example in cell C35 I've already gotten the desired SPLIT and FILTER done in one go, but I'd still need to find a way to sum up the values of the first character of the second column. Doing this requires that I take the LEFT value of the second column, but for that I need to output the results and continue in a new cell. Is there a way to avoid this?
Ralph, I'm not sure if your sample sheet really reflects what you are trying to end up with, since, for example, I assume you are likely to want the total of the hours per area.
In any case, this formula extracts all of the areas, and the hours worked, and is then easy to do further calculations with.
=ArrayFormula({REGEXEXTRACT({C5:C9;D5:D9;E5:E9;F5:F9;G5:G9;H5:H9},"(.*) \d"),
VALUE(REGEXEXTRACT({C5:C9;D5:D9;E5:E9;F5:F9;G5:G9;H5:H9}," (\d+)hrs"))})
Try that in cell E13, to see the output.
The first REGEXEXTRACT pulls out all the text in front of the first space and number, and the second pulls out all the digits in a string of " #hr" in each cell. These criteria could be modified, if necessary, depending on your actual requirements. Note that it requires the use of VALUE, to convert the hours from text to numeric values, since REGEXEXTRACT produces text (string) results.
It involved concatenating your multiple data columns into one long column of data, to make it simpler to process all the cells in the same way.
This next formula will give you a sum, for whatever matching room/task you type into B6, as an example.
=ArrayFormula(QUERY({REGEXEXTRACT({C5:C9;D5:D9;E5:E9;F5:F9;G5:G9;H5:H9},"(.*) \d"),
VALUE(REGEXEXTRACT({C5:C9;D5:D9;E5:E9;F5:F9;G5:G9;H5:H9}," (\d+)hrs"))},
"select Col1, sum(Col2) where Col1='"&B6&"' group by Col1 label sum(Col2) '' ",0))
I will also answer my own question given what I know from kirkg13's answer and other sources.
Short answer: no, there isn't. If you want to do really convoluted computations with particular cell values, there are a few options and tips:
Script your own functions. You can expand INDEX to accept array inputs and thereby you can select any set of values from an array without outputting it first. Example that doesn't use REGEXMATCH and QUERY to get the SUM of hours in the question's example data set: https://docs.google.com/spreadsheets/d/1NljC-pK_Y4iYwNCWgum8B4NJioyNJKYZ86BsUX6R27Y/edit?usp=sharing.
Use QUERY. This makes your formula more convoluted quite quickly, but is still a readable and universally applicable method of selecting data, for example particular columns. In the question's initial example, QUERY can retrieve only the second column just like an adapted INDEX function would.
Format your input data more effectively. The more easily you can get numbers from your input, the less you have to obfuscate your code with REGEXMATCHES and QUERY's to do computations. Doing a SUM over a RANGE is a lot more compact of a formula than doing a VALUE of a LEFT of a QUERY of an ARRAYFORMULA of a SPLIT of a FILTER. Of course, this will depend on where you get your inputs from and if you have any say in this.
Also, depending on how many queries you will run on a given data set, it may actually be desirable to split up the formula into separate parts and output partial results to keep the code from becoming an amalgamation of 12 different queries and formulas. If the results don't need to be viewed by people, you can always choose to hide specific columns and rows.

Excel/Sheets Consecutive Count Based on Two Conditions (function?)

I have a Google Sheet, and I'm trying to see if it's possible to get a consecutive count outputted in a third column based on the values of two other columns.
My columns are:
Column A: Will have a handful of text values that are "grouped" together. Likely around 30 of the same value, until it changes to another value. In the image above, these are text1, and text2.
Column B: Will have one of 3 values assigned to each value in column A. In the image above, these are id1, id2, id3.
Column C: Will output a consecutive count based on the values of the first two columns. My hope is that if there are multiple ID1,ID2 in consecutive order, they'll repeat that first +1 value; while ID3 is always plus 1 to the count. This is what I am trying to show in column C in the layout image above.
I've hit a wall with trying to accomplish this with various COUNTIF iterations.
Thanks for any help, or any better ideas to accomplish something similar.
(I'm hoping for a formula, but open to being pointed into a direction for a script if that's the only way).
You can try following formula:
=IF(A2=A1;IF(OR(B2="id3";B2<>B1);C1+1;C1);1)
It is also possible to do this as an array formula. I used offset ranges for column B in the first Countifs to check for a change in value but this made it a little awkward to get equal-sized arrays:
=ArrayFormula(if(A2:A="","",
countifs({"";B2:B}<>{B2:B;""},true,{A2:A;""},A2:A,row(A:A),"<"&row(A2:A),{B2:B;""},"<>id3")+
countifs(A2:A,A2:A,row(A2:A),"<="&row(A2:A),B2:B,"=id3")
))

GoogleSheet value search conditioned by approximate match

Hope you're coping with confinement wherever you are.
I'm having some difficulties with a GoogleSheet operation I'd like to do and can't find any suitable solution on the web.
I'm aiming to compare values (decimal numbers) in columns in two separate tables (I'll call table 1 column A and table 2 column C) and obtain the corresponding cell value in another column of the second table (table 2 column E).
Besides, the search is also conditioned by the a corresponding date(month) of the values in both tables (in table 1 column B and table 2 column D).
I've come up to here with a simple INDEX(MATCH()) formula applied in all lines of the table 1 sheet :
=index(table2!A:E;match(A1&month(B1);table2!C:C&month(table2!D:D);0);5)
The twist is that values in A and C are sometimes not exactly the same, they can have a slight difference in decimals, but I still want the match to work. I've tried using STDEV(X;Y)<0,1 inside the INDEX(MATCH()) or a way of using this kind of formula IF(AND(X<=Y+0,1;X>=Y-0,1) but was unsuccessful.
Please let me know if you have any suggestions in doing an efficient search with two conditions with an approximate match for one of them ?
(I have basic knowledge of QUERY functions if this can be done with that too.)
Hope I've been clear enough.
Thanks a lot !
Instead of Roundup and Rounddown, using Round and Abs worked for me.
Not working consistently.
= index($F$2:$H;match(ROUNDDOWN(-$B2;1)&month($A2);ROUNDDOWN($G$2:$G;1)&month($F$2:$F);0);3)
This worked for me. =index($F$2:$H;match(ROUND(abs($B2);1)&month($A2);ROUND($G$2:$G;1)&month($F$2:$F);0);3)
See if this works for your all data. You may need to include if condition if values could be negative/positive in your left table.
Without if condition this also may work -
index($F$4:$H;match(ROUNDup(abs($B4);$B$1)&month($A4);ROUNDup(abs($G$4:$G);$B$1)&month($F$4:$F);0);3)

Sumifs match ANY from one column

So I have two sheets. Neither need to be pretty. One is the basic entry sheet where data should be pulled from and looks a bit like this.
There's colours in column A, random fruit in column B and the value of what those two together would be in any given situation in Column C. That's all entirely manual and based on whatever I need when I'm inputting. The idea behind it is that nothing is entirely unique. You can see Apples can be on the same row as Red or Green, similarly nearly everything on this list is next to the word Red.
The trouble I run into is on the calculating sheet.
Column A is now made up of SOME colours from the Entry Sheet. This is a dynamic list that can change depending on other inputs so the number of rows won't always be the same.
Column B successfully uses UNIQUE, FILTER, and IFERROR to search Column B on the Entry Sheet, and return all the different values where the value in the A column on the Entry sheet appears SOMEWHERE in the A column on the Calculate sheet. I can go ahead and add a "Green Frog" to my entry sheet but he won't show up here. For those curious the formula here is:
=unique(FILTER(Entry!B:B,iferror(match(Entry!A:A,A:A,0))))
So far so swell.
Now I want to add them. I've ended up, because many hours on google took me there, using some kind of SUMIFS but it's producing the result pictured. The actual formula in C1 is
=SUMIFS(Entry!C:C,Entry!A:A,A:A,Entry!B:B,B1)
The result in C1 is exactly what I want. 5 is indeed the number of Red Apples and does not include the number of Green apples.
However, the same formula doesn't produce the desired result for the rest of the column. All other returns are '0' because the word 'Red' in the A column is only on the top row and obviously 'Yellow' is also not on the same row as 'Grape'.
So the question is, how to I get the 'Entry!A:A,A:A' to essentially make that particular criteria say "See these? Yes ALL of these please"
try:
=SUM(FILTER(C:C; B:B=F1; REGEXMATCH(A:A; TEXTJOIN("|"; 1; E:E))))

Resources