Google Sheets: Query function can't copy data - google-sheets

I attached a sample Google Sheet data (Link).
It contains 2 sheets. The Response sheet contains the response of the Google Form. Since the Google form has a lot of repeating questions, I have to transform the data using the Query function (sheet Transformed cell B2). The query command is
=QUERY(
{
Response!E2:H,Response!B2:D;
Response!I2:L,Response!B2:D;
Response!M2:P,Response!B2:D;
Response!Q2:T,Response!B2:D;
Response!U2:X,Response!B2:D;
Response!Z2:AC,Response!B2:D;
Response!AD2:AG,Response!B2:D;
Response!AH2:AK,Response!B2:D;
Response!AL2:AO,Response!B2:D;
Response!AP2:AS,Response!B2:D;
Response!AU2:AX,Response!B2:D;
Response!AY2:BB,Response!B2:D;
Response!BC2:BF,Response!B2:D;
Response!BG2:BJ,Response!B2:D;
Response!BK2:BN,Response!B2:D;
Response!BP2:BS,Response!B2:D;
Response!BT2:BW,Response!B2:D;
Response!BX2:CA,Response!B2:D;
Response!CB2:CE,Response!B2:D;
Response!CF2:CI,Response!B2:D;
Response!CK2:CN,Response!B2:D;
Response!CO2:CR,Response!B2:D;
Response!CS2:CV,Response!B2:D;
Response!CW2:CZ,Response!B2:D;
Response!DA2:DD,Response!B2:D
},
"select * where Col1 <> '' Order By Col6"
)
However when you look at the sheet Transformed, some data in Col F (Corresponding to Response sheet Col B) didn't get transferred. How could this problem be fixed?
Thanks in advance

It is often noted that users are tempted to mix data types within a column. The query() function will give undesirable output. If a column is intended for numeric values then only numerical values must reside in that column. Date columns must only contain dates and text columns only contain text values.
This does not mean that numbers cannot appear in a text column as long as they are in a text format.
Generally, the query() function will assume the greater number of cell types in a column to be that data type. For example, if there are 100 numbers and 20 text values in the same column then a numeric value will be assumed for that column. There is a good chance the text values will just be ignored. A slight change in your formula will convert all values to text.
See if this helps
=ArrayFormula(QUERY(to_text({
Response!E2:H,Response!B2:D;
Response!I2:L,Response!B2:D;
Response!M2:P,Response!B2:D;
Response!Q2:T,Response!B2:D;
Response!U2:X,Response!B2:D;
Response!Z2:AC,Response!B2:D;
Response!AD2:AG,Response!B2:D;
Response!AH2:AK,Response!B2:D;
Response!AL2:AO,Response!B2:D;
Response!AP2:AS,Response!B2:D;
Response!AU2:AX,Response!B2:D;
Response!AY2:BB,Response!B2:D;
Response!BC2:BF,Response!B2:D;
Response!BG2:BJ,Response!B2:D;
Response!BK2:BN,Response!B2:D;
Response!BP2:BS,Response!B2:D;
Response!BT2:BW,Response!B2:D;
Response!BX2:CA,Response!B2:D;
Response!CB2:CE,Response!B2:D;
Response!CF2:CI,Response!B2:D;
Response!CK2:CN,Response!B2:D;
Response!CO2:CR,Response!B2:D;
Response!CS2:CV,Response!B2:D;
Response!CW2:CZ,Response!B2:D;
Response!DA2:DD,Response!B2:D}),"select * where Col1 <> '' Order By Col6"))

Related

Calculate sum and average treating blank values with specific values based on other column condition without adding helper column

I would like to calculate the sum and average in Google Spreadsheet of a range based on conditions from another column, but treat blanks with a specified value. It can be accomplished using a helper column, but I would like to do it without it. Here is the sample data:
I would like to sum values in column B based on value on Column A, but replacing blanks values with the value specified on E2 and E3 respectivelly.
Here is a sample in google sheet:
https://docs.google.com/spreadsheets/d/1Cv9YxFMHuGq2biNNCdGsjU8cAD_OwCPTR4YCPLc2r34/edit?usp=sharing
I was trying to use the following formulas for the sum of team A but I am not getting the expected result:
=sumif(A2:A,"A", if(B2:B<>"",B2:B,E2)) returns 6 instead of 19
=sum(if(A2:A="A",if(B2:B<>"", B2:B, E2),)) return 27 instead of 19
I cannot use a combintation of sumif and arrayformula like this because it expects a range in the third input argument:
=sumif(A2:A,"A", ARRAYFORMULA(if(B2:B<>"", B2:B,E2)))
I wasn't going to jump in on this one, since it's after midnight and I didn't feel I had the energy to both write and explain such a formula. But I see that you yourself have helped others on this forum. So I'll soldier through for you.
Delete everything from columns G:I (i.e., leave those columns entirely blank); and I suggest removing all of the formatting that you currently have in place in those columns, since it won't make sense after what I propose below.
Place the following formula in G1:
=ArrayFormula(QUERY(FILTER({A2:A,IF(B2:B="",IFERROR(VLOOKUP(A2:A&"*",D:E,2,FALSE),0),B2:B)},A2:A<>""),"Select Col1, SUM(Col2), AVG(Col2) GROUP BY Col1 LABEL Col1 'Team', SUM(Col2) 'Sum', AVG(Col2) 'Average'"))
This one formula will generate all headers, team names and results for all teams' sums and averages.
The virtual array between the curly brackets pairs every element of A2:A with the results of the IF function. That IF function checks to see if B2:B is blank. If so, a VLOOKUP with wildcard is performed to find the Col-A team name at the start of any value in Col D and, if found, returns the corresponding filler value from Col E. (If not found, IFERROR returns 0 as the filler value. It's important to have a numerical value because of the way QUERY works.)
FILTER filters in all of the above results only for those rows where A2:A contains a non-null value.
QUERY then pulls the team names, sums and averages; the LABEL portion of the QUERY assigns your desired column headers to the results.
This formula is not restricted to only two teams. You can add as many teams as you like in A:B and assign as many filler values as necessary in D:E.
It's important to note, however, that the formula relies on the FULL team name found in A:A being found at the beginning of the Col-D values. So if your team name is "Bears," just make sure the corresponding entry in Col-D starts with "Bears" as well (e.g., "Bears blanks" or even just "Bears").
You'll need to format the entire Col H as whole numbers and the entire Col I as 0.00 to match the results you shown in your sample.
ADDENDUM (after further comments from OP):
It seems that what you're saying is that the D:E values in your sample spreadsheet were something you wanted included within the formula itself and that you did not intend for them to be used as a reference list. I think your post's reference to "without a helper column" may have been your attempt to say this; but it was not clear, as with or without that D:E list as a live reference range, the main formula may have relied on its own helper column in addition to that list.
If you want a formula that contains the list:
=ArrayFormula(QUERY(FILTER({A2:A,IF(B2:B="",IFERROR(VLOOKUP(A2:A,{"A",2;"B",4},2,FALSE),0),B2:B)},A2:A<>""),"Select Col1, SUM(Col2), AVG(Col2) GROUP BY Col1 LABEL Col1 'Team', SUM(Col2) 'Sum', AVG(Col2) 'Average'"))
To add further blank-values, just keep adding to this section...
{"A",2;"B",4}
... being sure to follow the pattern of team-comma-value-semi for all but the last entry which will not need the closing semi.
You can use a combination of sumifs and countifs
to add up the non-blanks
=sumifs(B2:B10,A2:A10,"A")
to add up the blanks (and multiply by the default value)
=countifs(A2:A10,"A",B2:B10,"")*E2
all together
=sumifs(B2:B10,A2:A10,"A")+countifs(A2:A10,"A",B2:B10,"")*E2
Average (use countifs to work out how many items):
=(sumifs(B2:B10,A2:A10,"A")+countifs(A2:A10,"A",B2:B10,"")*E2)/countifs(A2:A10,"A")

How do I use minifs with arrayformula so it autopopulates whole column?

Google sheets user here.
I am using the formula minifs to return the lowest match (out of multiple possible match). Is there a way I can use arrayformula as well to auto-populate an entire column so I don't need to copy the same formula to an entire column?
Sample data below:
Column D and J are data manually inputted. Column I is the formula(s).
Essentially what I want to do here is:
Look at Column D - sees the name "Tom"
Sees that "Tom" has 3 scores 100, 90, 70 in Column J
Formula slaps "70" back into Column I because that is the lowest score
Repeats logic for "John" and "Mary"
Note: The actual data type for column J and I is a date instead of a number. But it is easier to illustrate the problem this way.
So I can do this elegantly with the formula: =minifs(J:J,D:D,D2) and D3,D4,D5,D6...etc.
However, I will have to manually drag the formula to the entire column. This is a problem because my colleagues often insert rows in between (and forget to copy n paste the formula to Column I), is there a way I can auto-populate the entire column like I could with an arrayformula?
Assuming your data are A2:C, you can get the min or max of each row by this way: (you can also add a condition in query)
=query(transpose(query(transpose(A2:C),"select " & "min(Col"&arrayformula(textjoin("),min(Col",,row(A2:C)-1))&")")),"select Col2")
https://docs.google.com/spreadsheets/d/1Ia05jywxlvT2amFDG4vQhYOd0lo68FKdOY733MzU-MQ/copy

Google Sheets Query returning odd formatting

I have a simple sheet to try to track and format race results from a league that I've joined. For the most part I know how I want to do this but when I use a query it's dropping data in some situations and formatting it strangely in others.
It seems as if where there are more numbers in a column than text it drops all text entries.
In addition for some reason when I add a check row, if it's included in the query it pushes almost all the data into a single cell except for the check row.
Would someone mind having a look and trying to figure out why it's doing this. Link Below
On sheet RRL1 I have my compiled data on the left, my 'missing' data on the right and my weirdly formatted data below.
https://docs.google.com/spreadsheets/d/1c9xlQG06dQCrpMk3UMAX29oTlpRuhTfx6btbYTGmC8g/edit?usp=sharing
The query() formula will only support one data type per column — number, text, boolean or date. The type is determined by the majority of the values in the first few hundred rows. Values that are of another type will be returned as null, i.e., blank values.
=QUERY('Tournament Details'!D2:E22)
Use an { array expression } like this:
={ 'Tournament Details'!D2:E22 }
=TRANSPOSE(query('Tournament Details'!I3:I26))
Use this:
=transpose('Tournament Details'!I3:I26)
Use this pattern to replace "DNS" and "DNF" with nulls:
=arrayformula(
query(
{ 'RRL1'!A1:C, iferror(value('RRL1'!D1:D)) },
"select Col3, sum(Col4)
where Col3 is not null
group by Col3
label sum(Col4) 'Total AUS RRL1' ",
1
)
)
The "squished" values you mention come about because you are not specifying the headers parameter. The best practice is to always include it, like this:
=query('Tournament Details'!A2:E22,"select A where C != 'N/A'", 1)

Query Formula gives values that can't be summed using =sum()

So I have two query formulas:
=iferror(QUERY(IMPORTRANGE('Index Sheet'!$C$2,"Table 1!A1:Z1000"),"Select sum(Col7) where Col1 = """&$A5&""" label sum(Col7)''",0))
and it repeats for every row with the A5 being dynamic reference.
I am also using the query select formula:
=iferror(QUERY(IMPORTRANGE('Index Sheet'!$C$3,"Table 1!A1:Z1000"),"Select Col7 where Col1 = """&$A5&""" label Col7''",0))
When I try to use the query sum formula it says AVG_SUM_ONLY_NUMERIC for most of the sheets I am referencing, so I'm forced to use the query select formula instead.
So long story short I am trying to sum certain certain parts of columns in this new sheet (eg. H10:H15), but the sums are not summing, they just return a "-". Please see my screenshots below:
Original Source (sheet the queries are referencing):
New sheet query example:
New sheet I am trying to get the values over to (see sum function of D18):
The =sum() functions work when I use the query Sum to get the values, but for certain original sheets I can't use query sum because of AVG_SUM_ONLY_NUMERIC (some of the columns are merged etc.). So basically, forgive my poor explanation, how do I get to sum up these queried values if the query sum function can't put the values in the cells because of AVG_SUM_ONLY_NUMERIC, and the query select function are returning values that even though they appear like numbers can't be summed. I can't change the original sheets.

Google Spreadsheet Function That Sums Numbers In A Column When the Row Contains An EXACT Text

I've been at this problem for a while now. I am trying to sum numbers under a specific column when the rows equal a certain text and then display that sum on a different sheet. So far I came up with this formula: =IF(EXACT(A2,Table!A2:A)=TRUE,SUM(Table!C2:C)); however the only problem is that is sums everything in column C (which makes sense).
I wish there was a way to do something like the following: SUM(Table!C2:C where EXACT(A2,TABLE!A2:A)=TRUE). I've also tried the SUMIF(), DSUM(), and QUERY() functions to no avail. I must be getting logically tripped up somewhere.
Figured it out: =SUM(FILTER(Table!E4:E, EXACT(Table!A4:A,A4)=TRUE)).
=sum ( FILTER (b1:b10, a1:a10 = "Text" ) )
// the above formula will help you to take the sum of the values in column B when another column A contain a specific text.
The formula is applicable only in Google Spreadsheets

Resources