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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.