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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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
Memorable Member
Memorable Member

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors