AverageIf Moving Average - excel

I'm trying to find a 5yr Moving Average that takes a condition of the month of the year. Column A contains daily dates from 1/1/2010 all the way to today's current date. Column B contains a daily quantity. Basically I want to average all the days for January in a rolling 5 year period. I could easily do this as a static number, but this sheet will be updated daily.
Ex:
Date Quantity
1/1/2010 14,113
1/2/2010 15,364
etc...
I've written this formula:
=AVERAGEIFS(OFFSET(B2,COUNT(B2:B10000)-1826,0,1826,1),A2:A10000,MONTH(A2:A10000)=1)
This returns #VALUE!

You will not be able to use AVERAGEIFS(). The AVERAGEIFS only do simple iteration, no conversion.
I prefer to use INDEX to get the range since it is not Volatile, when setting variable ranges.
Also all the ranges must be the same size. So limiting Column B to 1826 cells but not limiting Column A to the same 1826 cells will cause a second error.
Use this array formula:
=AVERAGE(IF(MONTH(INDEX(A:A,MATCH(1E+99,B:B)-1826):INDEX(A:A,MATCH(1E+99,B:B)))=1,INDEX(B:B,MATCH(1E+99,B:B)-1826):INDEX(B:B,MATCH(1E+99,B:B))))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
You can also use the dates themselves instead of a hard coded 1826 days. This will allow for leap year.
=AVERAGE(IF(MONTH(INDEX(A:A,MATCH(DATE(YEAR(TODAY())-5,MONTH(TODAY()),DAY(TODAY())),A:A,0)):INDEX(A:A,MATCH(TODAY(),A:A)))=1,INDEX(B:B,MATCH(DATE(YEAR(TODAY())-5,MONTH(TODAY()),DAY(TODAY())),A:A,0)):INDEX(B:B,MATCH(TODAY(),A:A))))

Here's another option
=AVERAGE(IF(A2:A5000>EDATE(TODAY(),-60),IF(MONTH(A2:A5000)=1,B2:B5000)))
confirm with CTRL+SHIFT+ENTER
This doesn't try to find a specific range of data, just looks at all the data and averages rows where the date is in the last 5 years and also in January. There's some extra overhead looking at all rows but you have a relatively small amount of data so I wouldn't expect that to be significant

Related

Excel - Get hourly averages for data spanning 1 year (8760 data points)

I have an excel document with values for every hour for every day of a year.
In total there are 365*24=8760 values.
I would like to get the average value at every hour, (etc at 00:00, 01:00, 02:00..)
I was thinking maybe to sum every 24th value and then divide it by 365 but I don't know how to sum every 24th value.
Does anyone know how to do that?
Thanks for any help!
The clearer way of doing it is using a column to get the hour of each data point and adding a AVERAGEIF formula to calculate the average of every hour.
I am assuming your date/time is on A2:A8761 and the values on B2:B8761.
On C2, add the formula =HOUR(A2) and drag it down to find the hour of every data observation (for newer excel versions you can also
do =HOUR(A2:A8761) and the formula will automatically spill down). The results are numbers from 0 to 23.
Building the summary table:
Add a column listing the numbers 0 to 23 to represent the 24 hours. I did it on column E2:E25. Note that those are numbers, not date/time.
On F2, add the formula =AVERAGEIF($C$2:$C$8761,E2,$B$2:$B$8761) to calculate the average of the hours listed on column E. Then, drag it down. Finally, you will have the values you need on F2:F25.
Find bellow my result.
Please let me know if you have any question so I can improve the answer.

Calculating total monthly precipitation values using SUMIF function to specify date window

I've got an excel sheet with dates in one column (daily) and total precipitation values for that date in another column from 01/01/2000 to 31/12/2021.
I want to calculate the total monthly precipitation values from 2000 to 2021.
The formula I have been using after some research online was (which I was planning on iterating over the months):
=SUMIFS(B2:B8036,C2:C8036,"<="&DATE(2003,9,1),C2:C8036,">="&DATE(2003,9,30))
The values I want to sum (precipitation) are in the B column, the dates are in the C column. However, I get a zero value returned in the cell when I run the formula, even though the actual value should be 131.94.
I'm not sure what mistake I made in my formula.
My operators were wrong, changing it to
=SUMIFS(B2:B8036,C2:C8036,">="&DATE(2003,9,1),C2:C8036,"<="&DATE(2003,9,30))
solved my problem.

Incrementing a Cell Reference By x

I am trying to average a large data set within Excel where there is data by 30 minute periods for each day of the month. I have a header for 'start date' and 'Start time' which displays 00:00:00 - 23:30:00 for each day, with the data in question in columns to the right. I am trying to develop a formula which averages data which concerns 00:00:00 - 11:30:00 for each day and 12:00:00 - 23:30:00 for each day (AM:PM).
I have one header for AM values and another for PM values, and Im looking to average both these elements for each day of the month
I have used the following formula which does work:
=AVERAGE(OFFSET(C6,24,0,-24))
This formula capture the range from 00:00:00 - 11:30:00 and averages it like I want, but when I drag the formula down to the next cell, C6 increments to C7, but I want it to increment by 48 each time, meaning C6 should become C54. This means when I drag it down each time it will average the AM values for 02/01/2019 and so on.
Is there any way the cell reference in the above formula can increment by 48 instead of 1 each time it is dragged down into another cell?
Sample Data
Expected output
I made up a dataset kind of like yours:
The formula I've used in F4 to obtain the average of values, when date is '01/01/2019' and time is AM, is AVERAGEIFS:
AVERAGEIFS function
My formula is:
=AVERAGEIFS($C$6:$C$101;$A$6:$A$101;E4;$B$6:$B$101;"<"&0,5)
You can use it and just drag down. As you can see in the image above, the formula returns 27,07
The formula will work only with AM because the criteria is "<"&0,5. In the column where you want to do the average of PM times, we would use the same, but changing criteria to ">="&0,5, this means:
=AVERAGEIFS($C$6:$C$101;$A$6:$A$101;E4;$B$6:$B$101;">="&0,5)
Hope you can adapt this to your needs.
NOTE: Because your data is in a Pivot Table (you never mentioned that), if the Pivot Table changes, you'll neeed to adapt the formula. A solution would be using ranges from row 6 until last one, so the formula will take always all the rows. Remember to not show total row or it may affect the result.
OPTION 2: In case you can add an extra column to original data (not in the Pivot Table), maybe this can help a lot.
I've used same data than before, but I added an extra column, named AM/PM with a formula on it:
=IF(B6<0,5;"AM";"PM")
It looks like this:
Then I created a Pivot Table based on it, with a configuration:
Field Start date to section of rows.
Field AM/PM to columns.
Field VALUE to values section, but instead of suming up, I did average operation.
I get this on my Pivot Table:
As you can see, AM for 01/01/2019 is 27,07 (the oher numbers are based on random numbers I made up).
Hope this can help.
If you absolutely must go with the AVERAGE(OFFSET()) solution I would have a 'helper' cell (in this case J6) with 1 in it, then J7 have =J6+48, then the main formula could be
=AVERAGE(OFFSET($C$6,J6,0,24))
Then when you copy the formula down, you'll also need to copy down from J7.
The Rows argument is purpose built for what you want to do. Going to efforts to change the reference cell defies this purpose.
Edited because I missed the AM/PM split:
I think, however, AVERAGEIFS() will fit your requirement better. This takes the average of all values in column C where the date in column A matches that in cell G6 (Change this to wherever your output is). This will avoid errors should any of your dates have any half hour periods missing.
=AVERAGEIFS($C:$C,$A:$A,G6,$B:$B,"<"&TIMEVALUE("12:00:00"))

How to make criteria in AVERAGEIFS update automatically?

I am trying to make my AVERAGEIFS criteria update automatically according to the day of the year (in this case only for 2014). My original AVERAGEIFS formula works perfect =AVERAGEIFS($B$2:$B$35041,$A$2:$A$35041,">=1/1/2014",$A$2:$A$35041,"<1/2/2014"), but I have to manually edit it for each day of the year.
I have tried modifying formulas I have seen in other posts here, but no luck so far. I want to average the values in column B (Amount) for each day in column A (Date). I have 96 time slots for each day of the year, hence the 96 entries in column A with the same date, e.g. 1/1/2014 and so on until 12/31/2014. How can I modify the formula so that it automatically adjusts the day when I drag it down?
Here is a screenshot of the data:
Screenshot of data
Any help is much appreciated!
EDIT: Updated formula below; I had misread the original question as to how your data was formatted. You can put this formula in B2 and copy down.
=AVERAGEIFS($B$2:$B$35041,$A$2:$A$35041,">="&$A2,$A$2:$A$35041,"<"&($A2+1))
Your original comparison operator hardcoded a date as a string. We can simply concatenate the value of a cell into that comparison string instead.
Granted, this will do the same calculation for each of the 96 entries for each of the days in your sheet. You could add a second sheet for the averages with just the distinct days of the year listed in A and an average calculation in B.
EDIT 2:
If your entries in column A have fractional date information (i.e. with hours, minutes, and seconds), converting the dates to integers will drop any fractional component:
=AVERAGEIFS($B$2:$B$35041,$A$2:$A$35041,">="&INT($A2),$A$2:$A$35041,"<"&INT($A2)+1)

How To Ignore Blank Cells for a Formula

I'm tracking hours for my company and I need to see the updated hours daily. I have everything formatted how I want it, except I want two columns to show how many hours over and under each employees current hours are.
What I have right now is
=(D3-C3)+(F3-E3)+(H3-G3)+(J3-I3)+(L3-K3)+(N3-M3)+(P3-O3)
But it is including all the empty cells (for days that haven't been worked yet) as a zero.
I want a formula that can allow me to ignore those blank cells until they have content.
I can't just use a SUMIF >0 function because I need to count the number of hours employees have MISSED (i.e. scheduled 12 hrs, actually worked 0).
Here's an alternate approach to #Tom's, though it works on similar principles. This one, however, relies on your ability to add a couple of 'HELPER' rows, above your current data.
I'm assuming that row 1 will alternate between saying "PROJECTED", and "ACTUAL".
I'm assuming that row 2 will be dates for that week, in Date format. So A2 will be Jan 1 2015, B2 will be Feb 1 2015, C3 will be Feb 1 2015, or however often the time blocks go up. The key here is that the PROJECTED columns and the ACTUAL columns will each need the date in them.
The formula to check the variance between that row's PROJECTED amount and that row's ACTUAL amount, only for dates prior to today, is (for row 3 and copied down, and assuming the data goes to column Z):
=SUMIFS(A3:Z3,$A$1:$Z$1,"PROJECTED",$A$2:$Z$2,"<"&TODAY())-SUMIFS(A3:Z3,$A$1:$Z$1,"ACTUAL",$A$2:$Z$2,"<"&TODAY())
This checks to see the value of PROJECTED columns for that row, where the date is less than today, and subtracts the value of ACTUAL columns for that row, where the date is less than today.
If you are looking to compare with something other than TODAY(), you can set up a cell to be your 'comparison point'. Manual type the breakoff period you are concerned with into that cell, and replace "&TODAY()" with, say, "&AA1" [assuming your breakoff point is entered in cell AA1].
As it stands, either a very long series of IF's and OR's or an array formula:-
=SUM(IF(OR(D3="",C3=""),0,D3-C3),IF(OR(F3="",E3=""),0,F3-E3),IF(OR(H3="",G3=""),0,H3-G3),IF(OR(J3="",I3=""),0,J3-I3),IF(OR(L3="",K3=""),0,L3-K3),IF(OR(N3="",M3=""),0,N3-M3),IF(OR(P3="",O3=""),0,P3-O3))
=SUM(D3:P3*ISEVEN(COLUMN(D3:P3))*(C3:O3<>""))-SUM(C3:O3*ISODD(COLUMN((C3:O3))*(D3:P3<>"")))
The second one must be entered with CtrlShiftEnter
Note that it could easily be broken by insertion/deletion of columns.

Resources