Search code examples
dategoogle-sheetsgoogle-sheets-formulaarray-formulasmoving-average

Google Sheets: Moving daily, weekly, monthly, and yearly averages


I have a spreadsheet where I'm collecting data, and entering a date the data is collected. I would like to get the moving averages of collected data in my spreadsheet on a daily, weekly, monthly, and yearly for charts.

The two columns that I'm working off of are "Date" in column A (when the data was collected) and "Data" in column C (the actual collected data). The date is always increasing and is "mm/dd/yyyy" format. The data in column C are integers, and an almost-always increasing running total, except in four places where manual corrections had to be made.

The collected data is not entered every day, and as such, there are gaps between dates in the "Date" column. Sometimes 2 or 3 days go by without collected data, sometimes more. The largest gap is 98 days without collected data.

E.G.:

    + ---------- + - + ----- +
    |     A      | B |   C   |
+ - + ---------- + - + ----- +
| 1 |    Date    |   | Data  |
| 2 |  6/15/2016 |   | 1263  |
| 3 |  6/30/2016 |   | 1371  |
| 4 |   7/1/2016 |   | 1382  |
| 5 |   7/7/2016 |   | 1429  |
| 6 | 10/13/2016 |   | 2588  |

I have collected almost 3 years of data across 217 rows.

Getting a moving daily average seems as simple as =(C3-C2)/DATEDIF(A2, A3, "D")). Getting the moving weekly, monthly, and yearly averages are stumping me.

How can I get moving weekly, monthly, and yearly averages for data that isn't entered every day in Google Sheets?


Solution

  • 0

    G2:

    =IFERROR(MINUS(QUERY($A2:$C, 
     "select C 
      where A >= date'"&TEXT($A2,   "yyyy-mm-dd")&"' 
        and A <= date'"&TEXT($A2+7, "yyyy-mm-dd")&"'
      limit 1 offset "&COUNTA(QUERY($A2:$C, 
     "select C 
      where A >= date'"&TEXT($A2,   "yyyy-mm-dd")&"' 
        and A <= date'"&TEXT($A2+7, "yyyy-mm-dd")&"'"))-1), $C2)/7, )
    

    H2:

    =IFERROR(MINUS(QUERY($A2:$C, 
     "select C 
      where A >= date'"&TEXT($A2,    "yyyy-mm-dd")&"' 
        and A <= date'"&TEXT($A2+30, "yyyy-mm-dd")&"'
      limit 1 offset "&COUNTA(QUERY($A2:$C, 
     "select C 
      where A >= date'"&TEXT($A2,    "yyyy-mm-dd")&"' 
        and A <= date'"&TEXT($A2+30, "yyyy-mm-dd")&"'"))-1), $C2)/30, )
    

    I2:

    =IFERROR(MINUS(QUERY($A2:$C, 
     "select C 
      where A >= date'"&TEXT($A2,     "yyyy-mm-dd")&"' 
        and A <= date'"&TEXT($A2+365, "yyyy-mm-dd")&"'
      limit 1 offset "&COUNTA(QUERY($A2:$C, 
     "select C 
      where A >= date'"&TEXT($A2,     "yyyy-mm-dd")&"' 
        and A <= date'"&TEXT($A2+365, "yyyy-mm-dd")&"'"))-1), $C2)/365, )