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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
v-rzhou-msft
Community Support
Community Support

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
v-rzhou-msft
Community Support
Community Support

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.