简体   繁体   English

Pivot计算公式:SUM(Field1)/ AVG(Field2)

[英]Pivot Calculated formula: SUM(Field1)/AVG(Field2)

I've a simple table with some amount and interval in sec by date and product name. 我有一个简单的表,其中包含一些数量和间隔,以秒为单位和产品名称。

Month | Product | Amount | Interval in sec
------------------------------------------
05-'12| Prod A  | 10     | 5
05-'12| Prod A  | 3      | 5
05-'12| Prod B  | 4      | 5
05-'12| Prod C  | 13     | 5
05-'12| Prod C  | 5      | 5

From this table I've derived a PivotTable with SUM(Amount), AVERAGE(Interval in sec) by Month and Product. 从这个表中我得到了一个数据透视表,其中包含SUM(金额),AVERAGE(以秒为单位的间隔)按月和产品。

Month | Product | SUM of Amount | AVG of Interval in sec
--------------------------------------------------------
05-'12| Prod A  | 13            | 5
05-'12| Prod B  | 4             | 5
05-'12| Prod C  | 18            | 5

So far so good. 到现在为止还挺好。 Now I want to add and extra column to my PivotTable with gives me the outcome of SUM of Amount / AVG of Interval in sec. 现在我想为我的数据透视表添加额外的列,并以秒为单位给出间隔量的数量/ AVG的结果。

Adding a calculated value =SUM(Amount)/AVERAGE(Interval) is not giving me the right values. 添加计算值=SUM(Amount)/AVERAGE(Interval)不会给我正确的值。 Excel gives me: Excel给了我:

Month | Product | SUM of Amount | AVG of Interval in sec | Amount per sec
-------------------------------------------------------------------------
05-'12| Prod A  | 13            | 5                      | 1.3
05-'12| Prod B  | 4             | 5                      | 0.8
05-'12| Prod C  | 18            | 5                      | 1.8

What it actually is doing is =SUM(Amount)/SUM(Interval in sec) for every Month and Product based on the values in the first table. 它实际上做的是=每个月的Sum(Amount)/ SUM(以秒为单位)和基于第一个表中的值的Product。 But I'm looking for: 但我正在寻找:

Month | Product | SUM of Amount | AVG of Interval in sec | Amount per sec
-------------------------------------------------------------------------
05-'12| Prod A  | 13            | 5                      | 2.6
05-'12| Prod B  | 4             | 5                      | 0.8
05-'12| Prod C  | 18            | 5                      | 3.6

So literally divide pivot field 'Sum of Amount' by pivot field 'AVG of Interval in sec'. 因此,通过枢轴字段“以秒为单位的AVG”来划分枢轴场“量的总和”。

How to achieve this? 怎么做到这一点?

In case anyone else comes across this problem, a solution that I've found is to add a helper "Count" column to your data set, where each record has a 1 entered under the "Count" field. 如果其他人遇到此问题,我发现的解决方案是在数据集中添加帮助“计数”列,其中每个记录在“计数”字段下输入1。

Then, in order to achieve the effect of an Average, you can use: 然后,为了达到平均值的效果,您可以使用:

SUM(FIELD_TO_AVERAGE)/SUM(COUNT)

I believe this should work in all cases where an average is needed, but I haven't tested it very broadly. 我相信这应该适用于需要平均值的所有情况,但我没有对它进行过广泛的测试。

You need to refer to the pivot table data in your formula, something like this: 您需要在公式中引用数据透视表数据,如下所示:

=GETPIVOTDATA("Sum of Amount",$A$3,"Product","A")/GETPIVOTDATA("Average of Interval",$A$3,"Product","A")

Edit: From your spreadsheet: To add your desired column to Pivot Table A: 编辑:从电子表格中:将所需列添加到数据透视表A:

K5=GETPIVOTDATA("Sum of Amount",$H$2,"Month",DATE(2012,5,1),"Product","Prod A")/GETPIVOTDATA("Average of Interval",$H$2,"Month",DATE(2012,5,1),"Product","Prod A")
K6=GETPIVOTDATA("Sum of Amount",$H$2,"Month",DATE(2012,5,1),"Product","Prod B")/GETPIVOTDATA("Average of Interval",$H$2,"Month",DATE(2012,5,1),"Product","Prod B")
K7=GETPIVOTDATA("Sum of Amount",$H$2,"Month",DATE(2012,5,1),"Product","Prod C")/GETPIVOTDATA("Average of Interval",$H$2,"Month",DATE(2012,5,1),"Product","Prod C")

You can also produce the column by adding an extra column to the original data table of: Amount/Sec for each individual entry then when you pivot all the data the product of that column will be your desired result. 您还可以通过向原始数据表添加额外列来生成列:每个条目的数量/秒,然后当您旋转所有数据时,该列的产品将是您想要的结果。

Edit (2): 编辑(2):

The formulas above are cell formulas not pivot table formulas which can't use references sorry I didn't make that clear. 上面的公式是单元格公式而不是透视表公式,不能使用引用抱歉我没有说清楚。 I am looking into a pivot table calculation formula but for now the above as a column alongside your pivot table should produce what you need. 我正在研究一个数据透视表计算公式,但现在上面作为您的数据透视表旁边的列应该产生您需要的。

make a field in the source (counter) and put "1" in every line. 在源(计数器)中创建一个字段并在每一行中放置“1”。 put it also into the pivot and divide the SUM of seconds with this field - then it Will be an average. 把它放到枢轴中并用这个字段除去秒的SUM - 然后它将是一个平均值。 divide the original field with this average so your final-final division Will be the amount per sum of seconds per counter and that Will make the trick :) 将原始字段除以此平均值,这样您的最终最终分数将是每个计数器的每秒秒数的数量,这将成为诀窍:)

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

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