Here's what the table looks like.
<table style="width: 277px; height: 276px;" border="1" bordercolor="#999999" cellspacing="0"> <tbody><tr><td bgcolor="#C0C0C0">
</td> <td bgcolor="#C0C0C0" align="center">
A</td><td bgcolor="#C0C0C0" align="center">
B</td><td bgcolor="#C0C0C0" align="center">
C</td></tr> <tr><td bgcolor="#C0C0C0" align="center">
1</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
Field_1 </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
Field_2 </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="left">
Amount </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
2</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
a </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="right">
</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
5 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
3</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
b </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="right">
</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
9 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
4</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
c </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="right">
</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
7 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
5</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
d </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
a </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
5 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
6</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
e </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
b </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
4 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
7</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
f </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
c </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
6 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
8</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
g </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
d </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
10 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
9</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
h </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
e </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
2 </td></tr> <tr><td bgcolor="#C0C0C0" align="center">
10</td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
i </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="center" width="192" align="left">
f </td><td rowspan="1" colspan="1" bgcolor="#FFFFFF" height="25.5" valign="bottom" width="192" align="right">
4 </td></tr> </tbody></table>
First I created a simple measure:
Measure_1:=SUM(myTable[Amount])
Now for Measure_2, I need a sum of the [Amount] column where Field_1 is filtered on the values in Field_2 in the current context.
For example: If the context of the cell in my PT is where Field_1 contains {d,e,f} then Measure_1 = 5+4+6 = 15. Now in this context the values in Field_2 are {a,b,c} so I need Measure_2 to return the sum of [Amount] from the rows where Field_1 = {a,b,c} i.e. 5+9+7 = 21.
In
my post on MSDN PowerpIvot forum I came up with something that works but unfortunately it is painfully slow. Here it is for reference.
Code:
Measure_2:
=SUMX(VALUES(myTable[Field_2]),
CALCULATE(myTable[Measure_1],
ALL(myTable),
myTable[Field_1]=EARLIER(myTable[Field_2])
)
)
My idea was that the same way that after using ALL(myTable) you can reinstate the context filter of a certain column by including VALUES(myTable[myColumn]) as a filter expression, I instead wanted to apply VALUES(Field_2) as a table filter expression against Filed_1 (a different column). It seemed that this would be orders of magnitude more efficient than iterating over every value in Field_2. I'm not really sure how I can do this though.