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

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.

Related

Using COUNTIF excluding duplicates in the same column

I'd like a formula to be able to calculate a COUNTIF function, but ignoring duplicate values in the same column. My sheet looks something like this:
For example, if I were to count the number of occurrences of "Grass" it would return a result of 4, ignoring the duplicates in columns C and D.
Link to this spreadsheet - https://docs.google.com/spreadsheets/d/110De2u6mKLT7SOTfaXN6EZVqgD35pEeeog9o5WWn5bs/edit?usp=sharing
There is a new sheet on your sample spreadsheet called MK.Idea. There you will find this formula:
=ARRAYFORMULA(COUNTIF(UNIQUE(SPLIT(FLATTEN(Sheet1!B1:G1&"|"&Sheet1!B2:G),"|")),"Grass"))
That uses UNIQUE and a SPLIT(Flatten technique to isolate the "grass" values that are duplicates in a given column

How to return a dynamic range based on a horizontal lookup on Google Sheets?

I've been trying to get a range of data based on a date criteria to be found horizontally, but I can't wrap my head around getting the columns referenced in a formula and return adjacent columns and rows.
For example:
If the criteria is 01/06/2021 in a certain cell, then it should return columns H through K and the rows below up to the totals.
Appreciate any time/help!
There is one formula where you may want to use to filter your data,
=query({A:H},"Select " &"Col"&match(K5,$A$1:$H$1,0)-2&", Col"&match(K5,$A$1:$H$1,0)-1&", Col"&match(K5,$A$1:$H$1,0)&", Col"&match(K5,$A$1:$H$1,0)+1)
Where the following formula is mean to find the position of the date you want to search for (For explanation). The reason to use it is to reach the query selection such as Col5, Col6 etc:
match(K5,$A$1:$H$1,0)
Here is the result based on date in K5:

How to invert INDEX formula in Google sheets

I have a sheet named NYSEDB with stock names in column A and the companies that issued them in column B.
In another sheet I have companies in cells B2 and C2 (for example, AAPL, GOOGL etc.).
I have the following formula:
=ArrayFormula(IFERROR(INDEX(NYSEDB!$A:$B,SMALL(IF(NYSEDB!$B:$B={$B$2,$C$2},ROW(NYSEDB!$A:$A)),ROW(1:1)),1,1),""))
This formula, when I spread it down the sheet, returns all the stocks from NYSEDB that are issued by the companies I have specified in B2 and C2.
How can I make the exact opposite formula and "invert" the task - I want for the formula to show all stocks from NYSEDB WITHOUT those issued by the companies I have specified in B2 and C2.
Solution
Use the not equal operand <> instead of the equal one =.
In this case It's better to use the QUERY function. Which is effectively more expressive than an IF statement and makes exclusion straightforward using an array of exceptions.
Your formula will be then like:
=QUERY(NYSEDB!A:B, "select A where B<>'"&TEXTJOIN("' and B<>'", TRUE, B2:C2)&"'", -1)
Note: you can add as many exceptions as you want, but you will have to edit the B2:C2 range in the QUERY formula.
In this way the Query function will return the column A value whenever the column B value is not included in the exception row.
Reference
QUERY Formula

Google Sheets: Query function can't copy data

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"))

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

Resources