cancel
Showing results 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

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 1 100 B 1 101 B 2 102 A 2 103 B 3 104 B 4 105 C

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
Community Support

Hi, @Z7_852

You can try the following methods.

Measure:

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

``````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))``````

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.

2 REPLIES 2
Community Support

Hi, @Z7_852

You can try the following methods.

Measure:

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

``````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))``````

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.

Memorable Member

HI, @Z7_852

try below

``````Measure =

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

``````

Announcements

#### 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.

#### Power BI Monthly Update - February 2024

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

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### 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
Top Kudoed Authors