Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Z7_852
New Member

How to calculate sum of single value from related table

I have three tables: [Category] that has relationship to [Purchase] that has relationship to [Customer]. All relationships are one to many.

Now I want to calculate number of customers that have bought only from specific category "B".

 

Doing a simple Calculate(DISTINCTCOUNT(Customer[Id], Filter[Category, Category[type] = "B") returns all sales from said category. I want to know number of customers who have only bought from this one category (once or more times).

 

Example

Customer
Purchase
Category
1100B
1101B
2102A
2103B
3104B
4105C

 

This should return 2 because Customers 1 and 3 have bought only "B". Where as now it returns 3 because client 2 have also bought "B" as well as "A".

1 ACCEPTED SOLUTION
v-zhangti
Community Support
Community Support

Hi, @Z7_852 

 

You can try the following methods.

Measure:

Count1 = CALCULATE(DISTINCTCOUNT(Purchase[Category]),ALLEXCEPT(Customer,Customer[Customer]))

vzhangti_0-1701070953310.png

Count result = 
Var _table=CALCULATETABLE(VALUES(Customer[Customer]),FILTER(ALL(Customer),[Count1]=1))
Return
CALCULATE(DISTINCTCOUNT(Customer[Customer]),FILTER(ALL(Category),[Category]="B"),FILTER(ALL(Customer),[Customer] in _table))

vzhangti_1-1701070995661.png

Is this the result you expect? Please see the attached document.

 

Best Regards,

Community Support Team _Charlotte

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-zhangti
Community Support
Community Support

Hi, @Z7_852 

 

You can try the following methods.

Measure:

Count1 = CALCULATE(DISTINCTCOUNT(Purchase[Category]),ALLEXCEPT(Customer,Customer[Customer]))

vzhangti_0-1701070953310.png

Count result = 
Var _table=CALCULATETABLE(VALUES(Customer[Customer]),FILTER(ALL(Customer),[Count1]=1))
Return
CALCULATE(DISTINCTCOUNT(Customer[Customer]),FILTER(ALL(Category),[Category]="B"),FILTER(ALL(Customer),[Customer] in _table))

vzhangti_1-1701070995661.png

Is this the result you expect? Please see the attached document.

 

Best Regards,

Community Support Team _Charlotte

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Dangar332
Super User
Super User

HI, @Z7_852 

try below

Measure = 

 CALCULATE(DISTINCTCOUNT(Customer[Id]),
       FILTER( 
          ADDCOLUMNS(
             ALL('Customer[Id]),
             "countofcategory",CALCULATE(DISTINCTCOUNT(Category[type]))
          ),
         [d]<2
        ),
        Category[type] = "B"
  )


Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.