简体   繁体   English

如何使用 Google 表格计算每天的平均利润

[英]How to calculate an average profit per day with Google Sheets

I made a spreasheet to follow the growth of my income.我做了一个电子表格来跟踪我的收入增长。 It include a script that save periodically what I've earned.它包括一个脚本,可以定期保存我所获得的。 Column A is DATES, Column B is how much. A列是日期,B列是多少。

I want to calculate my daily average profit.我想计算我的每日平均利润。

So I went to substract yesterday to today and then make an average of all that data.所以我去减去昨天到今天,然后对所有数据取平均值。 But there is a problem along the way because my script is saving data many times a day (to ensure I get data, and to tack more precisely).但是在此过程中出现了一个问题,因为我的脚本每天会多次保存数据(以确保我获得数据,并更精确地跟踪)。

  • I would like to first calculate an average profit for each day individually (C column on screenshot)我想首先单独计算每天的平均利润(屏幕截图上的 C 列)
  • Then calculate the variation (D column on screenshot)然后计算变化(屏幕截图上的 D 列)
  • Then average the variation .然后平均变化 (E column on screenshot) (截图上的E栏)

这是我想到的过程

As my data are growing each day, I'm looking for a flexible way to do it.随着我的数据每天都在增长,我正在寻找一种灵活的方式来做到这一点。 I think QUERY may be a way but I don't know how to do that.我认为QUERY可能是一种方式,但我不知道该怎么做。 A script would work too I guess.我猜一个脚本也可以工作。 Maybe with the FILTER formula...也许使用 FILTER 公式...

What's your thoughts on that?你对此有何看法? Cheers, NipthiAe.干杯,NipthiAe。

You can do it with 3 formulas:你可以用3个公式来做到这一点:

在此处输入图像描述

First (query) in D2 : D2中的第一个(查询):

To get the average per day获取每天的平均值

=query({A2:B}, 
   "Select Col1 ,count(Col1), avg(Col2) where Col1 is not null 
        group by Col1 Order By Col1 asc 
           label Col1 'Date', count(Col1) 'Days', avg(Col2) 'Average/day'")

Second in G4 : G4第二名:

To get the differences between the averages obtained by the Query:要获得查询获得的平均值之间的差异:

=arrayformula(if(isblank(E4:E6),"",filter(E4:E6,E4:E6>-1)-filter(E3:E6,E3:E6>-1)))

Third (above the query) in F1 : F1中的第三个(查询上方):

To get the average of the variations:要获得变化的平均值:

="Variation Avg: "& DOLLAR(round(AVERAGE(F3:F),2))

To calculate the daily profits you shouldn't make an average of the portfolio on each time.要计算每日利润,您不应该每次都对投资组合进行平均。 For example, if in a single day you have例如,如果在一天内您有

Portfolio文件夹
1 1
2 2
3 3
4 4

The average would be 2.4 but that's not the day's profits.平均值为2.4 ,但这不是当天的利润。 To calculate a day's profits you need to subtract the last value of the day with the last value of the day before.要计算一天的利润,您需要用前一天的最后一个值减去当天的最后一个值。 In this case, if the last value was 0, the answer is 4.在这种情况下,如果最后一个值为 0,则答案为 4。

Here is how I would do this.这是我将如何做到这一点。

Solution解决方案

Step 1: Find the value of the protfolio when closing the day第 1 步:在收盘时找出投资组合的价值

What we need to do is find the latest value of each day.我们需要做的是找到每天的最新值。 The simplest way of doing so is to find the last entry (using the datetime) of the day.最简单的方法是查找当天的最后一个条目(使用日期时间)。 So to get the last datetime we can use (in my case added it to D2 ):所以要获得我们可以使用的最后一个日期时间(在我的例子中将它添加到D2 ):

=query({A3:A, arrayformula(int(A3:A))}; "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''"; 0)

Let's unpack this formula:让我们解开这个公式:

{A3:A, arrayformula(int(A3:A))}

It's a 2 column array.它是一个 2 列数组。 The first column are the datetimes.第一列是日期时间。 The second column are only the dates.第二列只是日期。 That relies on the fact that int(datetime) returns the number representing the day.这取决于int(datetime)返回代表日期的数字这一事实。 Add arrayformula to get it for each datetime and you get the day for each one.添加arrayformula以获取每个日期时间的数据,然后您将获得每个日期的日期。

Then the query:然后查询:

select max(Col1)
  where Col1 is not null
  group by Col2
  order by max(Col1)
  label max(Col1) ''

The trick here is to get the biggest (latest) datetime within a day.这里的诀窍是在一天内获得最大(最新)的日期时间。 This can be achieved by selecting max(datetime) and grouping by date.这可以通过选择max(datetime)并按日期分组来实现。 Then we can add a filter for empty cells ( where Col1 is not null ) and order the result to ensure the correct ordering.然后我们可以为空单元格添加一个过滤器( where Col1 is not null )并对结果进行排序以确保正确排序。 Also remove the default label by adding label max(Col1) '' at the end.还要通过在末尾添加label max(Col1) ''来删除默认的 label。

Step 2: Get the last value第 2 步:获取最后一个值

Now that we have the last datetime of the day we can use vlookup to get the value (on E3 ):现在我们有了一天中的最后一个日期时间,我们可以使用vlookup来获取值(在E3上):

=arrayformula(if(D3:D<>""; vlookup(D3:D; A3:B; 2; false); ""))

As you can see it's a simple vlookup that was wrapped in an if to prevent getting an error on empty cells.如您所见,这是一个简单的vlookup ,它被包裹在if中,以防止在空单元格上出现错误。 arrayformula allows us to do it for each row in one formula. arrayformula允许我们在一个公式中为每一行执行此操作。

Step 3: Getting the daily profit第 3 步:获取每日利润

Now that we have the latest value we only need to subtract the row with the previous one (on F3 ):现在我们有了最新的值,我们只需要减去前一行(在F3上):

=arrayformula(if(E3:E100<>""; E3:E-offset(E3:E; -1; 0); ""))

offset allows us to use this formula without having to specify the number of rows. offset允许我们使用这个公式而不必指定行数。 As always, arrayformula allows us to do it for all rows in a single formula and we filter empty rows.与往常一样, arrayformula允许我们对单个公式中的所有行执行此操作,并过滤空行。

Step 3: Total average第 3 步:总平均值

We can finally take the average:我们终于可以取平均值了:

=average(F3:F)

If you didn't start from 0, you should change it to start at the next value ( F4 ) since the first value will be the accumulated until that point.如果您不是从 0 开始,则应将其更改为从下一个值 ( F4 ) 开始,因为第一个值将是该点之前的累积值。

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM