Counting recent visits based on date range - google-sheets

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

Related

Google Sheets: Query Function - Counting unique values, but failing to show zeros

I am trying to create a function that allows me to count how many times a column has a certain phrase (i used dropdown menus in the column) and then sorting it by the month. However, if there is a month that doesn't have any of the items I am searching it doesn't populate a row in the query table showing that month had zero of the burgers, etc. Does anyone know how to have the query function show the 0 for months that did not have results.
=QUERY('2022'!A1:Z,
"select count(E)
where E= 'Burgers' and B >=date """&text(D2,"yyyy-MM-dd")&""" and B<=date """&text(D3,"yyyy-MM-dd")&"""
group by B
Label count(E) 'Burgers'")
Use the following formula in a single cell.
={INDEX(TEXT(DATE(YEAR(D3),SEQUENCE(MONTH(D3)-MONTH(D2)+1),1),"mmmm yyyy")),
INDEX(MAP(DATE(YEAR(D3),SEQUENCE(MONTH(D3)-MONTH(D2)+1),1),LAMBDA(x,COUNTIFS(Data!D2:D,"Burgers",Data!A2:A,">="&x,Data!A2:A,"<="&EOMONTH(x,0))))),
INDEX(MAP(DATE(YEAR(D3),SEQUENCE(MONTH(D3)-MONTH(D2)+1),1),LAMBDA(x,COUNTIFS(Data!D2:D,"Drinks",Data!A2:A,">="&x,Data!A2:A,"<="&EOMONTH(x,0)))))}

Count amount of cells within a specific year

What I want is for a single column (C-L respectively) to count exactly how many cells in their respective row match the same year (from P onward) as the labeled column. So L5(red) will count how many cells from P5-Z5 have "2022" and K8(pink) will count how many cells from P8-Z8 have "2021" in them.
I currently have the number manually entered into each cell, but would like to automate it so it will count the years on its own. Every string of formulas I have tried all come up as error. It was easier to get a second page to count the amount of cells that have a specific word. But now I can't get a single page to count how many cells contain the specific year date in it on its own page.
Here are various logic formulas I've tried. Each of them just comes up as error.
=COUNTIFS(!P5:cc5,(2022))
=COUNTIFS(!p5:cc5,"&2022")
=COUNTIFS(!p5:cc5,\<="2022")
=COUNTIFS(!p5:cc5,"\<=01/01/2022")
=COUNTIFS(!p5:cc5,"\>=01/01/2022",!p5:cc5,"\<01/01/2023")
=COUNTIF(!P5:cc,YEAR(2022))
=COUNTIFS(!P5:cc,"\<="&DATE(2022))
=(COUNTIF(!p5:cc,"\>="01/01/2022)-COUNTIF(!$p$5:$cc,"\>="01/01/2022))
This one is the formula I have for reading the second page to count how many times the specific name shows up. O5 is the cell with the name in it. So I was basing my year counting off this and trying to google my way through it.
=(COUNTIF('Queue List'!$B$3:$D$400,O5))
Sheet layout
As far as I understand you have 10 columns (C-L) that will have to find how many dates in P:CC are in year 2013,2014,2015... right? You can do it like with this formula in C5:
=COUNTIF(ARRAYFORMULA(YEAR($P5:$CC5)),2010+COLUMN())
You're "scanning" the year of the whole row with arrayformula, and then seeing if it matches 2022. In this case I changed 2022 with 2012+COLUMN() so you can drag it or copy and paste to the whole column and the number of column added to 2012 will "calculate" the corresponding year of each column
Another option is to create a whole array with one single formula in C5:
=MAKEARRAY(ROWS(P5:P),10,LAMBDA(r,c,IF(COUNTA(INDEX(P5:CC,r))=0,"",COUNTIF(ARRAYFORMULA(YEAR(INDEX(P5:CC,r))),2012+c))))
Obviously you can adapt it to a specific range. Right now it creates a "rectangle" of 10 columns wide (C-L) and to the bottom of the page (counting the rows between P5 to P (the end of the sheet). "r" and "c" are the number of the row and the column of each cell being calculated (C5 is Row 1, Column 1. D7 is Row 2, Column 3, etc). With INDEX you can select the row to count from the whole range (using that "r" that equals the row), and with c I use the same logic that with the other formula in order to add to 2012+1 in C, 2012+2 in D, 2012+3 in E, etc.
And COUNTA checks first if there is any value in that row, if it doesn't it leaves that row empty (so you won't have a bunch of unnecesary "0"

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

Google Sheets Lookup Row and Get Last Value

I have a Google Sheet with two sheets in it.
The first sheet is a list of songs with Title, Library #, Composer, ..., Last Performance Date
The second sheet is a list of performance dates with Title, Library #, date 1, date 2, ..., date n
Sheet 1 Example:
Title, Library #, Composer, Last Performance Date
Hip Song 1007 David {formula}
Slow Song 1002 Bob {formula}
Other Song 1004 David {formula}
Sheet 2 Example:
Title, Library #, Dates ->
Slow Song 1002 2021-01-12 2021-02-15
Other Song 1004
Hip Song 1007 2021-01-05
How can I automatically fill in the "Last Performance Date" column on the first sheet by looking up the song on the second sheet by the "Library #" and getting the last value in that row (with a variable number of dates per song on the second sheet).
With the example above, the Last Performance Date for "Hip Song" would be "2021-01-05", "Slow Song" would be "2021-02-15", and "Other Song" would be "" (an empty string or nothing).
For any given song, there could be no dates on the second sheet or a 100+ dates on that song's row.
I'd add a helper column to Sheet2 that contains the formula that calculates the "latest" date for a given song:
Sheet2
A
B
C
D
E
1
Title
Library #
Latest Date
Dates
2
Slow Song
1002
=IF(MAX(D2:E2)=0,"",MAX(D2:E2))
2021-01-12
2021-02-15
3
Other Song
1004
=IF(MAX(D3:E3)=0,"",MAX(D3:E3))
4
Hip Song
1007
=IF(MAX(D4:E4)=0,"",MAX(D4:E4))
2021-01-05
The IF(X=0,"",X) part of the formula is so that songs that don't have any dates show up as blank instead of 0 (which will get formatted as 1899-12-30).
Then, in Sheet1, use the VLOOKUP function to find the matching "latest date":
Sheet1
A
B
C
D
1
Title
Library #
Composer
Last Performance Date
2
Hip Song
1007
David
=VLOOKUP(B2,Sheet2!$B$2:$C$4,2,FALSE)
3
Slow Song
1002
Bob
=VLOOKUP(B3,Sheet2!$B$2:$C$4,2,FALSE)
4
Other Song
1004
David
=VLOOKUP(B4,Sheet2!$B$2:$C$4,2,FALSE)
Note the FALSE as the last parameter to VLOOKUP, which makes sure it will still return correct results, even if the data in Sheet2 is not sorted by Library #.
See this example spreadsheet.
if your dates (if present) are increasing with each next column try:
=INDEX(IF(B2:B="",,IFERROR(1*SPLIT(FLATTEN(QUERY(TRANSPOSE(IFNA(VLOOKUP(B2:B, Sheet2!B1:1000,
TRANSPOSE(SORT(SEQUENCE(COLUMNS(Sheet2!C1:1))+1, 1, 0))), 0)),,9^9)), " "))),,1)
demo spreadsheet
fx transcript: count columns from column with 1st date and create a sequence of number with +1 offset for each number. then sort the sequence in descending order and transpose it into row - this will be the 3rd parameter of vlookup which will return date columns in reverse order eg.: ..., Col5, Col4, Col3, Col2. so in vlookup we look for B2:B values in range Sheet2:B1:1000 and return multiple columns on each match. next, we transpose all results of vlookup and with query parameter ,,9^9 we collapse all rows of each column into a single row. next, we flatten the results and split by empty space (which was inserted after each date via query). in this stage, column with the latest date became our 1st column. next, dates are numeric values so we multiply them by 1 to catch errors of those cells that have no date only empty spaces. at last, we use simple if statement to check if B2:B range is empty. if so we return no value, otherwise we return date. and to limit the output only to 1 single column we use index to return all rows but only the first column

Count items in column range if value exists in corresponding row ranges

I have a spreadsheet which tracks weekly meeting attendance. I need to return the number of individuals who attended at least one meeting in the month, not the sum total of weekly meeting attendees. In other words, if a person attended 4 meetings in the month, the count is incremented by 1, not 4.
Names are listed in Column A, and the weeks in the month are listed in columns B-F (e.g. B2 is "Sep 2"; C2 is "Sep 9"; "D2 is "Sep 16", and so on.) When a person attends a meeting, the corresponding cell receives an "X".
So far, the only method I know I can use to return the number of unique or distinct meeting attendees is to first use a set of formulas in one column (H) to return whether an "X" is found in the corresponding rows, and then a second formula that references the range (in column H) containing the first set of formulas to return the number of TRUE results.
What I'm trying to do is use an ArrayFormula or something similar to give me the final number in just one shot. I'm currently using a COUNTIF function on values in a column range while the rows in that very range are populated using COUNTA functions.
How can I use just one formula to return the attendance count - not depending on that intermediary step/range in column H?
I can't seem to get an array formula to work correctly, and I haven't been able to find similar answers despite hours of searching. Apologies if there are similar questions already posted (I couldn't find one asking quite the same question as mine). Here's my best attempt so far:
=ArrayFormula(COUNTIF(COUNTA(B3:F17) > 0,TRUE)) ...which returns 1.
Here is an example spreadsheet with sample data.
In I22 I entered this formula
=countif(ArrayFormula(countif(if(B3:F17="X", row(B3:B17)), row(B3:B17))), ">0")
the formulas in H3:H17 are not used in this formula.
See if that works?

Resources