问题
I'm fairly new to using PowerPivot and struggling with DAX a little - help!
I have two tables: Values
and Offices
.
Values
has, among other columns, OFFICE
, TARGET
and ACTUAL
. Offices has OFFICE
, REGION
, MANAGER
and PROVIDER
.
I want to calculate the actual for REGION
, MANAGER
and PROVIDER
on the Values table so when the user selects an office or a range of offices they see the values for their selection and then against all other offices in the selected regions or for the selected managers. Doing this in Excel I would just use:
=SUMIF(OFFICES[REGION],OFFICES[@REGION],[ACTUAL])
I've tried to use the calculate function but can't get it to work.
回答1:
For this, you want to make use of PowerPivot's powerful relation-based calculation engine. PowerPivot is a PivotTable generating machine, not just a spreadsheet that can hold more rows than Excel or that uses SQL (contrary to my initial belief).
Your OFFICES table is what we call a "dimension" or "lookup table" (depending on who you talk to) and VALUES is a "fact" or "data" table. Our "fact" tables contain all the real data as I see you noticed by naming it VALUES and the "dimensions" describe the data and allow us to slice & dice by it. These two types of tables interact via "relationships."
In Excel 2013, I find the diagram view the easiest way to create a "relationship." For this method, go to the Home tab of PowerPivot and click Diagram View. This will show all of the tables in your data model with all of their respective fields (columns). Click and hold on the Office field on the VALUES tab and drag it to the Office field of OFFICE.
This indicates that VALUES is the "many side" and OFFICE is the "one side" in a "many-to-one" relationship. If, for example, a given Office has multiple Providers, that would cause that Office to appear more than once in the OFFICE dimension, causing a problem for the relationship. If that is the case, you might want to consider re-structuring your queries to facilitate fact-to-dimension many-to-one structure (i.e. - moving provider to the VALUES table).
Once this relation is created, go to the VALUES tab and in the calculation area at the bottom of your window (below all of your data) enter the following code to create a "measure."
sumActual:=SUM(Values[Actual])
With this "measure" created, click PivotTable on the Home tab of your PowerPivot window to create a PivotTable in the "front-end" of Excel). Here, you will be able to filter, slice, or pivot your Actuals by Regions, Managers and/or Providers and quickly rearrange your pivot without any new calculation or structural work!
Feel free to also create "measures" for Targets or even variance/%variance.
sumTarget:=SUM(Values[Target])
variance:=sumActual-sumTarget
%variance:=DIVIDE(variance,sumTarget,BLANK())
来源:https://stackoverflow.com/questions/39393710/sumif-for-powerpivot