How can I add the results from 3 query statements? [duplicate] - google-sheets

This question already has answers here:
IF + AND / OR logic inside of a query
(1 answer)
How to calculate a sum conditionally based on the values of two other columns
(2 answers)
Closed 4 months ago.
To get the number of flight cancellations in any given month, I'm using a COUNTIFS statement, as follows:
=COUNTIFS(QF_Data_2022!C$2:C,"September",QF_Data_2022!E$2:E,"canceled",QF_Data_2022!F$2:F,"MIA")
Where Col_C is the month, Col_E is the flight status, and Col_F is the airport code.
It works great.
But in an effort to obtain figures that reflect more than a single month (i.e. Q3), the approach of adding the following conditions on the same column (C) is not working:
(...QF_Data_2022!C$2:C,"September",QF_Data_2022!C$2:C,"August",QF_Data_2022!C$2:C,"July"...)
So I've moved to a QUERY function:
=QUERY(QF_Data_2022!A2:R, "SELECT COUNT(C) WHERE C='July' OR C='August' OR C='September' AND E='CANCELED' AND F='MIA' LABEL COUNT(C) '' ")
BUT the result gives me an integer that is FAR too high (e.g. 3770 results out of 5800 rows, when the actual number should be about 164).
So, is something like the following possible:
SUM = QUERY 1 + QUERY 2 + QUERY 3
Or is there a better approach altogether?

Related

Repeate filled cells of a row for x times in another row in Goolge Sheets by formula [duplicate]

This question already has answers here:
Repeat range dynamically
(2 answers)
Closed 15 days ago.
I have a column containing x rows of names and I want to be able to repeat those rows of names x times each in another row.
It should be possilbe to have at least 15 names and repeate it 900 times.
Here an example:
I tried this now for about 6 hour with arrayformula etc. and didn't find any working solution..
For example =TRANSPOSE(split(rept(join(";",A:A)&";",10),";")) would do the job, but the rept function is limited in characters, so it doesn't work for this case..
Are you able to do this?
You can try with REDUCE and SEQUENCE like this. It's wrapped in QUERY to exclude empty rows, so you can get your full column as input, despite its amount of elements:
=QUERY(REDUCE(,SEQUENCE(A1),LAMBDA(a,v,{a;B1:B})),"Where Col1 is not null")
use:
=INDEX(FLATTEN(TEXT(TRANSPOSE(A1:A4); FLATTEN(SPLIT(REPT("#×"; 5); "×")))))

Weighted lottery [duplicate]

This question already has answers here:
Repeat whole row N times based on column value in Google Sheets
(2 answers)
How to Repeat a Data Set (several columns) x times in Google Sheets [duplicate]
(1 answer)
Repeat range of items multiple times in Google Sheets
(5 answers)
Closed 5 months ago.
I need to create a weighted lottery where people can have multiple entries and the number of entries is based upon a series of questions. For each question they get right, they get an entry into the lottery.
In my head, I've got a table with the participants' names and their total number of points (entries), then I need some way to have a list of the names with their multiple entries. So if my initial table looked like this:
Name
Points
John
5
Larry
4
Andre
2
Mika
6
Then my output list would look like this:
Name
John
John
John
John
John
Larry
Larry
Larry
Larry
Andre
Andre
Mika
Mika
Mika
Mika
Mika
Mika
Then I could just use the default row leaders from the output table as the entry for the person and use a random number generator to pick numbers.
That being said, I'm open to other ideas.
Try this formula-
=QUERY(INDEX(FLATTEN(SPLIT(REPT(A1:A4&"#",B1:B4),"#"))),"where Col1 is not null")
REPT(A1:A4&"#",B1:B4) will repeat each cell value of A1:A4 as per number in B1:B4. Then Split and flatten with make them a vertical array (column). Query() will return all the values except nulls (if any).
To make it more dynamic you may try-
=QUERY(INDEX(FLATTEN(SPLIT(REPT(A1:INDEX(A1:A,COUNTA(A1:A))&"#",B1:INDEX(B1:B,COUNTA(A1:A))),"#"))),"where Col1 is not null")
And with new LAMBDA() functions.
=FLATTEN(SPLIT(JOIN("",BYROW(A1:INDEX(B1:B,COUNTA(A1:A)),LAMBDA(x,REPT(INDEX(x,1,1)&"|",INDEX(x,1,2))))),"|"))

Sequential number based on repeating value in another column - Google Sheets

this one seems super simple but I'm having a tough time figuring it out, any help would be greatly appreciated.
I have repeating data in Column A, in Column B I need sequential numbering unless the previous row has a repeat value, in which case it would repeat that number in the sequence. Example below.
Is this possible in a single cell array formula?
Column A Column B
7648490 1
7634199 2
7631608 3
7620465 4
7620465 4
7616976 5
7601241 6
7601241 6
7601241 6
7601241 6
7599651 7
7597439 8
7597376 9
7596068 10
7596068 10
7596068 10
7596068 10
7596068 10
7596067 10
Delete everything from Col B (including the header) and place the following formula in B1:
=ArrayFormula({"Header";IF(A2:A="",,VLOOKUP(A2:A,{UNIQUE(FILTER(A2:A,A2:A<>"")),SEQUENCE(COUNTA(UNIQUE(FILTER(A2:A,A2:A<>""))))},2,FALSE))})
This will create header text (which you can change as you like within the formula itself) and will produce the result for each row.
The virtual array formed between the curly brackets { } creates a pairing of each UNIQUE value from Col A with an incremental SEQUENCE that starts at 1. Then VLOOKUP just finds each actual value from Col A within the virtual array and returns the SEQUENCE number.

How to Repeat a Data Set (several columns) x times in Google Sheets [duplicate]

This question already has answers here:
Repeat whole row N times based on column value in Google Sheets
(2 answers)
Closed 5 months ago.
The idea here is to create a backup table to enable a faster filling of repeated information in another worksheet.
Dataset:
Exam Parameter Step System Samples
b-HCG OD Calibration 1 5
TSH OD Calibration 2 3
where Col1 = Exam, Col2 = Parameter, Col3 = Step, Col4 = System
enter image description here
So I've been trying to repeat each line x times. X is defined by the nº of samples in each analysis and I would like to return the repeated set in a merged table. For example, all info in row 1 repeated 5 times, row 2 repeated 3 times and so on.
To make a dynamic formula, I tried the following:
=TRANSPOSE(SPLIT(REPT(B3&"|";F3);"|"))
=TRANSPOSE(SPLIT(REPT(C3&"|";F3);"|"))
=TRANSPOSE(SPLIT(REPT(D3&"|";F3);"|"))
=TRANSPOSE(SPLIT(REPT(E3&"|";F3);"|"))
enter image description here
By this I can get the repeated set but just for the first row. If I try to autofill the remaining rows with the above formula, the original formula is overwritten, and I get the repeated data set for the 2nd row instead.
Can I solve this with native formula only or is this only manageable by JavaScript?
try:
=INDEX(SUBSTITUTE(SPLIT(FLATTEN(SPLIT(QUERY(REPT(FLATTEN(
QUERY(TRANSPOSE(SUBSTITUTE({"♦"&A2:A, B2:D}, " ", "♠")),,
9^9)), E2:E),,9^9), "♦")), " "), "♠", " "))

Google Sheets Query Group By / First-N-Per-Group

I'm trying to find a simple solution for first-n-per-group.
I have a table of data, first column dates and rest data. I want to group based around the date, as multiple entries per date are allowed. For the second column some numbers, but want the FIRST record.
Currently the aggregate function I could possibly use is MIN() but that will return the lowest value and not the first.
A B
01/01/2018 10
01/01/2018 15
02/01/2018 10
02/01/2018 2
02/01/2018 100
02/01/2018 20
03/01/2018 5
03/01/2018 2
Desired output
A B
01/01/2018 10
02/01/2018 10
03/01/2018 5
Current results using MIN() - undesired
A B
01/01/2018 10
02/01/2018 2
03/01/2018 2
It's a shame there isn't a FIRST() aggregate function in Google Sheets, which would make this a lot easier.
I saw a couple of examples of using the Row Number and ArrayQuery, but that doesn't seem to work for me. There are about 5000 rows of data so trying to keep this as efficient as possible, and not have to recalculate the entire sheet on any change, each taking a few seconds.
Currently I have this, which appends a third column with the Row Number:
=query({A1:B, arrayformula(row(A1:B))}, "select min(Col1),min(Col2) group by Col1")
Thanks
EDIT 1
A suggested solution was =SORTN(A:B,2^99,2,1,1), which is a clean simple one. However, this requires a large range of "free space" to display the returned dataset. Imagine 3000+ rows.
I was hoping for a QUERY() -based solution, as I wanted to do further operations with the results. Specifically, count the occurrences of distinct values.
For example: I wanted a returned dataset of
A B
01/01/2018 10
02/01/2018 10
03/01/2018 5
Yet I want to count the occurrences of those values (and then ignoring the dates). For example:
B C
10 2
5 1
Perhaps I've confused the situation by using numbers? the "data" in ColB is TEXT (short 3 letter codes), however I used numbers to show I couldn't use MIN() function as that returns the numerically lowest value.
So in brief:
Go through all rows (3000+ rows) and group by the FIRST row of a particular date
return the FIRST value of that row
COUNT() all unique occurrences of those FIRST values, disregarding the date. Just a list with the unique values and their count (again, only the first one of any particular day)
=SORTN(A:B,2^99,2,1,1)
If your data is sorted as in the sample, You can easily remove duplicates with SORTN()

Resources