Distinct counting across multiple relationship tables

masplin

Active Member
Joined
May 10, 2010
Messages
413
Struggling with the syntax having reorganized my tables as recommended. my tables look like this

Transactions
INVNUMService
123MOT
123LUBE
123AIR
567MOT
567LUBE
567AIR

<tbody>
</tbody>
Invoices
INVNUMRegNumSurnamePostcodeUnique customer
123OE08DYOSmithAL1 3QBAL1 3QB - Smith
567OE08DYOSmithAL1 3QBAL1 3QB - Smith

<tbody>
</tbody>
Customers
Unique CustomerMOT CountUnique MOT Vehicles
AL1 3QB - Smith21

<tbody>
</tbody>
So each unique customer has multiple INVNUM and each INVNUM has multiple transactions. transaction[invnum] is related to invoices[invnum] and invoices[unique customer] is related to customers[unique customer]

To calculate MOT I needed to count the number of transaction lines with "MOT" where the INVNUM linked to the Custoter via the Invoices relationships. I managed ot work this out as


<tbody>
</tbody>

<tbody>
</tbody>

Code:
=sumx(RELATEDTABLE(Invoices),CALCULATE(COUNTROWS(Transactions),filter(Transactions,Transactions[INVNUM]=Invoices[INVNUM]  && Transactions[Service]="MOT" )))

So far so good. However I also wanted to be able to count how many different vehicles the customer had brought in for MOT using unique RegNum. In this case he has brought the same vehicle twice so the answer should be 1. I have tried everything, but cannot work out how to calculate this. in my first version of the model I had the RegNum and customer details in the transaction table so could use.
Code:
=CALCULATE(DISTINCTCOUNT(Transactions[RegNum]),filter(Transactions,Transactions[Unique Customer]=Customers[Customer Code] && Transactions[Service]="MOT"))

I tried various things including this, but this just gave me the same as the MOT Count i.e. =2.

Code:
=sumx(RELATEDTABLE(Invoices),CALCULATE(DISTINCTCOUNT(Invoices[RegNum]),filter(Transactions,Transactions[INVNUM]=Invoices[INVNUM] && Transactions[Service]="MOT")))

Is this possible or do I have to RegNum back into the transaction table and use say DISTINCTCOUNT(transactions[regnum])?

This is all new to me having a triple layer cake so any advice appreciated.

Mike
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Thanks but I dont see the connection. When i had just 2 tables with a relationship between the formula was simple. I now have 3 tables with 2 relationships and that is where I am struggling. The "MOT" data sits in one table and the "regnum" filters sits in the 2nd table. So essentially i am filtering table 1 based on "MOT" then counting the disitinct "REGNUM" for those invoices that match both the "MOT" transactions and the unique customer.
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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