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

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

Reply
Anonymous
Not applicable

Count amount of occurrence with filter in Power Query

So I got the following Calculated Column created with DAX code. The goal is to count how many products each customer has. Each product has an unique ID and we do have a field end date which is filled in if the customer does not use their product anymore.  

Amount of different products in use per customer   =
VAR __Customer = FACT_Sales[Master ID]
VAR __DCountVIN =  CALCULATE(DISTINCTCOUNT(FACT_Sales[uniqueProductID]), ISBLANK(FACT_Sales[End_Date]))
 RETURN
__DCountVIN

This column works, but it takes forever to load. I'm looking to optimize it a bit and was thinking of doing this in Power Query. But I'm struggling in converting the code to Power Query. Is this even possible? Anyone that can support? 

 

Thanks in advanced! 

 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Anonymous 

 

Download example PBIX file

 

After seeing your data I decided to use another approach rather than the grouping I initially thought of.

 

Add a Custom Column in Power Query and you can do this with this line of code

= List.Count(List.Distinct(let _CustID = [Customer ID] in Table.SelectRows(#"Changed Type", each [Customer ID] = _CustID)[Unique Product ID]))

 

custs1.png

 

How it works:

 

This stores the Customer ID in a variable calld _CustID

let _CustID = [Customer ID]

 

This selects the rows in the table from the previous step (#"Changed Type" - you may have to change this to match your query) where the Customer ID = _CustID

Table.SelectRows(#"Changed Type", each [Customer ID] = _CustID)

 

This returns a list of the distinct Products each Customer uses

List.Distinct(.... , [Unique Product ID])

 

This counts the number of items in that list

List.Count

 

You'll notice my results are different to the ones you show in your sample data, but I believe mine are correct.  For example, Customer 1 uses Products A, D and C.  Customer 2 uses B, E, F and G.

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

5 REPLIES 5
PhilipTreacy
Super User
Super User

Hi @Anonymous 

 

Download example PBIX file

 

After seeing your data I decided to use another approach rather than the grouping I initially thought of.

 

Add a Custom Column in Power Query and you can do this with this line of code

= List.Count(List.Distinct(let _CustID = [Customer ID] in Table.SelectRows(#"Changed Type", each [Customer ID] = _CustID)[Unique Product ID]))

 

custs1.png

 

How it works:

 

This stores the Customer ID in a variable calld _CustID

let _CustID = [Customer ID]

 

This selects the rows in the table from the previous step (#"Changed Type" - you may have to change this to match your query) where the Customer ID = _CustID

Table.SelectRows(#"Changed Type", each [Customer ID] = _CustID)

 

This returns a list of the distinct Products each Customer uses

List.Distinct(.... , [Unique Product ID])

 

This counts the number of items in that list

List.Count

 

You'll notice my results are different to the ones you show in your sample data, but I believe mine are correct.  For example, Customer 1 uses Products A, D and C.  Customer 2 uses B, E, F and G.

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

Hi Phil! 

 

Thanks a lot for your reply and solution!  Indeed your results where different because it did not take in account the records with an end date (which it should do)

I modified your query a bit and added a "end end date = null" which works. 

 

= List.Count(List.Distinct(let _CustID = [Customer ID] in Table.SelectRows(#"Changed Type", each ([Customer ID] = _CustID) and ([End date product use] = null))
[Unique Product ID]))

Again thanks a lot and I wish you a nice weekend!

 

Kind regards

Sofian

PhilipTreacy
Super User
Super User

Hi @Anonymous 

 

You could group by Customer and use Distinct Count as the aggregate for the Product.

 

Please provide some sample data and I'll show you an example.

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

Hi Philip,

 

Did you have time to have a look into this? I'm a bit stuck and your support would be really great. :😁

Anonymous
Not applicable

Hi Philip, 


Here you can find a file with some sample data: https://we.tl/t-vrK1D7DUbG

 

(For future reference, this is the sample data: 

Customer IDSales dateUnique Product IDEnd date product use
Amount of different products in use per customer
101 January 2020A06 January 20202
202 January 2020B07 January 20203
303 January 2020C08 January 20201
104 January 2020D 2
205 January 2020E 3
206 January 2020F 3
307 January 2020A 1
508 January 2020B 1
109 January 2020C 2
410 January 2020H15 January 2020 
211 January 2020G 3

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Kudoed Authors