Google sheets sumif with odd data - google-sheets

I have sales data that gives me dates in a bad format. Every new sale gets automatically added to the sheet. Looks like this:
Column A
Column B
Column C
Order 1
2022-12-02T02:09:37Z
$1025.19
Order 2
2022-12-02T01:25:15Z
$873.65
This will continue on for all sales. Now the date format is UTC for whatever reason and I can't adjust that, so within this formula I have to subtract 6 hours to get it to central time. I'm trying to create an auto-updating chart that shows an average day for 7 days, so I'm trying to do a sumif formula.
Here's what I have on Sheet2:
=sumif(Sheet1!C:C,index(split((index(split(Sheet1!B:B,"T"),1)+index(split(left(Sheet1!B:B,19),"T"),2))-0.25,"."),1),A1)
Where A1 is a single date. Testing this with one date and not the range shows that it does match. When I do the range, the total comes to 0, even though multiple different dates should match. What am I doing wrong?

Assume A1 has the value: 2022-12-02T02:09:37Z
Apply this formula:
=LAMBDA(RAW,TUNEHOUR,
LAMBDA(DATE,TIME,
TEXT((DATE&" "&TIME)+TUNEHOUR/24,"yyyy-mm-dd hh:mm:ss")
)(TEXT(INDEX(RAW,,1),"yyyy-mm-dd"),REGEXREPLACE(INDEX(RAW,,2),"Z",""))
)(SPLIT(A1,"T"),-6)
returns:
2022-12-01 20:09:37
And assume you have a set of data like this:
you can apply this formula:
=ArrayFormula(
LAMBDA(DATES,AMOUNTS,START,END,DFORMAT,TFORMAT,SKIPBLANK,TUNEHOUR,
LAMBDA(DATES,AMOUNTS,DTFORMAT,START,END,
LAMBDA(DATES,TIMES,
LAMBDA(VALIDDATES,AMOUNTS,
TEXT(SUM(FILTER(AMOUNTS,VALIDDATES>=START,VALIDDATES<=END)),"$#,##0.00")
)(TEXT((DATES&" "&TIMES)+TUNEHOUR/24,DTFORMAT),IF(ISNUMBER(AMOUNTS),AMOUNTS,VALUE(REGEXEXTRACT(AMOUNTS,"^\$(.+)"))))
)(TEXT(INDEX(DATES,,1),DFORMAT),REGEXREPLACE(INDEX(DATES,,2),"Z",""))
)(SPLIT(QUERY({DATES},SKIPBLANK),"T"),QUERY({AMOUNTS},SKIPBLANK),DFORMAT&" "&TFORMAT,TEXT(START,DFORMAT)&" 00:00:00",TEXT(END,DFORMAT)&" 23:59:59")
)($B$5:$B,$C$5:$C,$B$1,$B$2,"yyyy-mm-dd","hh:mm:ss","WHERE Col1 IS NOT NULL",-6)
)
Where you enter a start date and an end date at B1 & B2 to sum up the amount with.
The provided date column will be deducted by 6 hours.
What this formula does is...
format the date column into a valid date,
compare dates from step 1 with a given start and end date as filter condition,
filter the given amount column with conditions from step 2,
sum the result of filter from step 3 as an array,
format the output as price.

Use regexreplace() and query(), like this:
=arrayformula(
query(
{
weeknum(
regexreplace(B2:B, "([-\d]+)T(\d\d:\d\d).+", "$1 $2")
-
"6:00"
),
C2:C
},
"select Col1, avg(Col2)
where Col1 is not null
group by Col1
label Col1 'week #' ",
0
)
)

I think you're trying to split the values and sum them. I can't understand fully what's the purpose of 19 in LEFT function, and why are you again splitting it? Maybe some approach similar to yours is use LEFT function with 10 characters for the date, and MID from 12th character to get the time. Then substract .25 for the 6 hours as you did, and ROUNDDOWN with 0 digits to get the only the day
=ARRAYFORMULA(ROUNDDOWN(LEFT('Sheet1'!B:B,10)+MID('Sheet1'!B:B,12,8)-0.25,0))
And then you can insert it in your SUMIF:
=SUMIF(Sheet1!C:C,ARRAYFORMULA(ROUNDDOWN(LEFT(Sheet1!B:B,10)+MID(Sheet1!B:B,12,8)-0.25,0)),A1)

Related

Sum range by month in Google Sheets with date conditional

I have a sheet with these columns:
A (Date): Range of dates for the year
B (Amount): An amount for an expense
D (Month): Name of each month in the year (e.g. June, July, etc)
I've tried this as suggested to work in other posts:
=SUMPRODUCT(B:B, ISDATE(A:A)*MONTH(A:A)=MONTH(D3&1))
I get the error Function MONTH parameter 1 expects number values. But 'Date' is a text and cannot be coerced to a number. with this.
If I remove the header, I just get a 0 for the sum for each month, which is incorrect.
How do I get this to work for each month the way my sheet is setup?
What the sheet currently looks like:
https://docs.google.com/spreadsheets/d/1_8DQTa9aXGjvd7twL6RZMcFyqxa4Sg7eVTcD2wDXH2A/edit?usp=sharing
Try this instead. Not sure where you got that other formula:
=SUMPRODUCT(B:B,TEXT(A:A,"mmmm")=D3)
use:
=ARRAYFORMULA(IFNA(VLOOKUP(MONTH(D3:D10&1),
QUERY(A2:B, "select month(A)+1,sum(B) group by month(A)"), 2, )))
Use this
=IF(D3="",,IFERROR(SUM(FILTER($B$2:$B,MONTH($A$2:$A&1)=MONTH(D3&1))),""))

Google sheets - List of dates between a list of start and end dates

I want to list all the dates between a list of start and end dates. I have used sequence formula for each row of dates but I am looking for one single formula to work irrespective of any number of rows I have. Any help is highly appreciated
While there is already an answer for this, if the same date is repeated in multiple columns, that won't be reflected in the resulting list. Therefore, if you want the dates included in multiple intervals to be repeated, here is an alternative:
=ARRAYFORMULA(QUERY(FLATTEN(IF(DAYS(B2:B4,A2:A4)>=SEQUENCE(1,1000,0),A2:A4+SEQUENCE(1,1000,0),"")),"where Col1 is not null"))
Use DAYS, SEQUENCE and IF to get a list of all the dates in between the intervals.
Use FLATTEN to put all values in a single column.
Use QUERY to remove the null values.
I entered this formula in cell E2
=sort(query(sequence(max(A2:B)-min(A2:B)+1,1,min(A2:B)),
"where "&join(" or ","(Col1>="&filter(A2:A,A2:A<>"")&"
and Col1<="&filter(B2:B,B2:B<>"")&")")&
" format Col1 'dd.mmm.yyyy'"))
First, we list all the dates from the lowest one to the greatest one in the range A2:B, then we use query to remove all those who aren't between the specified intervals.
try:
=ARRAYFORMULA(QUERY(FLATTEN(IF(DAYS(B2:B, A2:A)>=SEQUENCE(1, 10000, 0),
A2:A+SEQUENCE(1, 10000, 0), )), "where Col1>0 format Col1'dd.mmm.yyyy'"))

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

Need help creating a formula for dynamic average of last 4 weeks expenses

I am looking to create a spreadsheet that my staff fill out, it then gives me a master sheet with all the data, then I import dynamically to my financial spreadsheet telling me the average cost of my client over the last 30 days.
I am looking to create an AVERAGE formula of the last 30 days when Date = Today (Monday) (I want the weekday Monday as that's when staff hand in invoices)
Hope this makes sense, it's really tough!
Here's a video of me explaining my desired outcome
https://www.loom.com/share/3a9cb75052b246d1af2ba2f9ce9180a7
I've followed several guides & can't figure it out.
=ArrayFormula(iferror(query(average(if(today() - weekday(today(),3)-30)))))
I expected $90 average and I just get blank
You could use this formula:
=AVERAGE(VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+1,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-6,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-13,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-20,A:H,2,FALSE))
To break it down in to its component parts, the AVERAGE is taken from VLOOKUP results:
VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+1,A:H,2,FALSE)
The VLOOKUP is looking for the last Monday from the current date:
TODAY()-WEEKDAY(TODAY(),2)+1
Then
TODAY()-WEEKDAY(TODAY(),2)-6
and so on...
When using on your sheet, you will have to specify the column you want to reference in your look up, for colunm B (brand1) use: A:H,2,FALSE), for colunm C (brand2) use: A:H,3,FALSE), for colunm d (brand3) use: A:H,4,FALSE) and so on...
=INDEX(QUERY({INDIRECT("A2:D"&ROW()-1)},
"select avg(Col2),avg(Col3),avg(Col4)
where Col1 <= date '"&TEXT(TODAY(), "yyyy-MM-dd")&"'
and Col1 >= date '"&TEXT(TODAY()-30, "yyyy-MM-dd")&"'"), 2, )

How to autofill dates using arrayformula

I'm using Google sheets for data entry that auto-populates data from my website whenever someone submits to a form. The user's data imports into my sheet with a timestamp (column A).
Using the Arrayformula function, I'd like a column to autofill all the dates of a timestamp within that month. For example, if 1/5/2016 is entered as a timestamp, I'd like the formula to autofill in the dates 1/1/2016 - 1/31/2016.
Additionally, I'd like other months added in the Arrayformula column. For example, if both 1/5/2016 and 2/3/2016 are entered in column A, I'd like the formula to fill in the dates from 1/1/2016 - 2/29/2016.
I know I can manually write in the dates and drag them down the column, but I have a lot of sheets, and using an Arrayformula will save me a lot of time. I've tried a similar formula in column B, but it doesn't autofill in the date gaps. Is what I'm looking for possible?
Here's a copy of the editable spreadsheet I'm referring to: https://docs.google.com/a/flyingfx.com/spreadsheets/d/1Ka3cZfeXlIKfNzXwNCOWV15o74Bqp-4zaj_twC3v1KA/edit?usp=sharing
Short answer
Cell A1
1/1/2016
Cell A2
=ArrayFormula(ADD(A1,row(INDIRECT("A1:A"&30))))
Explanation
In Google Sheets dates are serialized numbers where integers are days and fractions are hours, minutes and so on. Once to have this in mind, the next is to find a useful construct.
INDIRECT(reference_string,use_A1_notation) is used to calculate a range of the desired size by given the height as a hardcoded constant, in this case 30. You should not worry about circular references in this construct.
ROW(reference) returns an array of consecutive numbers.
A1 is the starting date.
ADD(value1,value2). It's the same as using +. As the first argument is a scalar value and second argument is an array of values, it returns an array of the same size of the second argument.
ArrayFormula(array_formula) displays the values returned by array_formula
As A1 is a date, by default the returned values will be formatted as date too.
Increment by Month
If anyone wants to be able to increment by month, here's a way I've been able to accomplish that. Your solution #ptim got me on the right track, thanks.
Formula
Placed in B1
First_Month = 2020-11-01 [named range]
=ARRAYFORMULA(
IF(
ROW(A:A) = 1,
"Date",
IF(
LEN(A:A),
EDATE( First_Month, ROW( A:A ) -2 ),
""
)
)
)
Result
ID Month
1 2020-11-01
2 2020-12-01
3 2021-01-01
4 2021-02-01
5 2021-03-01
I have an alternative to the above, which allows you to edit only the first row, then add protection (as I like to do with the entire first row where I use this approach for other formulas):
=ARRAYFORMULA(
IF(
ROW(A1:A) = 1,
"Date",
IF(
ROW(A1:A) = 2,
DATE(2020, 1, 1),
DATE(2020, 1, 1) + (ROW(A1:A) - 2)
)
)
)
// pseudo code!
const START_DATE = 2020-01-01
if (currentRow == 1)
print "Date"
else if (currentRow == 2)
print START_DATE
else
print START_DATE + (currentRow - 2)
Notes:
the initial date is hard-coded (ensure that the two instances match!)
ROW(A1:1) returns the current row number, so the first if statement evaluates as "if this is Row 1, then render Date"
"if this is row 2, render the hard-coded date"
(nB: adding an integer to a date adds a day)
"else increment the date in A2 by the (adjusted) number of rows" (the minus two accounts for the two rows handled by the first two ifs (A1 and A2). Eg: in row 3, we want to add 1 to the date in row 2, so current:3 - 2 = 1.
Here's a live example (I added conditional formatting to even months to assist sanity checking that the last day of month is correct):
https://docs.google.com/spreadsheets/d/1seS00_w6kTazSNtrxTrGzuqzDpeG1VtFCKpiT_5C8QI/view#gid=0
Also - I find the following VScode extension handy for syntax highlighting Google Sheets formulas: https://github.com/leonidasIIV/vsc_sheets_formula_extension
The Row1 header trick is courtesy of Randy via https://www.tillerhq.com/what-are-your-favorite-google-spreadsheet-party-tricks/
nice. thanks.
To get the list length to adapt to the number of days in the selected month simply replace the static 30 by eomonth(A1;0)-A1. This accommodates for months with 31 days, and for February which can have either 28 or 29 days.
=ArrayFormula(ADD(A1,row(INDIRECT("A1:A"&eomonth(A1;0)-A1))))
Updated for 2022:
This can now be done pretty easily with the SEQUENCE function, it's also a bit more adaptable.
Below will list all of the days in columns but you can swap the first 2 values to place in rows instead:
=SEQUENCE(1,7,today()-7,1)
More specific to your example, below will take the date entered (via cell, formula, or named cell) and give you the full month in columns:
=SEQUENCE(1,day(EOMONTH("2016-1-5",0)),EOMONTH("2016-1-5",-1)+1,1)

Resources