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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
BoCoKeith
New Member

Count Customers by Product in Order/LineItem Structure

I think I'm missing something fundamental.  Given this PowerBI data model:

 

BoCoKeith_0-1597078242491.png

 

I'm trying to build a matrix showing the number of customers that have purchased a particular product.  The table contents are shown below but, trust me for now, not every customer purchased every product.  Here's my matrix definition and the results:

 

BoCoKeith_3-1597078787160.png

("Customers" is a distinct count.)

 

It counts the number of *orders* correctly for each product, but not the number of *customers*.  I assumed that the table relationships would sort this out for me, but maybe not?  Do I need to denormalize CustomerId into LineItem?

 

Here's the SQL script I used to build and populate the tables.

 

DROP TABLE IF EXISTS dbo.temp_LineItem;
DROP TABLE IF EXISTS dbo.temp_Order;
DROP TABLE IF EXISTS dbo.temp_Product;
DROP TABLE IF EXISTS dbo.temp_Customer;
GO

CREATE TABLE dbo.temp_Customer(
    CustomerId INT NOT NULL PRIMARY KEY,
    CustomerName VARCHAR(100) NOT NULL
);

CREATE TABLE dbo.temp_Product(
    ProductId INT NOT NULL PRIMARY KEY,
    ProductName VARCHAR(100) NOT NULL
);

CREATE TABLE dbo.temp_Order(
    OrderId INT NOT NULL PRIMARY KEY,
    CustomerId INT NOT NULL REFERENCES dbo.temp_Customer(CustomerId),
    OrderDate DATE NOT NULL
);

CREATE TABLE dbo.temp_LineItem(
    LineItemId INT NOT NULL PRIMARY KEY,
    OrderId INT NOT NULL REFERENCES dbo.temp_Order(OrderId),
    ProductId INT NOT NULL REFERENCES dbo.temp_Product(ProductId),
    Quantity INT NOT NULL
);

INSERT dbo.temp_Customer(CustomerId, CustomerName)
VALUES (1, 'Best Products Ever'), (2, 'My Company'), (3, 'Widgets Inc');

INSERT dbo.temp_Product(ProductId, ProductName)
VALUES (1, 'Widget'), (2, 'Doodad'), (3, 'Gizmo');

INSERT dbo.temp_Order(OrderId, CustomerId, OrderDate)
VALUES (1, 1, '1/1/2020'), (2, 1, '2/1/2020'), (3, 2, '1/15/2020'), (4, 2, '2/15/2020'), (5, 3, '3/1/2020');

INSERT dbo.temp_LineItem(LineItemId, OrderId, ProductId, Quantity)
VALUES (1, 1, 1, 1), (2, 1, 2, 1),
       (3, 2, 1, 1), (4, 2, 2, 1), (5, 2, 3, 1),
       (6, 3, 2, 1);
GO

SELECT * FROM dbo.temp_Customer;
SELECT * FROM dbo.temp_Product;
SELECT * FROM dbo.temp_Order;
SELECT * FROM dbo.temp_LineItem;

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @BoCoKeith 

If you change the relationship from single to both, you will find the answer is correct.

1.png2.png

But Both direction is not recommended in Power BI. So you may try to use measure or merge to achieve your goal.

Measure:

Discount CustomerID = CALCULATE(DISTINCTCOUNT(temp_Order[CustomerId]),CROSSFILTER(temp_LineItem[OrderId],temp_Order[OrderId],Both))

Result:

3.png

Merge:

Select Merge Queries as New.

4.png1.  Merge temp_Product and temp_LineItem by ProductID column in each table

2.  Merge New Table and temp_Order by OrderID column in each table. 

5.png

The Final table is as below.

6.png

Result:

7.png


You can download the pbix file from this link: Count Customers by Product in Order/LineItem Structure

 

Best Regards,

Rico Zhou

 

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
Anonymous
Not applicable

Hi @BoCoKeith 

If you change the relationship from single to both, you will find the answer is correct.

1.png2.png

But Both direction is not recommended in Power BI. So you may try to use measure or merge to achieve your goal.

Measure:

Discount CustomerID = CALCULATE(DISTINCTCOUNT(temp_Order[CustomerId]),CROSSFILTER(temp_LineItem[OrderId],temp_Order[OrderId],Both))

Result:

3.png

Merge:

Select Merge Queries as New.

4.png1.  Merge temp_Product and temp_LineItem by ProductID column in each table

2.  Merge New Table and temp_Order by OrderID column in each table. 

5.png

The Final table is as below.

6.png

Result:

7.png


You can download the pbix file from this link: Count Customers by Product in Order/LineItem Structure

 

Best Regards,

Rico Zhou

 

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

amitchandak
Super User
Super User

@BoCoKeith , better to merge temp order and temp order line in power BI

 

https://radacad.com/append-vs-merge-in-power-bi-and-power-query

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.