Using CONTAINS to find data dependent on row/column context of a pivot table

masplin

Active Member
Joined
May 10, 2010
Messages
413
I have a table of values Depot_GP_Type. Depot Abbrev is related to a Table "Depot" and Type is related to a table "Type" that contain just the unique values of each

Depot AbbrevTypeGPTV
BASOther£50
BASMOT£27
BASCombi£75
BASService£23
BASBrand£34
BRIOther£50
BRIMOT£41

<tbody>
</tbody>

i have a pivot table with rows Depot[Depot] and columns Type[Type]. I am trying to work out how to select the appropriate number form the first table and get it in the right cell. for example

MOT Service
BAS 27 23
BRI 41

i thought this might work although i'm not 100% sure (obviously as it doesn't!!) how contains works as trying to use the pivot table context as a filter to find the values. however says it is returning multiple values. Am I even close?

Code:
=CALCULATE(                                  VALUES(Depot_GP_Type[GPTV]),
                                  FILTER(
                                             Depot_GP_Type,
                                              CONTAINS( Depot_GP_Type, Depot_GP_Type[Depot Abbrev],VALUES(Depot[Depot]))  &&
                                              CONTAINS( Depot_GP_Type, Depot_GP_Type[Type],VALUES('Type'[Type]))
                                              )
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
A pivot table is an aggregation tool. If you set up your pivot like you go have suggested, then you need to write a measure that will aggregate the values in the GPTV column. You could count the values

=countrows(Depot_GP_Type)

or sum the values

=sum(Depot_GP_Type[GPTV])

the values approach you describe can work on text columns when there is only a single value as long as you protect for the case where there is more than 1 value by wrapping the entire formula in an IF(HASONEVALUE( construct. But if you know there is only 1 value, then why not just use SUM.
 
Last edited:
Upvote 0
Hi Matt

I change the code but still says multiple values

Code:
CALCULATE(
                                  SUM(Depot_GP_Type[GPTV]),
                                  FILTER(
                                             Depot_GP_Type,
                                              CONTAINS( Depot_GP_Type, Depot_GP_Type[Depot Abbrev],VALUES(Depot[Depot]))  &&
                                              CONTAINS( Depot_GP_Type, Depot_GP_Type[Type],VALUES('Type'[Type]))
                                              )
                                             
                                 )

I suppose the question is whether my CONTAIN clauses is doing what I expect. The intention is to filter the table Depot_GP_Type to give me a unique row that has the Depot Abbrev = Depot[Depot] on rows and the Type[Type] = type on the columns.
 
Upvote 0
Ah I see. The reason I was trying to do this was actually a more complicated calculation and this was a stepping stone. I have another table with contracts per depot per type. I want to multiple the contract by this GPTV to get a sales value. Using Sum(GPTV) works correctly for each type but the total is completely wrong as it is summing the GPTV for all type and then multiplying by the total contracts so comes out miles too high.

I was therefore trying to construct some kind of SUMX calculation that multiplied each contract by the related GPTV and then summed the result. For example the bottom row should have a total 679 not 4862 i.e. the sum of the 5 columns. The [Estimated Bookings] is a measure not a table column, based off data in a different table.

Type
DepotMOTServiceCombiOtherBrandGrand Total
Estimated Bookings
BAS1342122
BRI141020633
CAR9101729
KET8400216
LEE4811025
LIV2723301065
MIL741215
NOR139312249
NOT81020931
STE570620
STO3510415
WAR19186751
WEN21520828
Sum of GPTV
BAS£30£23£79£50£39£221
BRI£52£119£149£50£93£463
CAR£61£128£164£50£109£512
KET£49£93£115£50£66£373
LEE£27£22£71£50£31£201
LIV£35£28£88£50£52£253
MIL£40£102£147£50£70£409
NOR£54£115£131£50£89£439
NOT£43£116£166£50£103£478
STE£47£86£132£50£83£398
STO£63£96£126£50£96£431
WAR£58£40£116£50£72£336
WEN£43£114£156£50£98£461
Total Estimated Bookings127135292104399
Total Sum of GPTV£602£1,082£1,640£650£1,001£4,975
BAS390921580394862

<tbody>
</tbody>


I tried this sort of thing which is correct for individual cells but all the totals are completely wrong as using the SUM of GPTV for all types or all depot rather than summing the components. I know SUMX would work if I just had 2 columns of data in a table so thought maybe the ADDCOLUMNS would mimic this.

Code:
=SUMX(
              ADDCOLUMNS(
                                        PPC,
                                        "Type GPTV",SUM(Depot_GP_Type[GPTV])
                                        ),
              PPC[Estimated Bookings]*[Type GPTV]
              )
 
Upvote 0
Power Pivot is a system of tables and relationships that interoperate together. It is very difficult (maybe impossible) to give you a correct formula without seeing the underlying table structure. have a read of this blog post I wrote and it may help you. Otherwise, please post a picture of the diagram view of the data model and indicate which tables have the relevant columns you are trying to use.
When to use SUM vs SUMX in DAX
 
Upvote 0
ADDCOLUMNS is an iterator but doesn't do automatic context transition. The SUM(Depot_GP_Type[GPTV]) will Sum the whole table (as you noted), not the ones related to each row of PPC. You need to wrap it in a CALCULATE to get transition.

CALCULATE ( SUM ( Depot_GP_Type[GPTV] )).
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top