Need help creating a formula for dynamic average of last 4 weeks expenses

时间:2019-05-31 11:30:22

标签: google-sheets average google-sheets-formula google-sheets-query

I am looking to create a spreadsheet that my staff fill out, it then gives me a master sheet with all the data, then I import dynamically to my financial spreadsheet telling me the average cost of my client over the last 30 days.

I am looking to create an AVERAGE formula of the last 30 days when Date = Today (Monday) (I want the weekday Monday as that's when staff hand in invoices)

Hope this makes sense, it's really tough!

Here's a video of me explaining my desired outcome

https://www.loom.com/share/3a9cb75052b246d1af2ba2f9ce9180a7

I've followed several guides & can't figure it out.

=ArrayFormula(iferror(query(average(if(today() - weekday(today(),3)-30)))))

I expected $90 average and I just get blank

2 个答案:

答案 0 :(得分:0)

您可以使用以下公式:

  

= AVERAGE(VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+ 1,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-6,A :H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-13,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-20 ,A:H,2,FALSE))

要将其细分为各个组成部分,请从VLOOKUP结果中获取平均值:

  

VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+ 1,A:H,2,FALSE)

VLOOKUP正在查找当前日期的最后一个星期一:

  

TODAY()-WEEKDAY(TODAY(),2)+1

然后

  

TODAY()-WEEKDAY(TODAY(),2)-6

以此类推...

在工作表上使用时,您必须指定要在查询中参考的列,对于列B(brand1)使用: A:H,2,FALSE ),对于 colunm C(品牌2),请使用: A:H,3,FALSE),对于 colunm d(品牌3)使用: A:H,4,FALSE)等等...

答案 1 :(得分:0)

=INDEX(QUERY({INDIRECT("A2:D"&ROW()-1)}, 
 "select avg(Col2),avg(Col3),avg(Col4) 
  where Col1 <= date '"&TEXT(TODAY(),    "yyyy-MM-dd")&"' 
    and Col1 >= date '"&TEXT(TODAY()-30, "yyyy-MM-dd")&"'"), 2, )

enter image description here