Spreading cost per month over duration of months in Sheets - google-sheets

I have a tricky problem in Sheets.
I need a formula to calculate running total costs for each month from Nov 2019 on wards (column B).
Currently, my formula for B2 is:
=SUMIFS($F$2:$F$6,$E$2:$E$6,">="&A2,$E$2:$E$6,"<="&(EDATE(A2,1)-1))
Basically, this finds all values in cells F2:F6 whose dates (in column E) match that of A2.
E.g. Cell B3 is the total cost for December, so cells F3 and F6 are a match (200 + 300 = 500)
However, this does not take into account the duration of the cost (column G).
This means that the total cost for December 2019 (cell B3) should actually be 600 (because the November cost duration lasts 12 months). Meaning there is a cumulative cost for the duration of months the cost lasts for.
I am pretty much stuck on this. If anyone could help that would be great!

So as long as the date given in column A intersect between "start date" and "start date" + x months the cost per month should be applied?
So according to this, Jan-2020 will have a cost of 600 in this example:
Sant/Falskt = True/False
Green rows = date intersect between Start and End date
If that is correct then this formula should do the job, paste in B2:
=SUMPRODUCT((--(A2>=DATE(YEAR($E$2:$E$6),MONTH($E$2:$E$6),1))*--(A2<DATE(YEAR($E$2:$E$6),MONTH($E$2:$E$6)+$G$2:$G$6,DAY($E$2:$E$6))))*$F$2:$F$6)

Related

Time function in sheets

I have the data of 4000 employees in google sheets along with their shift timings (9 hour long shift) spread across 24 hours. I wish to use a formula to understand the most common timing these employees are available in the office (09:00 to 18:00). My results would be 09:00 to 11:00, 11:00 to 13:00, 13:00 to 15:00, 15:00 to 18:00, 18:00 to 22:00, 22:00 to 09:00.
I could have used this formula to derive to the value:
=IF(AND(TIMEVALUE(A2)>=TIMEVALUE("09:00"), TIMEVALUE(A2)<=TIMEVALUE("11:00")), "09:00 to 11:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("11:00"), TIMEVALUE(A2)<=TIMEVALUE("13:00")), "11:00 to 13:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("13:00"), TIMEVALUE(A2)<=TIMEVALUE("15:00")), "13:00 to 15:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("15:00"), TIMEVALUE(A2)<=TIMEVALUE("18:00")), "15:00 to 18:00",
IF(AND(TIMEVALUE(A2)>=TIMEVALUE("18:00"), TIMEVALUE(A2)<=TIMEVALUE("22:00")), "18:00 to 22:00", "22:00 to 09:00")))))
but the problem is the timings are not in the time format but they are in text format
Here's my take:
Suppose Column A has clock ins, and Column B has clock outs. Let Column D have Times starting at 00:00 and going up to 33:00 (8am next day) in 5 minute (or 30, 60 etc) increments.
Let column E be the amount of clock in and outs that an employee was in the office at the time referred to in E.
We will define E to be =COUNTIFS($A$2:$A$9999,"<="&D2,$B$2:$B$9999,">="&D2).
Next, apply some conditional formatting to highlight the most busy times.
Note that you will need only the times of day, which it sounds like you have, but you will need to convert overnight shifts to not wrap around midnight.

TIme Separator in Google Sheets

I hope everyone reading this is doing well. I am making attendance sheets on Google Sheets that also calculates the salary of the person. It is entirely automated except for one part, the part that calculates salaries.
For that I need to separate the Hours and Minutes worked so that I can calculate the salary accurately based on 60 minutes instead of the first half being in hours and the second from a percentage of 100.
It coverts the hours into days and omits the remaining hours. Please assist. Thank you!
What it does
What it should do
HOUR() returns the hour component of a specific time, so it will always return a value between 0 and 23.
In Google Sheets, times are just numbers where 1 indicates 1 day. So a duration of hh:mm:ss means hh/24 + mm/24/60 + ss/24/60/60 which means hours_in_a_day + minutes_in_a_day + seconds_in_a_day. (You can see this if you format the cell as "Number")
So, if you want to extract the hours from a duration, you have to multiply it by 24 and take the INT().
=INT(B20*24)
Spreadsheet time values such was elapsed hours are in units of days. In your spreadsheet, salary is recorded per hour. To multiply the hours by the salary, first convert the salary per hour to salary per day, and then multiply by the elapsed hours, like this:
=n((B23 * 24) * B20)
The n() wrapper is there just to get the number format right. You can also leave it out and format the formula cell as Format > Number > Currency.
See this answer for an explanation of how date and time values work in spreadsheets.

How to check if a series of timestamps are within a range of times (google sheets)

I have a 200+ rows of timestamps. I have two cells, say 4am and 10am. I'm trying to write an If statement to see which rows time is between 4am and 10am, regardless of the day, month or year.
For example, B1 = 4am and B2 = 10am
A4 = "1/1/2020 3 am"
A5 = "1/2/2020 5 am"
A6 = "3/3/2020 11 am"
etc...
So only A5 would meet the conditions
Here's my pseudo-code:
IF(AND(A4>$B$1,A4<$B$2),"in between","NOT")
but column A has a timestamp format and I need to strip off the date part and just compare the times. I'm trying to do this in google sheets.
You're going to want to compare the data after running it through TIMEVALUE.
So instead of just referencing cell A4, you'd reference TIMEVALUE(A4), for example.
This will trim the date data from the string and you'll end up getting a number between 0 and 1. For example, TIMEVALUE("12:00 PM") would return 0.5.

GoogleFinance formula to find previous historical trading day of a stock

I have been searching for a solid formula to find stock prices of "previous trading days" of a large batch of historical dates.
The formula I started with:
=googlefinance(A5, "close", B5-1)
where A5 refers to a ticker, and B5 refers to a date in the past.
I get #NA results for previous days that fall on weekends or holidays.
I need a formula that will work reliably for 365 days per year, no matter the day of the week or number of non-trading days prior. I have tried something like this:
=index(googlefinance(A5, "close", (B5-1)-2,2),2,2)
That formula works if the B5 date is the first trading day of the week, but not if it falls anywhere midweek.
I tried using WORKDAY(B5,-1) in a few ways but I realize that function can only calculate a number of workdays, not produce a date.
I need a formula that I do not have to edit and adjust cell by cell.
Here is one way that gives
the closing price of the date if it was a trading-day
the closing price of the previous trading day, if it was not a trading day.
=vlookup(B5+16/24,googlefinance(A5, "close", B5-7, 7),2,true)
Here is how it works:
googlefinance(A5, "close", B5-7, 7) gets the data for 7 trading days starting from the date 7 days ago.
vlookup get the close price on the date closest to the date in B5 if B5 is a non-trading day.
B5 + 16/24 is to match the date with the closing price date stamp of 4pm.

How to SUM duration in Google Sheets?

Time started time end Duration
6:02:53 PM 6:11:07 PM 0:08:13
6:11:22 PM 6:20:33 PM 0:09:11
6:20:48 PM 6:32:21 PM 0:11:34
6:32:44 PM 6:39:04 PM 0:06:20
6:39:28 PM 7:00:41 PM 0:21:13
7:01:00 PM 7:09:16 PM 0:08:16
7:09:40 PM 7:16:03 PM 0:06:23
7:16:03 PM 7:24:21 PM 0:08:17
7:24:45 PM 7:30:57 PM 0:06:12
7:31:27 PM 7:37:21 PM 0:05:54
7:37:21 PM 7:44:06 PM 0:06:45
I want sum of all duration entries in x hours x minutes x seconds like i have more then 1000 rows of duration when i try to use =SUM(C2:C100) I am not getting sum of total duration after sum of 24:00:00 24 hours it starts from 00:00:00
for example sum of total duration gets 24:00:00 between range of c1:c8 it will start from 00:00:00 from c9: next range kindly assist me how to overcome this issue
try:
=ARRAYFORMULA(TEXT(SUM(IFERROR(TIMEVALUE(C:C))), "[h]:mm:ss"))
spreadsheet demo
Wherever you put the =SUM(), Select that cell and do Format>Number>More Formats>Custom Number formatting, and put the same formatting that Player0 put in his answer:
What worked for me to resolve a similar problem was a suggestion by user ttarchala in Google Sheets Query multi condition sum of time duration.
I used N() function as he said, and my final formula for the duration is:
=IF(To<>"", N(To-From+(To<From))*24, "")
with To and From being Named ranges for End Time and Start Time respectively.
N() function converts the time delta into a number. Multiplied by 24, this gives the hours in decimal format, such as 2 hours 30 minutes = 2.5 hours.
From there on, there is no problem with using the built-in Sum function to calculate the total duration as a decimal. Such as, the total duration of 27 hrs 10 minutes is shown as 27.16. This sufficed for my purposes.
Time delta is calculated using a formula from https://webapps.stackexchange.com/questions/104829/calculate-time-difference-between-times-past-midnight to take into account past-midnight differences.
And the first condition, To<>"", makes sure the formula is not showing in empty cells. As soon as the End Time is filled into "To" column, the decimal duration is calculated. Then it can be used in the regular Sum function.
This seemed shorter and easier than the formulas suggested above so I am sharing it in the hopes it may help someone else. Using thus formula, I just added up the Sum of time I spent looking for this solution: 3.34 hours :)
It's a formatting problem. You formatted your reply as HH:MM:SS, therefore the number displayed is not showing the date, which would have been incremented by one. If you multiply your sum by 24, and then format the result as a pure number, you will get a number that goes above 24, and will show you the number of hours, and its decimals. If you use those hours in further calculations, the result will be correct.
In cell C1, use the formula
=IF((B1-A1)>=0, B1-A1, 1+B1-A1)
Explanation: the problem is durations that exceed the 24 hour limit, as you say.
Google Sheets has become a bit deceptive here, as it will show the correct duration for the individual time interval, but if you SUM over it, it will actually deduct the value!
A B C
23:39 1:10 1:31
When you SUM then Google Sheets will see the value in cell C1 as if it was the beginning of the same day as the time in A1. So when you in C1 do =B1-A1 then it will register as a negative duration! But it won't show up as that!
In C1 use this formula, =IF((B1-A1)>=0, B1-A1, 1+B1-A1) for individual cells in column C, when you see that cells in column B has exceeded the 24-hour limit once. The duration in C1 should still show 1:31, but now the result when doing SUM over a range of cells in column C, like =SUM(C1:C2), will now show the correct and strictly additive sum. You can safely copy this formula to all cells in column C.
PS: cells in all of the columns can have Automatic or no formatting (which I think defaults to Automatic), if your time inputs look like the above. So you don't need to format all of those cells to Time or Duration. BUT remember to format the SUM cell to Format -> Number -> Duration.
PPS: if you are manually inputting the times (for for instance time tracking), then the easiest way to keep the much simpler =B1-A1 formula is to split the time up into two rows, like this:
A B C
23:39 0:00 0:21
0:00 1:10 1:10
Then the SUM of cells in column C still becomes 1:31.

Resources