Countif with last 5 values - google-sheets

I am trying to calculate the last 5 and last 10 values for specific players in a set of data. The date is in column B, players names are in column K, the data I need to count is in column Z. I just want to have a formula that finds the last 5 values associated with a players name and adds them up. Is that possible? I attached the googlesheet for your review! Thank you for the help!
https://docs.google.com/spreadsheets/d/1iR44nAFSUxZ54LOH61zPOGS_6ugYIMYHEfhJM-SkV6k/edit?usp=sharing

since the dates are in descending order all you need is:
=QUERY(FILTER({K2:K, Z2:Z},
COUNTIFS(K2:K, K2:K, ROW(K2:K), "<="&ROW(K2:K))<6),
"select Col1,sum(Col2) where Col1 !='' group by Col1 label sum(Col2)''")

Related

On Google Sheets, how to reduce a table with multiple values per date to one row for each date and a total of the values for that date?

I have something like this:
date value
2022/01/02 4
2022/01/02 3
2022/01/01 2
2022/01/01 1
and I want another table computed from that which has a single row for each date and the sum of the values for that date:
Date Value
2022/01/02 7
2022/01/01 3
etc...how?
solution #1
Use a pivot table to do what you expect !
solution #2
=SORT(QUERY(A2:B,"select A,sum(B) group by A label sum(B) ''"))
with duration, use
=SORT(QUERY({A2:A,arrayformula(value(B2:B))},"select Col1,sum(Col2) group by Col1 label sum(Col2) ''"))

Google sheets Query function with Arrayformula

For each of the email id, I want to get latest 10 records by timestamp. How do I get the results with arrayformula? Query function is not important as long as I can still achieve this with arrayformula. Here is the sample data:
https://docs.google.com/spreadsheets/d/1YAHA02VM-5MXzVKhkxu_eODPKObpoz441mGX8lOFu5M/edit?usp=sharing
Try this on another sheet, row 1:
=arrayformula(query({query({Sheet1!$A:$C},"order by Col1 desc,Col2",1),{"Dupe position";countifs(query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0),query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0),row(Sheet1!$A2:$C),"<="&row(Sheet1!$A2:$C))}},"select Col1,Col2,Col3 where Col1 is not null and Col4 <= 10 order by Col1",1))
You can adjust the number of records found by adjusting Col4 <= 10, and also the final sort by altering order by Col1 at the end of the formula.
Explanation
This gets the data from Sheet1, sorts it by date desc then email asc:
query({Sheet1!$A:$C},"order by Col1 desc,Col2",1)
Then to the side of this data, a COUNTIFS() is used to get the number each time an email appears in the list above (since it's sorted desc, 1 represents the most recent instance).
countifs(<EmailColumnData>,<EmailColumnData>,row(<EmailColumn>),"<="&row(<EmailColumn>))
In place of <EmailColumnData> in the COUNTIF() is:
query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0)
In place of <EmailColumn> above, we only want the row number so we don't need the actual data. We can use:
Sheet1!$A2:$C
Various {} work as arrays to bring the data together.
Eg., {a,b,c;d,e,f} would result in three columns, with a, b, c in row 1 and d, e, f in row 2. , is a new column, ; is a return for a new row.
A final query around everything gets the 3 columns we need, where the count number in col 4 is <=10, then sorts the output by Col1 (date asc).
On second thoughts, maybe this is bit cheeky, but this might do it ( taken from conditional rank idea )
=ArrayFormula(filter(A2:C,countifs(A2:A,">="&A2:A,B2:B,B2:B)<=10,A2:A<>""))
EDIT
The above assumes (because the data is time-stamped) dups shouldn't occur. If they do and the data is pre-sorted, you can use row number as a proxy for time stamp as suggested by #Aresvik.
Alternatively, you could count separately
(a) only rows with a later timestamp
plus
(b) rows with the same time stamp but with earlier (or identical) row number
=ArrayFormula(filter(A2:C,countifs(A2:A,">"&A2:A,B2:B,B2:B)+countifs(A2:A,"="&A2:A,B2:B,B2:B,row(A2:A),"<="&row(A2:A))<=10,A2:A<>""))
I have added a new sheet ("Erik Help") with the following formula in A1:
=ArrayFormula({"Submitted Time","Email","Score";SORT(SPLIT(FLATTEN(QUERY(SORT(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(IF(Sheet1!B2:B=TRANSPOSE(UNIQUE(FILTER(Sheet1!B2:B,Sheet1!B2:B<>""))),Sheet1!A2:A&"|"&Sheet1!B2:B&"|"&Sheet1!C2:C,),,COUNTA(Sheet1!A2:A)))," ",0,1)),SEQUENCE(MAX(COUNTIF(Sheet1!B2:B,Sheet1!B2:B))),0),"LIMIT 10")),"|",1,0),1,0)})
The number of records is set after LIMIT.
The order is set by the final two numbers: 1,0 (meaning "sort by column 1 in reverse order," which, as currently set, is sorting in reverse order by date/time).

How can I separate a column into multiple columns based on values?

I have searched on a lot of pages but I cannot find a solution to my problem except in reverse order. I have simplified what I do, but I have a query that comes looking for information in my data sheet. Here there are 3 columns, the date, the amount and the source.
I would like, with a query function, to be able to make different columns which counts the information of column C based on the values of its cells per month, like this
I'm okay with the start of the formula
=QUERY(A2:C,"select month(A)+1, sum(B), count(C) where A is not null group by month(A)+1")
But as soon as I try a little different things by putting 2 query together in an arrayformula, obviously the row count doesn't match as some minus are 0 for some sources.
Do you have a solution for what I'm trying to do? Thank you in advance :)
Solution:
It's not possible in Google Query Language to have a single query statement that has one result grouped by one column and another result grouped by another.
The first two columns can be like this:
=QUERY(A2:C,"select month(A)+1, sum(B) where A is not null group by month(A)+1 label month(A)+1 'Month', sum(B) 'Amount'")
To create the column labels for the succeeding columns, use in the first row, in my example, I1:
=TRANSPOSE(UNIQUE(C2:C))
Then from cell I2, enter this:
=COUNTIFS(arrayformula(month($A$2:$A)),$G2,$C$2:$C,I$1)
Then drag horizontally and vertically to apply to the entire table.
Results:
try:
=INDEX({
QUERY({MONTH(A2:A), B2:C},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label Col1'month',sum(Col2)'amount'"),
QUERY({MONTH(A2:A), B2:C, C2:C},
"select count(Col3) where Col2 is not null group by Col1 pivot Col4")})

How to combine all the columns in into one and count them respectively in google sheet?

Data
Hi everyone,
I have 3 columns of data for the age of people in a city and also 3 columns of the count for the ages. I want to combine all the 6 columns into 2 columns which means there will be only one column of age data and one column for number of count. I try to use query function in googlesheet but not sure how to use it. Please give me some advice on this if there is other method that can achieve the same result. Thank you.
Try
=query({G8:H; J9:K; M9:N}, "Select Col1, sum(Col2) where Col1 is not null group by Col1 label sum(Col2) 'Count of people'", 1)
and see if that works?
and see if that works?

Single occurrence of a value in a column

In a column I have some values, some of which only occur once, others occur multiple times. I want to identify the values that only occur once, and then count the number of those values depending on them having a specific value in an adjacent column i.e.
Col A............Col B
John.............8
John.............2
Phil.............1
Bill.............4
Dick.............1
Dick.............2
For example, I want to find the values in column A that appear only once (i.e. Phil and Bill), and for those values, I want to identify the ones that have a value of 4 in column B
In this case, the answer to my question is 1 there is only one instance of a value in Col A that is a single occurrence, and which has a value of 4 in Col B
I want to be able to do this in Google Sheets!
=ARRAYFORMULA(QUERY(VLOOKUP(QUERY(QUERY({A2:B},
"select Col1,count(Col1)
group by Col1"),
"select Col1
where Col2=1"), A2:B, {1,2}, 0),
"where Col2=4"))

Resources