How to calculate moving average 7 days of milk production of a each cow, Dates not sorted. Excel - excel

The daily milk production of each cow is recorded daily. I would like to cancel out the noise. Therefore I would like to use a moving average of seven days ( 3 days before and 3 days after, including current date production) in Excel.
The data is not ordered according to date therefore offset function cannot be used.
Additional Information: "Date" is in Column A, Cow No in Column B and Milk in Column C . Rows are from 1-22.
The expected result would be get get a moving average of milk for 7 days of each cow ( -3 days before <=current date and < days+3 days after current date from each cow.
Similiar function used to calculate rolling average of past year is =AVERAGEIFS(B2:K2,B1:K1,">"&TODAY()-365,B1:K1,"<"&TODAY()). A similiar formula could be used.
Problem when using sumproduct formula: No Data for 7 days should be 0 7D Average. However is given 1.79 .Rolling AVerage with the Sumproduct formula Problem

You can try to use a combination of SUMPRODUCT and AVERAGE functions. Note that this is an array formula, so you need to enter it using Ctrl+Shift+Enter keys:
=SUMPRODUCT(AVERAGE(IF(($B$2:$B$22=B2)*($A$2:$A$22>=(A2-3))*($A$2:$A$22<=(A2+3)),$C$2:$C$22)))
Result:
The average for the first three entries (D2:D4) is exactly the same as the value in column C, as there are no other dates provided for the range between 23-29 January. The remaining values (D5:D22) should properly present the 7-day average values.

This may not be the pretiest solution but it works. We can use countifs in the following way:
1. Create a column that combines Cow no and Date (I called it CowNoDate) with the format CowNo + "-" + date(as number). I put it in column C.
2. Sort list by this new column
3. use the following formula in column E to calculate the running average:
=SUMIFS(D:D,C:C,">="&B2&"-"&(A2-3),C:C,"<="&B2&"-"&(A2+3))

Related

Excel - counting how many consecutive 0s in a row

I have multiple ‘people’ in rows in excel, with 10+ columns of system usage data. I want the final column to show how many consecutive months have 0 usage. For example, person a might have had 0 usage in March and April, used in June, then 0 usage in July - December, I would need the end column to show 6.
If your data is from B2 to M2 you could use the following formula:
=MAX(FREQUENCY(IF(B2:M2=0,COLUMN(B2:M2)),IF(B2:M2<>0,COLUMN(B2:M2))))
This will show the max number of consecutive 0's in that row 2 from column B to M.
Older versions of Excel require this array formula entered with ctrl+shift+enter
If you want the formula to show the quantity of consecutive zeroes of the last occurance in the row then you could use:
=LOOKUP(2,1/(FREQUENCY(IF($B2:$M2=0,COLUMN($B2:$M2)),IF($B2:$M2<>0,COLUMN($B2:$M2)))>0),FREQUENCY(IF($B2:$M2=0,COLUMN($B2:$M2)),IF($B2:$M2<>0,COLUMN($B2:$M2))))
I've made something for you, based on the numbers of the months in the year: 1-12 (in column "item").
In the "Value" column, there are the values zero or else.
The "Length" column contains the number of subsequent zeroes, based on following formulas:
First entry : =IF(B2=0,1,0)
Following entries: =IF(B3=0,C2+1,0)
Screenshot:
Have fun!
This might help if 2 is threshold for consecutive in F3:AI3
=SUM((FREQUENCY(IF(F3:AI3>=0,COLUMN(F3:AI3)),IF(F3:AI3<0,COLUMN(F3:AI3)))>=2)*1)
suppose I want count of values more than 10 in groups of 5 and more then
=SUM((FREQUENCY(IF(F3:AI3>=10,COLUMN(F3:AI3)),IF(F3:AI3<10,COLUMN(F3:AI3)))>=5)*1)
hope this helps

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

How to calculate the daywise average and multiply it to number of rows in that particular day

I have daywise excel data of four months which contains hourly data from 10 AM to 3 PM or sometimes 10 AM to 2 PM....1PM ..... like wise it is varying. The column A contains the date, Column B contains the time and column C and D has the data which i want to average for each day and multiply it to the number of occurrence.
For example If I have a data in column C and D for 17 Oct from 10 AM to 3PM (6 Hours)..Then i want to average these hourly data for a day and multiply it by 6. Since my interval of hourly data is not fixed, that is why i want a code which can average the daywise data and multiply it to the number of occurrence. I want to average the data 1 and data 2 for each day and to multiply its average by the number of occurrence of data in that particular day
I could not exactly get what does "average these hourly data" means since Average basically means summing all data within a day and dividing them to the occurrences. If you again multiply by the number of daily occurrence, you will get the summed value again.
Apart from above unclear point, un-merging all column B cells and filling all your rows with date data will be very helpful in order to form a quick sum or average formula based on the date conditions.
After doing above step, you may enter the below formula to E2 to get the sum of Col:C, for all data dated A2; 17-Oct.
=SUMIFS(C:C,A:A,A2)
And you may get the occurences on the date of your row data, you should use the formula and copy down:
=COUNTIFS(A:A,A2)
In combination of above 2 formulas, you may calculate the averages and/or date occurrences. I may write an exact solution if you may enter your requested values into COL:E and provide a screenshot.

Using Max function result in SumIFS Excel

I have a table like the image given above. There is another sheet which reports some information based on this data. I need the sum of Apples in the last week for a given month.
The answer for apples for 8 th month would be 14 assuming there is another record as Apples - 32 - 10 - 8.
So I would have to find max week number for a given month and then sum the quantity for Apples with max week number
The formula written is
=SUMIFS( C2:C300, A2:A300, H16, B2:B300, MAX(IF(D2:D300=MONTH(TODAY()-1),B2:B300)))
H16 contains the word "Apples"
Column A - Item
Column B - Week
Column C - Quantity
Column D - Month
Column H - Unique Items again..
MAX(IF(D2:D300=MONTH(TODAY()-1),B2:B300)) returns the correct max as 32 for month 8 but the formula returns the value as 0. If I put the max formula in aseperate cell and refer that cell in main formula, it works as expected. I am not sure why the result of max value is not being used for sumif function.
Your formula is good. all you need to do is to convert it into Array Formula. Just type the formula with Ctr + shift + enter.
{=SUMIFS( C2:C300, A2:A300, H16, B2:B300, MAX(IF(D2:D300=MONTH(TODAY()-1),B2:B300)))}
Actually, without array, the part IF(D2:D300 evaluates to #value error as Excel doesn't not know how to compare an array against one value.
Hint: Not sureif you know it already, but anyways, always use Formulas --> Evaluate Formula to pin-point an error in a formula.

Sum values in a column based on date

I have written this function that will give me a monthly sum for two columns: one has the date of each order, one has the cost of each order.
=SUMIF($C$1:$C$1000,">="&DATE(2010,6,1),$D$1:$D$1000)-SUMIF($C$1:$C$1000,">="&DATE(2010,7,1),$D$1:$D$1000)
Using data like this:
8/16/10 17:00 7.99
8/16/10 14:25 7.99
8/15/10 22:42 7.99
I end up with a table like this:
May 998
June 968.28
July 1239.76
August 514.96
However, now I would like to do daily sums and using my way I have to hand edit each row.
How can I do this better in Excel?
Use a column to let each date be shown as month number; another column for day number:
A B C D
----- ----- ----------- --------
1 8 6 8/6/2010 12.70
2 8 7 8/7/2010 10.50
3 8 7 8/7/2010 7.10
4 8 9 8/9/2010 10.50
5 8 10 8/10/2010 15.00
The formula for A1 is =Month(C1)
The formula for B1 is =Day(C1)
For Month sums, put the month number next to each month:
E F G
----- ----- -------------
1 7 July $1,000,010
2 8 Aug $1,200,300
The formula for G1 is =SumIf($A$1:$A$100, E1, $D$1:$D$100). This is a portable formula; just copy it down.
Total for the day will be be a bit more complicated, but you can probably see how to do it.
Use pivot tables, it will definitely save you time. If you are using excel 2007+ use tables (structured references) to keep your table dynamic. However if you insist on using functions, go with Smandoli's suggestion. Again, if you are on 2007+ use SUMIFS, it's faster compared to SUMIF.
Following up on Niketya's answer, there's a good explanation of Pivot Tables here:
http://peltiertech.com/WordPress/grouping-by-date-in-a-pivot-table/
For Excel 2007 you'd create the Pivot Table, make your Date column a Row Label, your Amount column a value. You'd then right click on one of the row labels (ie a date), right click and select Group. You'd then get the option to group by day, month, etc.
Personally that's the way I'd go.
If you prefer formulae, Smandoli's answer would get you most of the way there. To be able to use Sumif by day, you'd add a column with a formula like:
=DATE(YEAR(C1), MONTH(C1), DAY(C1))
where column C contains your datetimes.
You can then use this in your sumif.
Add a column to your existing data to get rid of the hour:minute:second time stamp on each row:
=DATE(YEAR(A1), MONTH(A1), DAY(A1))
Extend this down the length of your data. Even easier: quit collecting the hh:mm:ss data if you don't need it. Assuming your date/time was in column A, and your value was in column B, you'd put the above formula in column C, and auto-extend it for all your data.
Now, in another column (let's say E), create a series of dates corresponding to each day of the specific month you're interested in. Just type the first date, (for example, 10/7/2016 in E1), and auto-extend. Then, in the cell next to the first date, F1, enter:
=SUMIF(C:C, E1, B:B )
autoextend the formula to cover every date in the month, and you're done. Begin at 1/1/2016, and auto-extend for the whole year if you like.
If the second row has the same pattern as the first row, you just need edit first row manually, then you position your mouse pointer to the bottom-right corner, in the mean time, press ctrl key to drag the cell down. the pattern should be copied automatically.

Resources