how can I query sum across columns, but avoid duplicated submissions / rows from unique users? - google-sheets

I have a Google form accepting data from different users, which goes to a sheet and is used to SUM the values across columns at the end of the day. The problem is that users can re-submit forms to update the data totals if there is a change at the end of the day:
NAME K L M
ALF 4 0 1
BILL 1 0 0
SALLY 1 0 1
DENNIS 1 1 1
RICK 0 0 1
SALLY 2 1 1 <--- SALLY RESUBMITTED HER FORM AGAIN WITH UPDATED VALUES
In my current Query, I SUM() the columns after filtering by the date like this:
SELECT SUM(K), SUM(M), SUM(N) WHERE C = date '"&TEXT($B$1,"yyyy-mm-dd")&
$B$1 is a cell with a datepicker and col C is the user submitted form date. Col A has the unique form generated submission timestamps
As you can see, the SUM for each column will be off by the extra submission from Sally. I need to include only the most recent submissions from any of the users, and ignore any prior ones for this date. I'm not sure how to filter in this manner and sum just the most recent instance from each unique user.
** EDIT **
I should note the original form data is on another sheet and the cells are referenced via a query to this range. The form is also submitted daily, so the query must be able to specify the date in question for summation of entries.

Give a try on following formula-
=QUERY(INDEX(REDUCE({0,0,0,0},UNIQUE(J2:J7),LAMBDA(a,b,{a;SORTN(FILTER(J2:M7,J2:J7=b,C2:C7=date(2023,2,17)),1)})))," select sum(Col2), sum(Col3), sum(Col4)")
If you actually want most recent response to sum then use-
=QUERY(INDEX(REDUCE(SEQUENCE(1,COLUMNS(A2:M7),0,0),UNIQUE(J2:J7),LAMBDA(a,b,{a;QUERY(SORT(FILTER(A2:M7,J2:J7=b),1,0),"limit 1")}))),"select sum(Col11), sum(Col12), sum(Col13)")

Here you have another option, creating an auxiliary column that returns 1 if it corresponds to the date and is the last timestamp
=QUERY({K:M,
MAP(A:A,C:C,J:J,LAMBDA(ts,date,n,IF(date<>B1,0,IF(ts=MAX(FILTER(A:A,J:J=n,C:C=date)),1,0))))},
"SELECT SUM(Col1),SUM(Col2),SUM(Col3) where Col4=1")

Related

Formula to build report of employees that are not working between 2 dates

I am trying to find a way to see which employees are not working between 2 dates.
Should I use vlookup, index & match, filter, query, or something else?
Sheet 1 contains employee details & start/end dates.
Sheet 2 accepts user input to select 2 dates, and it will automatically display a list of available employees who are not working.
Sheet 1 - Database/Log of all employees and days worked.
#
A
B
C
D
1
ID
Name
Start Date
End Date
2
12345
John
01/01/2021
01/08/2021
3
54321
Sarah
01/24/2021
01/29/2021
4
00731
James
02/05/2021
02/15/2021
5
00731
John
02/10/2021
02/30/2021
Sheet 2 (Row 1-2)- Manually enter in two dates.
#
A
B
1
Start Date (Manual input)
End Date (Manual input)
2
01/01/2021
01/30/2021
Sheet 2 (Row 3+)- List of all employees that are not working between the two dates entered in Sheet 2!A2:B2 (Expected Results)
#
A
B
3
ID
Name
4
00731
James
try:
=INDEX(SUBSTITUTE(UNIQUE(QUERY(""&SPLIT(FLATTEN(IF(SEQUENCE(1, MAX(D2:D-C2:C))<=D2:D-C2:C,
"♥"&A2:A&"♦"&B2:B&"♦"&C2:C+SEQUENCE(1, MAX(D2:D-C2:C), 0), )), "♦"),
"select Col1,Col2 where not Col3 matches '"&JOIN("|", "^$",
IF(SEQUENCE(1, G2-F2)<=G2-F2, F2+SEQUENCE(1, G2-F2, 0), ))&"'", 0)), "♥", ))
demo sheet
Assuming the name of your first sheet with the full data is actually Sheet1, place the following in Sheet2 cell A4:
=FILTER(Sheet1!A2:B,Sheet1!A2:A<>"",(Sheet1!C2:C>G2)+(Sheet1!D2:D<F2))
The combined either/or condition (Sheet1!C2:C>G2)+(Sheet1!D2:D<F2) in the FILTER means "either the start date is after the range date given, or the end date is before the range date given."
The other condition of Sheet1!A2:A<>"" just rules out blank rows in the original data set. It's not strictly necessary as far as the visual results are concerned; but it keeps null rows from being added to those results, which would allow you to enter data below the results in Col A and B of Sheet2 if you wanted, or to have fewer rows in Sheet2 than in Sheet1 without the formula adding more rows to accommodate null returns.
Your posted sample data is unclear. You have two different names for the same ID (i.e., 00731). And you have an "End Date" in D5 of February 30, 2021—which is not a valid date. In any case, it's unclear whether the same person/ID may turn up twice in the original data set. My formula above assumes you will not.
If your original data list may, in fact, contain duplicates, things get a bit trickier. In that case, use the following formula instead, in Sheet2 cell A4:
=UNIQUE(FILTER(A2:B,ISERROR(VLOOKUP(A2:A,FILTER(A2:A,((C2:C>=F2)*(C2:C<=G2))+((D2:D>=F2)*(D2:D<=G2))),1,FALSE))))
Here, the inner FILTER first forms a list of all people who are working during that range, and then the outer FILTER filters in a UNIQUE set of the people who are not on that inner list (i.e., trying to VLOOKUP them returns IS(an)ERROR).

Total Sum With Vlookup

I have two spreadsheets with names and times. One is specific session times and on the second sheet, I want to sum up the total times based on each instance from sheet one.
Sheet 1: Session Time
Name | Time
David 5
Mike 2
Daniel 3
David 2
Daniel 8
Sheet 2: Total Time (The one for which I need a forumula)
Name | Total Time
David 7
Mike 2
Daniel 11
I tried a countif and vlookup but I couldn't get it match more than one instance of the name on sheet 1. I also tried this suggested formual from a suggested post but its not summing a second instance of the user name
=ARRAYFORMULA(SUM(ifna(VLOOKUP(A2, 'Sheet 1'!A3:AA16, 5, false))))
A2 = David (On Sheet 2)
Sheet 1'!A3:AA16 = List of names in columns A and all the way to AA is a series of columns with numerical values
5 = the column number from with I want to return the sum for every instance of David (2 in this example with a total of 7)
False = its not sorted
Any help is mucho appriciado!
You can use this formula in your Sheet2!B1:
={"Total Time";arrayformula(if(A2:A<>"",sumif(Sheet1!A1:A,A2:A,Sheet1!B1:B),""))}
Or simply use =arrayformula(if(A2:A<>"",sumif(Sheet1!A1:A,A2:A,Sheet1!B1:B),"")) in Sheet2!B2
Get the sum of the range based on a specific criteria using SUMIF()
Basically, get the sum of Sheet1!B1:B if Sheet1!A1:A matches the current cell being checked in arrayformula A2:A
Output:
This can be accomplished with a simple QUERY, e.g.,
=QUERY('Session Time'!A:B, "Select A, SUM(B) WHERE A Is Not Null GROUP BY A LABEL SUM(B) 'Total Time'")

Creating a list of top 5 occuring UNIQUE values (text) between 2 date ranges

I am hoping someone can help me. I am aiming to create a list of unique values (in this case email addresses) that occur the most in a column between 2 date ranges. I have 1 sheet with data (sheet 1) and another where I am creating this list (sheet 2).
So far I have managed to do this by adding a new column in sheet 1 with =IF(D2="","",COUNTIF(D:D,D2)) to the data.
Then using the below to list all unique values + count (sheet2)
=SORT(UNIQUE({'1st line appeals data'!B:B,'1st line appeals data'!D:D}))
Then using the below to select the top 5 occuring text + count (sheet 2)
=QUERY(AF:AG,"Select AF, AG where AF<>'' Order by AG Desc limit 5")
This works perfectly but what I want to do now is to add a between and to date value to the top 5.
In sheet 2, A5 I have date from value and sheet 2,A6 I have date to.
The numbers represented in A5,A6 are week numbers. I am hoping to reference them to the week numbers listed in sheet 2 something like - '1st line appeals data'!$A:$A,">="&$A$5,'1st line appeals data'!$A:$A,"<="&$A$6
Demo sheet here - https://docs.google.com/spreadsheets/d/1zMJ17LM7iA9a18WGbJ2IIuQAU_aS49cyn2K2X1bp6ts/edit#gid=0
Any help would be hugely appreciated
Thanks

Google Sheets QUERY count returning 'count' instead of numeric value

First-time sheets query user so pardon the noob question. I have a Google sheet (document) with multiple tabs. I'm trying to query across the tabs within the containing sheet. I have found that IMPORTRANGE isn't needed since the data is coming from the same source (correct me if I'm wrong please).
The query, grabbing count for specified month and year. emp1 is the tab name source's data:
=QUERY('emp1'!B2:B,"Select COUNT(B) where month(B)=3 and year(B)=2019",0)
Sample row data (B2 is starting data row, skipping headers B1):
A B C D E
Index Date Company Name Account # Verified (Y/N)
1 2019-03-15 ABC 5362 Y
month in QUERY starts from 0 but there is no zero month (Jan is 1) so you need +1 correction. do it like this:
=COUNTA(IFERROR(QUERY(emp1!B2:B, "where month(B)+1=3 and year(B)=2019", 0)))

Counting recent visits based on date range

I have one column with Name.
I have a 2nd column with Date associated with a visit.
I wanted to generate a count of how many times the person has visited in a previous number of days.
If the number is greater than X, I want to fill another column with match.
I'm having trouble figuring out how to filter out names that don't match the row, while simultaneously counting how many times that person has dates that fall within the 7 day range.
So if John visited on 1/23, 2/4, 2/6, and 2/8, and the range is 7 days, it should add "3" to the "recent visits" column next to John's 2/8 row, fill 2 into the "recent visits" column for 2/6, and 1 for 2/4 and 1/23.
There will be other rows with other names that will have the same requirements, so it would also need to filter out names that don't match John.
What I'm trying to do with this, is trigger an alert through Zapier to send an email when there is a frequent visitor match.
cell C2: =UNIQUE(FILTER(A2:A, A2:A<>""))
cell D2:
=COUNTA(QUERY(ARRAYFORMULA($A$2:$B),
"select A where B >= date '"&TEXT(TODAY()-7, "yyyy-mm-dd")&"'
and B <= date '"&TEXT(TODAY(), "yyyy-mm-dd")&"'
and A = '"&C3&"'", 0))
and drag down from D2 cell

Resources