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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
Laurix
Frequent Visitor

Distinct count with distinct count as filter

Hi,

 

I have a problem which should be simple, but I cannot get the correct result.

The source table is 

 

Order IDProductProduct category
1111MillersBeer
1111VAT69Liquor
2222BudBeer
3333Jack DanielsLiquor
4444CarlsbergBeer
4444HeinekenBeer
4444Johhnie WalkerLiquor
5555BallentinesLiquor

 

and as a result, I need to get the count of Order numbers having one Product category only:

 

Product categoryOrder count
Beer1
Liquor2

(Orders 1111 and 4444 should not be counted since they include both categories).

 

The real tables have more than two product categories.

Any ideas on how I can accomplish this?

 

Thank you!

2 ACCEPTED SOLUTIONS
rsbin
Super User
Super User

@Laurix ,

I would do this in two steps:

First create a new Calculated Column

NumberofCategories = CALCULATE( DISTINCTCOUNT( [Product category] ), 
                                  ALLEXCEPT( 'Beer&Liquor','Beer&Liquor'[Order ID] ))

Order IDProductProduct categoryNumberofCategories

1111 Millers Beer 2
1111 VAT69 Liquor 2
2222 Bud Beer 1
3333 Jack Daniels Liquor 1
4444 Carlsberg Beer 2
4444 Heineken Beer 2
4444 Johhnie Walker Liquor 2
5555 Ballentines Liquor 1

Second step is to create a new Measure:

OrderCount = CALCULATE( COUNT( 'Beer&Liquor'[NumberofCategories] ), 
                     FILTER( 'Beer&Liquor', 'Beer&Liquor'[NumberofCategories] = 1 ))

There is probably a way to do this all in one step, but at least this will hopefully get you started.

Regards,

View solution in original post

@Laurix ,

Please try this as your Measure:

OrderCount = CALCULATE( DISTINCTCOUNT( 'Beer&Liquor'[Order ID] ), 
                     FILTER( 'Beer&Liquor', 'Beer&Liquor'[NumberofCategories] = 1 ))

Regards,

View solution in original post

6 REPLIES 6
Kishore_KVN
Super User
Super User

Hello @Laurix , I do have similar solution provided by @rsbin . 

Please look at it for your information:

Create two measure to get count and filter:

Measure one DAX looks like this:

Counting = CALCULATE(COUNT('Counting Data'[Order ID]),ALLEXCEPT('Counting Data','Counting Data'[Order ID]))

Measure two DAX looks like this:

Final Count = 
CALCULATE('Counting Data'[Counting],ALLEXCEPT('Counting Data','Counting Data'[Product category]),FILTER('Counting Data','Counting Data'[Counting]=1))

 

Output looks like this:

Kishore_KVN_0-1686760155894.png

 

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

 

Thanks for providing this solution.
Same as for the other solution, it does not work well when there are only products of same category on an order.

vrvp_0-1686768128134.png

For this case, the solution proposed indicates for order 6666 2 products of same category, thus, ingoring them in the "Final count" measure.

vrvp_1-1686768199570.png

The answer should be
Beer   2
Liquor 2

 

How can this be adjusted to work for this case as well?

 

Many thanks!



rsbin
Super User
Super User

@Laurix ,

I would do this in two steps:

First create a new Calculated Column

NumberofCategories = CALCULATE( DISTINCTCOUNT( [Product category] ), 
                                  ALLEXCEPT( 'Beer&Liquor','Beer&Liquor'[Order ID] ))

Order IDProductProduct categoryNumberofCategories

1111 Millers Beer 2
1111 VAT69 Liquor 2
2222 Bud Beer 1
3333 Jack Daniels Liquor 1
4444 Carlsberg Beer 2
4444 Heineken Beer 2
4444 Johhnie Walker Liquor 2
5555 Ballentines Liquor 1

Second step is to create a new Measure:

OrderCount = CALCULATE( COUNT( 'Beer&Liquor'[NumberofCategories] ), 
                     FILTER( 'Beer&Liquor', 'Beer&Liquor'[NumberofCategories] = 1 ))

There is probably a way to do this all in one step, but at least this will hopefully get you started.

Regards,

Interesting solution. Thanks for sharing it!

However, for a particular case it does not work well. Please see below:

vrvp_0-1686767623492.png

vrvp_1-1686767722398.png

Order 6666 contains only Beer products and should be counted as 1, instead of 2.

 

How can this be corrected?

Thank you very much!

@Laurix ,

Please try this as your Measure:

OrderCount = CALCULATE( DISTINCTCOUNT( 'Beer&Liquor'[Order ID] ), 
                     FILTER( 'Beer&Liquor', 'Beer&Liquor'[NumberofCategories] = 1 ))

Regards,

Nice, works well now. Many thanks!

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.