简体   繁体   English

Powerpivot - 从单个事实表创建比率

[英]Powerpivot - creating ratios from a single fact table

I have a powerpivot table that looks like this... all "facts" are in a single table with the Information column used to segregate:我有一个看起来像这样的 powerpivot 表......所有“事实”都在一个表中,信息列用于隔离:

Information | Year | Amount
Expense       2010   1000000
Units Sold    2010   50000
Expense       2011   2000000
Units Sold    2011   125000

I would like to be able to calculate various ratios.我希望能够计算各种比率。 This seems like a straightforward need, but I've not been able to figure it out or find a solution online.这似乎是一个简单的需求,但我一直无法弄清楚或在线找到解决方案。

For example...例如...

  • Expense / Units Sold for 2010, 2011 or combined (based on filters applied in Excel) 2010 年、2011 年或合并的费用/销售单位(基于 Excel 中应用的过滤器)
  • ..... 2010 = 1000000/50000 = 20; ..... 2010 = 1000000/50000 = 20; 2011 = 2000000/125000 = 16; 2011 = 2000000/125000 = 16; all time = 3000000/175000 = 17.14 ETC.所有时间 = 3000000/175000 = 17.14 ETC。

In reality there are many more dimensions (region, location, month, etc.) and other Information types (headcount, square footage, etc.), but only three or four denominators, so I am hopeful the solution will scale out.实际上,还有更多维度(地区、位置、月份等)和其他信息类型(人数、平方英尺等),但只有三四个分母,因此我希望该解决方案能够横向扩展。

How can I do that in Powerpivot?我怎么能在 Powerpivot 中做到这一点? I can calculate the denominator using (as an example) CALCULATE(SUM(fact[amount]),fact[information]="Units Sold")), but cannot get that denominator applied against all numerators.我可以使用(例如)CALCULATE(SUM(fact[amount]),fact[information]="Units Sold")) 计算分母,但无法将该分母应用于所有分子。

xxxx xxx

Adding follow up based on the answer provided below...根据下面提供的答案添加跟进...

Issue #1: The solution provided below works when I am looking at Total Expenses, but in reality the Expense line will be multiple lines (Salary, Benefits, Rent, etc.) and I need to be able to calculate each per Unit Sold.问题 1:当我查看总费用时,下面提供的解决方案有效,但实际上费用行将是多行(工资、福利、租金等),我需要能够计算每售出的单位。 The solution doesn't seem to allow for this.解决方案似乎不允许这样做。

Issue #2: The fact table currently has four different information types ... Expenses (broken out as noted above), Units Sold, Headcount and Square Footage.问题#2:事实表目前有四种不同的信息类型……费用(如上文所述)、销售单位、员工人数和平方英尺。 What I would like to be able to do is have Expenses per Until Sold, Headcount per Unit Sold, etc. In other words, a denominator that can be applied against all rows which then can be summed in the pivot table easily.我希望能够做的是每售出费用、每售出单位人数等。换句话说,分母可以应用于所有行,然后可以轻松地在数据透视表中求和。 The solution takes everything that is not Units Sold, sums it and then divides by Units Sold... blending Expenses, Headcount and Square Footage.该解决方案将所有非销售单位数求和,然后除以销售单位数……混合费用、员工人数和平方英尺。

Thanks again for the assistance.再次感谢您的帮助。

SumAmt:= SUM( FactTable[Amount] )

UnitsSoldDenominator:=
DIVIDE(
    CALCULATE(
        [SumAmt]
        ,FILTER(
            VALUES( FactTable[Information] )
            ,FactTable[Information] <> "Units Sold"
        )
    )
    ,CALCULATE(
        [SumAmt]
        ,FactTable[Information] = "Units Sold"
    )
)

This will give the behavior you want.这将提供您想要的行为。

It's worth taking some time to explain why our filter arguments to CALCULATE() are different in the numerator and the denominator.值得花一些时间来解释为什么我们的 CALCULATE() 过滤器参数在分子和分母上不同。

CALCULATE() with a literal predicate, like we see in the second argument to DIVIDE() is implicitly rewritten to the following:带有字面谓词的 CALCULATE(),就像我们在 DIVIDE() 的第二个参数中看到的那样,被隐式重写为以下内容:

CALCULATE(
    [SumAmt]
    ,FILTER(
        ALL( FactTable[Information] )
        ,FactTable[Information] = "Units Sold"
    )
)

FILTER() simply iterates over the table passed to it in argument one and returns the rows from that table for which argument two returns true. FILTER() 简单地遍历在参数 1 中传递给它的表,并返回该表中参数 2 返回 true 的行。

With the implicit ALL() there, then there is no context preserved from the pivot table.有了隐式 ALL() ,就没有从数据透视表中保留的上下文。

VALUES() evaluates the table or column reference in filter context. VALUES() 评估过滤器上下文中的表或列引用。 Thus when we evaluate VALUES( FactTable[Information] ), we get back only the value in context in the pivot table.因此,当我们评估 VALUES( FactTable[Information] ) 时,我们只会返回数据透视表中上下文中的值。 In any given year, we get back every label that exists in that year, at the grand total we get every label.在任何给定的年份,我们都会找回那一年存在的每个标签,在总数上我们会得到每个标签。 At the "Units Sold" level, we get back nothing, because we're filtering that out.在“已售单位”级别,我们什么也得不到,因为我们将其过滤掉了。

Here's an image of a pivot table with the expected behavior based on your sample data and this measure.这是基于您的示例数据和此度量具有预期行为的数据透视表的图像。

在此处输入图片说明

Edit for dimension table编辑维度表

UnitsSoldDenominator:=DIVIDE(
    CALCULATE(
        [SumAmt]
        ,FILTER(
            VALUES( DimInformation[Information] )
            ,DimInformation[Information] <> "Units Sold"
        )
    )
    ,CALCULATE(
        [SumAmt]
        ,DimInformation[Information] = "Units Sold"
    )
)

This depends on an active relationship being defined between FactTable and DimInformation.这取决于在 FactTable 和 DimInformation 之间定义的活动关系。 I've defined mine on a numeric [InformationKey].我已经在数字 [InformationKey] 上定义了我的。

The image below includes my sample data based on what you provided in the original question, plus a mocked up dimension inferred from the same.下图包含我基于您在原始问题中提供的数据的示例数据,以及从中推断出的模拟维度。 The pivot table is shown along with the expanded field list.透视表与展开的字段列表一起显示。 The relationships in my model are displayed in the manage relationships dialog in the Power Pivot window.我的模型中的关系显示在 Power Pivot 窗口的管理关系对话框中。 I am seeing the same behavior as writing the original measure I provided against FactTable[Information] with the new version written against DimInformation[Information].我看到的行为与编写针对 FactTable[Information] 提供的原始度量以及针对 DimInformation[Information] 编写的新版本相同。

Please let me know if your model is configured the same and you are unable to reproduce this.如果您的模型配置相同并且您无法重现,请告诉我。

在此处输入图片说明

Edit 2 for hierarchy为层次结构编辑 2

UnitsSoldDenominator:=DIVIDE(
    CALCULATE(
        [SumAmt]
        ,FILTER(
            DimInformation
            ,DimInformation[Information] <> "Units Sold"
        )
    )
    ,CALCULATE(
        [SumAmt]
        ,ALL( DimInformation )
        ,DimInformation[Information] = "Units Sold"
    )
)

Here we've added a call to ALL( DimInformation ) in the denominator.在这里,我们在分母中添加了对 ALL( DimInformation ) 的调用。 This is because we will have filter context in the pivot table from DimInformation[InformationGroup] and DimInformation[Information].这是因为我们将在来自 DimInformation[InformationGroup] 和 DimInformation[Information] 的数据透视表中有过滤器上下文。 The filter predicate in CALCULATE(), the DimInformation[Information] = "Units Sold", implicitly strips filter context that exists on DimInformation[Information], but when we put DimInformation[InformationGroup] into the pivot table, it provides a new set of filter context (all filter contexts are evaluated in a logical and). CALCULATE() 中的过滤谓词 DimInformation[Information] = "Units Sold",隐式地剥离了 DimInformation[Information] 上存在的过滤上下文,但是当我们将 DimInformation[InformationGroup] 放入数据透视表时,它提供了一组新的过滤器上下文(所有过滤器上下文都在逻辑和中进行评估)。

Thus we're looking for (in the example in the image below) DimInformation[InformationGroup] = "Expense" (this is the context which is provided by the pivot table) && DimInformation[Information] = "Units Sold".因此,我们正在寻找(在下图中的示例中)DimInformation[InformationGroup] = "Expense"(这是数据透视表提供的上下文) && DimInformation[Information] = "Units Sold"。 This is not possible in our data so the denominator would be blank.这在我们的数据中是不可能的,因此分母将为空。

Thus we strip the filter context from all of DimInformation, and then evaluate our literal predicate.因此,我们从所有 DimInformation 中剥离过滤器上下文,然后评估我们的文字谓词。

在此处输入图片说明

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

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