Comments on: Rolling Averages in Excel?
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel/
Comments on Ask MetaFilter post Rolling Averages in Excel?Sun, 21 May 2006 09:05:54 -0800Sun, 21 May 2006 09:05:54 -0800en-ushttp://blogs.law.harvard.edu/tech/rss60Question: Rolling Averages in Excel?
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel
Excelfilter: how do I compute a rolling seven-day average for something that happens at sporadic intervals? <br /><br /> I keep an exercise log and I'd like to have both entries for how much I'm swimming as well as how much swimming I've done in the preceding seven days [not like a Sunday to Saturday week, just the last seven days] and how it compares to previous seven day increments. I swim four out of seven days on no particular schedule. My spreadsheet, such as it is, looks like this, with about seven months of data.<br>
<br>
Date Laps<br>
1/2/06 17<br>
1/3/06 27<br>
1/6/06 24<br>
. .<br>
. .<br>
. .<br>
<br>
I've got a chart with daily values over time and a trendline. I'd really like to be able to figure out how to make a chart with just seven day averages, but I'm not sure how to go about it. Most of the examples I've seen are for things where there are regular data elements that need to be dealt with, like daily revenues. I'm good with spreadsheets in general, but not super smart with functions so the more explanation the better where functions are involved. Thanks.post:ask.metafilter.com,2006:site.38591Sun, 21 May 2006 08:51:18 -0800jessamynexcelaveragesworkoutlogBy: boo_radley
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596552
There's an option under the Tools > Data analysis. <a href="http://www.statsclass.com/excel/analyses/bilder/excel_ma.html">Check out these instructions</a>.<br>
<br>
If you don't have the Data Analysis option, or want a way to do it while you're still gathering your data, I might make a new column of data called "Moving average" next to "Laps". Its entries would look like (assuming your data really starts in cell B2) <code>=Average(B2:B8)</code>. Copying and pasting that formula into each successive cell in the row will get you your moving average.comment:ask.metafilter.com,2006:site.38591-596552Sun, 21 May 2006 09:05:54 -0800boo_radleyBy: jessamyn
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596561
<em>Copying and pasting that formula into each successive cell in the row will get you your moving average.</em><br>
<br>
Thank you. I do have the data analysis option, but won't this just get me the average of the last seven <em>entries</em> instead of the last seven days? My problem is that some weeks I swim five days and some weeks I swim two. I know that I can make a chart that has every day on it and just fill in the second column for the days I swim, but it seems like Excel knows how to parse dates so that if I could figure out the magic formula, I could avoid this extra work.comment:ask.metafilter.com,2006:site.38591-596561Sun, 21 May 2006 09:15:59 -0800jessamynBy: boo_radley
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596581
You want it to average for the days that aren't recorded too? That's a bit more complex -- it's hard to consider data that isn't there.comment:ask.metafilter.com,2006:site.38591-596581Sun, 21 May 2006 09:49:55 -0800boo_radleyBy: Doofus Magoo
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596584
This would go better in a database. ;)<br>
<br>
That said, the "simplest" solution would probably be to make judicious use of the SUMIF function. Personally, I'd set it up as follows:<br>
<br>
Assume that your date/laps table is in a range named "MyLaps."<br>
<br>
Set up a parallel table (in the same sheet, or another sheet, doesn't matter) with one row per week, each row containing the week start date and week end date (e.g., 1/1/2006 and 1/7/2006) in columns A and B. Then in cell C, put the following formula:<br>
<br>
=(SUMIF(MyLaps,">="&A3,B:B)-SUMIF(MyLaps,">="&B3,B:B))/(COUNTIF(MyLaps,">="&A3)-COUNTIF(MyLaps,">="&B3))<br>
<br>
For the first row (1/1/2006 - 1/7/2006), you're basically saying "Total laps on or after 1/1/2006 MINUS Total laps on or after 1/8/2006 (e.g., total laps from 1/1 - 1/7). Divide by the number of times I did laps during that same interval.comment:ask.metafilter.com,2006:site.38591-596584Sun, 21 May 2006 09:54:53 -0800Doofus MagooBy: Doofus Magoo
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596585
... the formula posted above assumes that you're starting your "Weekly Table" on row 3 (i.e., it references cells A3 and B3). Those may need to be corrected if you start your "Weekly Table" in a different row.comment:ask.metafilter.com,2006:site.38591-596585Sun, 21 May 2006 09:56:14 -0800Doofus MagooBy: Doofus Magoo
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596591
Jeez, sorry -- one more assumption. The formula assumes that your "MyLaps" range is in columns A and B (i.e., references to A:A and B:B).<br>
<br>
I'll also point out that for your second row in the "Weekly Table," instead of typing the dates for the second week, you can add 7 to the dates in the first row. That way you can just copy the entire row down for as many weeks as you need to do it.comment:ask.metafilter.com,2006:site.38591-596591Sun, 21 May 2006 10:00:28 -0800Doofus MagooBy: jessamyn
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596641
Thank you. The assumptions are getting a little ahead of me. Let me make clearer exactly what I have. One worksheet, called "laps". In column A I have dates I swam, header is "date". In column B I have number of laps for days I swam. Header is "amount" Column C is blank. In column D and E I now have dates where column D is start of week date and column E is end of week date. Headers are "begin" and "end"<br>
<br>
Doofus, I think I am confused which A and B you mean. When I input the formula you give me, I'm not sure what to change MyLaps to. If I change it to "amount" I get a number that doesn't seem right [I get 20.2 when that week I only swam 12 and 18 laps] Maybe I'm not as good at Excel as I thought? Given the data I have I'd like to be able to plot what the average laps-per-day I have swum is, and I figured a rolling seven day average was an okay way to do this, but maybe I can do it as just an average year-to-date?comment:ask.metafilter.com,2006:site.38591-596641Sun, 21 May 2006 11:19:03 -0800jessamynBy: bim
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596650
How about a nice <a href="http://office.microsoft.com/en-us/results.aspx?Scope=TC%2CHP%2CHA%2CRC%2CFX%2CES%2CEP%2CDC%2CXT&Query=fitness+templates">excel template.</a> These are direct from Microsoft, but there are others on the internet. Maybe one will fit your needs and do the work for you.<br>
<br>
Good luck!comment:ask.metafilter.com,2006:site.38591-596650Sun, 21 May 2006 11:40:06 -0800bimBy: Opposite George
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596654
Not answering the original question (it's doable, but to my knowledge a PITA in Excel) but offering another option.<br>
<br>
You might want to consider comparing each day's laps to what I'll call a "moving average with decay" (I'm ignorant of the fancy math name for this technique, if there is one.) This will smooth out the bumps in your data.<br>
<br>
Add a column -- call it "MAWD" or whatever. For the first date just enter the same value as the laps entry. For subsequent dates add 90 percent of the previous MAWD value to 10 percent of that day's laps -- this value will reflect trends in your performance while not being too affected by one off day (or a day when you pushed yourself extra hard); thus, it should serve as a good benchmark of your progress. I've used this technique before to track my weight and it's a good way to keep from agonizing over temporary blips.<br>
<br>
Email me if you want a worked-out example in Excel.comment:ask.metafilter.com,2006:site.38591-596654Sun, 21 May 2006 11:47:15 -0800Opposite GeorgeBy: Doofus Magoo
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596716
<blockquote><i>Doofus, I think I am confused which A and B you mean. When I input the formula you give me, I'm not sure what to change MyLaps to. If I change it to "amount" I get a number that doesn't seem right [I get 20.2 when that week I only swam 12 and 18 laps] Maybe I'm not as good at Excel as I thought? Given the data I have I'd like to be able to plot what the average laps-per-day I have swum is, and I figured a rolling seven day average was an okay way to do this, but maybe I can do it as just an average year-to-date?</i></blockquote><br>
<br>
Assuming that your "date/amount" data start in A2:B2, and that your "begin/end" data start in D2:E2, then this formula would go in F2:<br>
<br>
=(SUMIF(A:B,">="&D2,B:B)-SUMIF(A:B,">="&E2,B:B))/(COUNTIF(A:B,">="&D2)-COUNTIF(A:B,">"&E2))comment:ask.metafilter.com,2006:site.38591-596716Sun, 21 May 2006 13:16:48 -0800Doofus MagooBy: Doofus Magoo
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596726
Feh, sorry, that should be:<br>
<br>
=(SUMIF(A:B,">="&D2,B:B)-SUMIF(A:B,">"&E2,B:B))/(COUNTIF(A:B,">="&D2)-COUNTIF(A:B,">"&E2))<br>
<br>
(changed the second ">=" to just ">")comment:ask.metafilter.com,2006:site.38591-596726Sun, 21 May 2006 13:29:42 -0800Doofus MagooBy: devilsbrigade
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#596733
I'm going to second the database opinion. This would be very easy with SQL, and most tools have an option to import CSV data.comment:ask.metafilter.com,2006:site.38591-596733Sun, 21 May 2006 13:38:01 -0800devilsbrigadeBy: kindall
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597028
You have all the weekly data -- the days when you didn't swim, the number of laps is zero. So fill in the zeroes and the rest of it will be easy. Your weekly average will be much lower than the number of laps you swim on any given day, of course, but that doesn't matter, as it won't affect your ability to see trends.<br>
<br>
It'll also make any weeks you slacked off and didn't swim 4 days stand out in your graph quite starkly...comment:ask.metafilter.com,2006:site.38591-597028Sun, 21 May 2006 20:17:22 -0800kindallBy: Opposite George
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597033
Probably late to the party here, but you can implement <b>Doofus Magoo</b>'s method without plugging in start/end dates, and just adding one column to your original table:<br>
<br>
Say the dates are in column A, and laps are in column B. We'll put the moving average in column C. Say your first data row is row 3. Put this formula in cell C4:<br>
<br>
=SUMIF(A$3:A3;">="&A4-7;B$3:B3)/COUNTIF(A$3:A3;">="&A4-7)<br>
<br>
Copy this formula down the column. Voila.comment:ask.metafilter.com,2006:site.38591-597033Sun, 21 May 2006 20:28:21 -0800Opposite GeorgeBy: Opposite George
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597038
BTW, that gives you avg. laps / session over the previous 7 days. If you'd prefer to get <b>kindall</b>'s number (avg. laps per day over previous seven days,) just throw the COUNTIF part out, and divide by 7.comment:ask.metafilter.com,2006:site.38591-597038Sun, 21 May 2006 20:31:24 -0800Opposite GeorgeBy: Doofus Magoo
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597203
<blockquote><i>=SUMIF(A$3:A3;">="&A4-7;B$3:B3)/COUNTIF(A$3:A3;">="&A4-7)</i></blockquote><br>
<br>
The elegance of this solution appeals to me greatly (as OG pointed out, this will give you more of a true "moving average" over the last seven days instead of averaging by arbitrary calendar weeks); I think it may be a bit inaccurate, however.<br>
<br>
Having not had my daily allotment of coffee, I may be off-base, but as written, however, it looks like OG's formula will give you the "average laps swum since one week before this date" since it doesn't include an upper bound on the dates. That is, for lap records from January, it will average laps from January - present. For lap records from July 2004, it will average laps from July 2004-present. For the most recent week, however, the average will be valid.<br>
<br>
If you want to look at the average between dates X and Y, I'm pretty sure you have to use two COUNTIF/SUMIF statements; e.g. the following formula in cell C3:<br>
<br>
=(SUMIF(A:A,">="&(A3-7),B:B)-SUMIF(A:A,">"&A3,B:B))/(COUNTIF(A:A,">="&A3-7)-COUNTIF(A:A,">"&A3))comment:ask.metafilter.com,2006:site.38591-597203Mon, 22 May 2006 03:58:54 -0800Doofus MagooBy: Opposite George
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597386
<b>Doofus Magoo</b>, <br>
<br>
I haven't had my morning coffee yet either, so no guarantees here but yes, our formulas give different results but not for the reason you think; it looks like your formula is actually calculating an eight-day moving average, including the date in A3. <br>
<br>
Add an equals sign to your second compare and our formulas will agree. At least that's how it worked out on my spreadsheet.comment:ask.metafilter.com,2006:site.38591-597386Mon, 22 May 2006 09:06:09 -0800Opposite GeorgeBy: Doofus Magoo
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597454
You're right. But wouldn't we want instead to either change the "A3-7" to "A3-6" or the first ">=" to ">" (that is, for laps on Saturday, we want to look backwards to Sunday, not to the previous Saturday)? By changing the ">" to ">=" we're excluding the current day's laps from the week's average.comment:ask.metafilter.com,2006:site.38591-597454Mon, 22 May 2006 10:22:17 -0800Doofus MagooBy: Opposite George
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597595
Whether you include the current day in the week's average is strictly a matter of taste. <br>
<br>
Today's laps make up 25% of the number calculated using the second method, so my feeling is that a comparison between that and today's number gives you less information than one between today's laps and the seven days leading up to today. Plus, the second method keeps the historical data separate from today's data, which is easier for me to wrap my head around.<br>
<br>
Ultimately, there is no single "right" way. This exercise really is one of decreasing a complex series' information content sufficiently that we can draw simple conclusions with our puny human brains. The key is understanding what goes into the smushed numbers we end up with so our interpretations make sense.comment:ask.metafilter.com,2006:site.38591-597595Mon, 22 May 2006 12:28:22 -0800Opposite GeorgeBy: jessamyn
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#597675
I tried both OG and DMs solutions and for some reason I get an error in Excel when I try <a href="http://ask.metafilter.com/mefi/38591#597033">this one</a> but the longer one (the eight day average) seem to work and do what I'd like it to do. You can see the new chart here<br>
<br>
<img src="http://www.librarian.net/tempo/laps.jpg"><br>
<br>
Where the yellow line is the trend line, the orange line is just the raw laps data and the red line is the average over eight days which is a little more useful and a little less all over the place. If anyone can show me how to troubleshoot OGs function (or feel free to email me) that would be super. Thanks very much.comment:ask.metafilter.com,2006:site.38591-597675Mon, 22 May 2006 13:36:14 -0800jessamynBy: jessamyn
http://ask.metafilter.com/38591/Rolling-Averages-in-Excel#602352
I could have also checked that there weren't <a href="http://www.usms.org/fitness/content/vscomplogs">swim logs</a> available elsewhere. While these don't do exactly what I want, it's good to see them available.comment:ask.metafilter.com,2006:site.38591-602352Fri, 26 May 2006 16:04:23 -0800jessamyn