How to make criteria in AVERAGEIFS update automatically? - excel-formula

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)

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.

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

Excel IF + AND + Date range formula

I am looking to create and IF function that does the following.
There is a ton of data with one column containing dates. I want and if functions that labels each row according to the following.
If date falls between 0-30 days of todays date in the past then label "GOOD" (so if todays date is 21/09/2017 then it should be labelled as "GOOD" should it falls between the dates 21/09/2017 and 21/08/2017)
If date falls between 31-60 days of todays date in the past then label "FAIR"
If date falls between 61-90 days of todays date in the past then label "ATTENTION"
If date falls between 91+ days of todays date in the past then label "CLEARANCE"
Hope someone can help.
Many thanks
Use
=IF(TODAY()-A2<31,"Good",IF(TODAY()-A2<61,"Fair",IF(TODAY()-A2<91,"Attention","Clearance")))
Column D shows the difference between today date and cell date.
Alternative Answer
Use VLOOKUP to potentially ease your future formula maintenance. In an unused location, set up a table that has your break point ranges and associated return values. For this example I used the following:
Technically speaking column G is not required, but it can be easier for some people to read.
Now assuming your dates are in Column A, you can use the following formula in B2 copying down:
=TODAY()-A2
and in C2 use the following look up formula and copy down to get your desired results:
=VLOOKUP(B2,$F$3:$H$6,3,1)
now if you are not keen on generating the extra column for calculate the number of days, you can substitute that first formula into the second to get:
=VLOOKUP(TODAY()-A2,$F$3:$H$6,3,1)
place the above in B2 instead and copy down.
The following is an example of the first approach:
The main advantage to this approach is you can manipulate the lookup table easily changing breakpoints, wording of results etc easily without touching your formula (when done right)
if you have the potential for negative days, instead of returning an error, you could wrap the lookup formula in an IFERROR function to give a custom message or result.
=IFERROR(VLOOKUP(B2,$F$3:$H$6,3,1),"In the Future")
Assuming your data starts with A2, A3 and so on.. as below
Apply the below formula in B2 and drag down up to A8
=IF(AND(--TEXT(TODAY()-A2,"##")>=-1,--TEXT(TODAY()-A2,"#")<30),"GOOD",IF(AND(--TEXT(TODAY()-A2,"##")>=30,--TEXT(TODAY()-A2,"#")<60),"FAIR",IF(AND(--TEXT(TODAY()-A2,"##")>=60,--TEXT(TODAY()-A2,"#")<90),"ATTENTION",IF(--TEXT(TODAY()-A2,"##")>90,"CLEARANCE","FUTURE DATES"))))

MEDIANIF formula using months

I've written the following "MEDIANIF" array formula to give me the median of values (percentage returns) from column B that correspond to months in column A. In other words, I want the median of all values that correspond to January, February, March, etc...
This formula works perfectly for all months, except for January. I've manually verified it is working fine for all other months, but for whatever reason, it just returns 0 for January. This isn't a formatting issue or anything like that (everything is formatted consistently). I cannot for the life of me figure out why it won't work for January.
=MEDIAN(IF(MONTH(Data!$A$4:$A$65536)=1,Data!$B$4:$B$65536))
I'd post an image, but this is my first post and I can't post pics until i have at least 10 reputation!
The data is arranged with calendar months in column A (last trading day of the month) and percentage returns (formatted as a percent) in column B.
Any help is very much appreciated!
Thank you!
Excel will treat blank cells as zero (in this sort of formula, at least) and zero will be deemed to be a date in January (January 1900) so any blank rows will contribute a zero to the January median calculation; that's why you get incorrect results - add another check to ensure that the cells aren't blank, i.e.
=MEDIAN(IF(Data!$A$4:$A$65536<>"",IF(MONTH(Data!$A$4:$A$65536)=1,Data!$B$4:$B$65536)))
Alternatively you could limit the formula to rows with data - perhaps employing dynamic named ranges if the amount of data is variable

Month-to-date totals

I have an Excel workbook that has several different worksheets in it. The one worksheet that contains the main data has 100's of entries. This sheet also gets a new entry every day in the row column. The first column is the date column that is formatted as date and time.
I need a formula that checks the newest entry's date to see if its month matches the current month and then sums all entries for that month. I know that I am going to use a SUMIF but the hard part is the rest of the formula. I forgot to mention that I need the formula to divide by the number of current month entries used.
Assuming your dates are in A1:A26 and amounts to be summed are in in B1:B26 here is the formula to use.
=SUMIFS(B1:B26,A1:A26,">="&DATE(YEAR(TODAY()),MONTH(TODAY()),1),A1:A26,"<="&TODAY())
This returns the amounts that are in a date that is greater than or equal to the first day of the current month, AND less than or equal to TODAY.
There are a couple ways you could do this and I would suggest using sumproduct formula.
I'm assuming your data has dates in column A starting in row 2, and a value in column B.
In column C you could have a total in each line which is the sum of all rows above the current one that the month matches.
Formula in C2 would be below which can be carried down
=SUMPRODUCT(--(MONTH(A2)=MONTH($A$2:A2)),$B$2:B2)
Otherwise if you have a single cell and just want to get the latest total this formula would do it. This assumes the latest entry will also be the newest date (highest value)
=SUMPRODUCT(--(MONTH(MAX(A:A))=MONTH(A2:A1000)),B2:B1000)
In sumproduct the first part of the formula --(A=B) returns true or false values which equate to 1s and 0s. The second part is just your value columns.
The second formula does the same thing but it compares the month of the newest date to all others in the column.
Gordon
Since new entries are added daily I am assuming these are in date order ascending, so please try:
=SUMIF(A:A,">"&EOMONTH(TODAY(),-1),AD1:AD100)
or adjust the AD range to suit (could use AD:AD).

Resources