Related
I am working on redoing a Google Sheets spreadsheet my wife uses for work. Each sheet has a section for Monday, Tues, Wed, Thurs, and Sat (no programs on Friday and Sunday). Starting with early next year, the first sheet has Monday, January 2, Tuesday, January 3, and so on through Sunday, January 7.
What I am trying to do (instead of manually editing 52 different sheets) is come up with a function that iterates so that the second weeks' spreadsheet will show Monday, January 9, Tuesday, January 10, and so on. Each week would iterate the previous sheets corresponding day by 7.
Now, this is easy enough with a simple function, but where it gets complicated is when we get to the end of the month. I want to see if it is possible to iterate months as well. So, the week towards the end of the month would have Monday 29, Tuesday 30, Wednesday 31, but then Thursday Feb 1, and Saturday Feb 3.
Any ideas?
You can use this formula to create a list of dates in the format you want of a whole year (365 days):
=ArrayFormula(LAMBDA(FIRSTOFYEAR,MONTHS,WEEKS,
LAMBDA(DATES,
{DATES,BYROW(DATES,LAMBDA(DATE,
{
INDEX(WEEKS,WEEKDAY(DATE)),
INDEX(MONTHS,MONTH(DATE)),
DAY(DATE),
INDEX(WEEKS,WEEKDAY(DATE))&", "&INDEX(MONTHS,MONTH(DATE))&" "&DAY(DATE)
}
))}
)(BYROW(SEQUENCE(365,1,0),LAMBDA(NUM,FIRSTOFYEAR+NUM)))
)(
DATE(2023,1,1),
{"January";"February";"March";"April";"May";"June";"July";"August";"September";"October";"November";"December"},
{"Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"}
))
Result:
Which you can than use a QUERY() to easily remove what you don't need, such as:
(Assume the list of dates are placed in A:E)
=QUERY({$A:$E},"SELECT Col5 WHERE Col1 IS NOT NULL AND Col2!='Friday' AND Col2!='Sunday'",0)
result in something like this:
You can also use limit and offset to define the range to be shown in a QUERY(), such as:
(Assume the result of the last query is placed in F:F)
=QUERY(F1:F,"LIMIT 5 OFFSET "&((1-1)*5))
results:
With this formula, you only need to change the number x in ((x-1)*5) to the week number you want, and that will returns you the 5 days in the format you requested.
You can combine everything above to form something like this:
=ArrayFormula(
LAMBDA(SHOWWEEK,
LAMBDA(FIRSTOFYEAR,MONTHS,WEEKS,
LAMBDA(DATES,
LAMBDA(DATA,
LAMBDA(RESULTS,
RESULTS
)(QUERY({DATA},"SELECT Col5 WHERE Col1 IS NOT NULL AND Col2!='Friday' AND Col2!='Sunday' LIMIT 5 OFFSET "&((SHOWWEEK-1)*5),0))
)(
{DATES,BYROW(DATES,LAMBDA(DATE,
{
INDEX(WEEKS,WEEKDAY(DATE)),
INDEX(MONTHS,MONTH(DATE)),
DAY(DATE),
INDEX(WEEKS,WEEKDAY(DATE))&", "&INDEX(MONTHS,MONTH(DATE))&" "&DAY(DATE)
}
))}
)
)(BYROW(SEQUENCE(365,1,0),LAMBDA(NUM,FIRSTOFYEAR+NUM)))
)(
DATE(2023,1,1),
{"January";"February";"March";"April";"May";"June";"July";"August";"September";"October";"November";"December"},
{"Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"}
)
)(1)
)
in which all you need to do is changing the number inside the last () to the number of week you want, the 5 dates in the format you requested will be returned.
don't forget that you can always use INDEX() to define which value of an array do you want to return for your final result.
Since you have mentioned that What I am trying to do (instead of manually editing 52 different sheets) is come up with a function that iterates so that...,
We can go one step further, add a simple apps-script to get the sheet number count, we assume that your file only have 52 sheets, 1 for a week, and the sheets are arranged in ascending order (otherwise you will need another method), add this script into your spreadsheet:
function getSheetNum() {
const sss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (const [index,sheet] of sheets.entries()) {
if(sheet.getName() === sss.getActiveSheet().getName()) return index;
}
}
This function returns the index of sheet you are currently working on, start from 0, you can call it from your spreadsheet by simply type in =getSheetNum() in any cell.
p.s. if you have no idea what is apps-script, you may want to read some documentation: https://developers.google.com/apps-script/overview
Combining this custom function with our formula, like this:
=ArrayFormula(
LAMBDA(SHOWWEEK,
LAMBDA(FIRSTOFYEAR,MONTHS,WEEKS,
LAMBDA(DATES,
LAMBDA(DATA,
LAMBDA(DATA,
DATA
)(QUERY({DATA},"SELECT Col5 WHERE Col1 IS NOT NULL AND Col2!='Friday' AND Col2!='Sunday' LIMIT 5 OFFSET "&(SHOWWEEK*5),0))
)(
{DATES,BYROW(DATES,LAMBDA(DATE,
{
INDEX(WEEKS,WEEKDAY(DATE)),
INDEX(MONTHS,MONTH(DATE)),
DAY(DATE),
INDEX(WEEKS,WEEKDAY(DATE))&", "&INDEX(MONTHS,MONTH(DATE))&" "&DAY(DATE)
}
))}
)
)(BYROW(SEQUENCE(365,1,0),LAMBDA(ROW,FIRSTOFYEAR+ROW)))
)(
DATE(2023,1,1),
{"January";"February";"March";"April";"May";"June";"July";"August";"September";"October";"November";"December"},
{"Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"}
)
)(GETSHEETNUM())
)
be noticed that, instead of ((SHOWWEEK-1)*5), we do (SHOWWEEK*5) in this version, because the custom function we created using Zero-based array indexing.
results:
It will return a different week of dates in format you requested in each sheet.
Update 2022-12-30:
While I don't really understand why your week 1 start from 2nd of Jan and last for 7 days, is it 1st of Jan considered as Week 0 in that case??
Anyway, I come up with this formula which gives you a list of week count with date range for your reference.
According to the comments, if all you need is the week count of the year, you can simply use WEEKNUM(DATE):
=ArrayFormula(
LAMBDA(FIRSTOFYEAR,
LAMBDA(DATES,
LAMBDA(WEEKS,
LAMBDA(DATA,
BYROW(ARRAY_CONSTRAIN(DATA,MAX(WEEKS),4),LAMBDA(ROW,JOIN(" ",ROW)))
)(TO_TEXT(QUERY(QUERY({WEEKS,"Week "&WEEKS&":",DATES,DATES},"SELECT Col2,MIN(Col3),'~',MAX(Col4),Col1 GROUP BY Col1,Col2",0),"OFFSET 1 FORMAT Col2'm/d',Col4'm/d'",0)))
)(WEEKNUM(DATES))
)(BYROW(SEQUENCE(365,1,0),LAMBDA(NUM,FIRSTOFYEAR+NUM)))
)(DATE(2023,1,1))
)
The formula works in the same concept as my other formulas.
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)
I have a basic spreadsheet to keep track of time spent on an activity.
The idea is to capture a time range per cell. E.g. 10:00 - 12:30 means 2 hours and 30 minutes.
Breaks can be taken during the day. On resuming, a new time range is entered in a new cell.
I want to calculate the total time per day. E.g. Mon Jun 15 is 2.5 + 4.5 = 7 hours.
The algorithm I'm thinking of is more or less
for each cell that contains time ranges for the given day
start, end = split(cell, " - ")
diff_decimal = (end - start) * 24
total += diff_decimal
But I'm not sure how to do this with spreadsheet functions.
The starting point I have is using =SPLIT(B2," - ") but I'm already blocked since I'm not sure how to handle the return value.
P.S. The problem could be simplified by having multiple "start" and "end" columns with one value per cell. But I want to try the given format, which I prefer, before trying another approach.
This is Google Sheets solution, as Excel does not have regex formulas.
Try this formula (place it in G1, column G:G formatted as number):
={
"Total";
ARRAYFORMULA(
IF(
A2:A = "",
"",
MMULT(
IFERROR(
( TIMEVALUE(REGEXEXTRACT(B2:F, "-\s+(\S+)"))
- TIMEVALUE(REGEXEXTRACT(B2:F, "(\S+)\s+-"))) * 24,
0
),
SEQUENCE(COLUMNS(B2:F), 1, 1, 0)
)
)
)
}
If you format G:G as Duration then you can remove * 24 part.
UPD
Added ignoring of the wrong formatted cells with IFERROR - just 0 in this case.
Better add this custom conditional formatting rule for B2:F to mark (in red on the screenshot) time periods in the wrong format and go fix them:
=AND(B2 <> "", NOT(IFERROR(REGEXMATCH(B2, "\b(\d|[01]\d|2[0-3]):[0-5]\d\s*-\s*(\d|[01]\d|2[0-3]):[0-5]\d\b"), False)))
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, )
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)