powerpivot using a calculated value in another calculation

。_饼干妹妹 提交于 2019-12-11 16:17:31

问题


I have the following tables

Orders:

OrderID|Cost|Quarter|User
-------------------------
   1   | 10 |   1   | 1
   2   | 15 |   1   | 2
   3   |  3 |   2   | 1
   4   |  5 |   3   | 3
   5   |  8 |   4   | 2
   6   |  9 |   2   | 3
   7   |  6 |   3   | 3

Goals:

UserID|Goal|Quarter
-------------------
   1  | 20 |   1   
   1  | 15 |   2   
   2  | 12 |   2   
   2  | 15 |   3   
   3  |  5 |   3 
   3  |  7 |   4 

Users:

UserID|Name
-----------
   1  | John  
   2  | Bob
   3  | Homer

What I'm trying to do is to sum up all orders that one user had, divide it by the sum of his goals, then sum up all orders, devide the result by the sum of all goals and then add this result to the previous result of all Users.

The result should be:

UserID|Name |Goal|CostSum|Percentage|Sum all
---------------------------------------------------
   1  |John | 35 |   13  |    0.37  |
   2  |Bob  | 27 |   23  |    0.85  |
   3  |Homer| 12 |   20  |    1.67  | 

the calculation is as follow:

CostSum: 10+3=13
Goal: 20+15=35
Percentage: CostSum/Goal=13/35=0.37

Sum all: 10+15+3+5+8+9+6=56
Goal all: 20+15+12+15+5+7=74
percentage all= Sum_all/Goal_all=56/74=0.76

Result: percentage+percentage_all=0.37+0.76=1.13 for John
                                            1.61 for Bob
                                            2.43 for Homer

My main problem is the last step. I cant get it to add the whole percentage. It will always filter the result so making it wrong.


回答1:


To do this you're going to need to create some measures.

(I will assume you've already set your pivot table to be in tabular layout with subtotals switched off - this allows you to set UserID and Name next to each other in the row labels section.)

This is what our output will look like.

First let's be sure you've set up your relationships correctly - it should be like this:

I believe you already have the first 5 columns set up in your pivot table, so we need to create measures for CostSumAll, GoalSumAll, PercentageAll and Result.

The key to making this work is to ensure PowerPivot ignores the row label filter for your CostSumAll and GoalSumAll measures. The ALL() function acts as an override filter when used in CALCULATE() - you just have to specify which filters you want to ignore. In this case, UserID and Name.

CostSumAll:

=CALCULATE(SUM(Orders[Cost]),ALL(Users[UserID]),ALL(Users[Name]))

GoalSumAll:

=CALCULATE(SUM(Goals[Goal]),ALL(Users[UserID]),ALL(Users[Name]))

PercentageAll:

=Orders[CostSumAll]/Orders[GoalSumAll]

Result:

=Orders[Percentage]+Orders[PercentageAll]

Download - Example file available for download here. (Don't actually read it in Google Docs - it won't be able to handle the PowerPivot stuff. Save locally to view.)



来源:https://stackoverflow.com/questions/26354487/powerpivot-using-a-calculated-value-in-another-calculation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!