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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
lalala932
Frequent Visitor

Count occurrence based on condition

Hi

 

I have a table with multiple years, customers, and products (along with some other metrics such as country, age, etc). I would like to find out how many unique products each customer bought each year. I will then split this by country, age etc.

 

First, I don't know whether it's better to create a new table or measures? Probably measures?

 

I wanted to use this solution, but it only counts the number of occurences - I need a number of occurences of unique products for each unique customer, but then also filtered for each year.

 

Can someone please help? 

 

1 REPLY 1
daXtreme
Solution Sage
Solution Sage

Hi @lalala932 

 

Please create a proper star-schema model for this unless you want to face issues impossible to resolve in a one-table model (speaking from experience) later on.

 

Here's some mandatory reading for all Power BI aspiring adepts: Understand star schema and the importance for Power BI - Power BI | Microsoft Docs

 

Once you've got a proper model, then you can start writing DAX. Not the other way round. The measure will be something along these lines:

// Say that you've got the following dimensions:
// Customers, Products, Dates (the date table in the model).
// You also have a fact table that these join to.
// Fact table should always have its all columns hidden,
// apart from degenerate dimensions (which you I believe
// don't have anyway).

[# Unique Prods Bought] = DISTINCTCOUNT( FactTable[ProductID] )

// This simple measure looks at the fact table
// (where there's a key to the Products dimension)
// and returns the number of distinct keys that are
// visible in the current context. If you start slicing
// and dicing your model by dimensions, it'll adjust
// automatically to the selections. So, if you, say,
// select a year from your Dates table and a customer
// from Customers, it'll return the number of distinct
// products bought by the customer in the very year.

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.