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, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
raksharh
Microsoft Employee
Microsoft Employee

Choose Aggregate function conditionally

Hi Everyone,

 

I need to calculate count/DistinctCount based on a condition. 

For example in below table, when Type is "Agreement1", I should determine total count of "Customer" for "Agreement1". 

When Type is Agreement2, I should determine distinct count of "Customer" for "Agreement2"

 

PartnerCustomerType
p1c1Agreement1
p1c2Agreement1
p2c1Agreement1
p2c3agreement2
p3c2agreement2
p4c3agreement2
p1c3agreement2

 

In the below case, I should get numbers like this

Agreement1agreement2
32

 

Can you please let me know how can we acheive this through single DAX measure.

 

NOTE: I even have date columns, so I should be able to slice it accordingly through dates

 

Thanks,

Raksha

1 ACCEPTED SOLUTION
harshnathani
Community Champion
Community Champion

 

Hi @raksharh ,

 

You can use the below measure:

 

Count of Customer =
var __disctinct = CALCULATE(DISTINCTCOUNT(Table1[Customer]),ALLEXCEPT(Table1,Table1[Type]))
var __common = CALCULATE(COUNT(Table1[Customer]),ALLEXCEPT(Table1,Table1[Type]))
return
IF(TOPN(1,VALUES(Table1[Type]),VALUE(RIGHT(Table1[Type],1)),ASC) = "Agreement1", __common,__disctinct)
 
This will only work for your above scenario. Incase you have more agreement types, the measure will not work.
Logic is to find last digit of Type and convert in into number and then do a TOPN.
So, Agreement 1 will be TOP1 when you do ASC and vice-versa.
 
I think incase you have multiple agreement types go for multiple measures.
 
 
123.JPG
Thanks and Regards,
Harsh Nathani
 
 
 

View solution in original post

2 REPLIES 2
harshnathani
Community Champion
Community Champion

 

Hi @raksharh ,

 

You can use the below measure:

 

Count of Customer =
var __disctinct = CALCULATE(DISTINCTCOUNT(Table1[Customer]),ALLEXCEPT(Table1,Table1[Type]))
var __common = CALCULATE(COUNT(Table1[Customer]),ALLEXCEPT(Table1,Table1[Type]))
return
IF(TOPN(1,VALUES(Table1[Type]),VALUE(RIGHT(Table1[Type],1)),ASC) = "Agreement1", __common,__disctinct)
 
This will only work for your above scenario. Incase you have more agreement types, the measure will not work.
Logic is to find last digit of Type and convert in into number and then do a TOPN.
So, Agreement 1 will be TOP1 when you do ASC and vice-versa.
 
I think incase you have multiple agreement types go for multiple measures.
 
 
123.JPG
Thanks and Regards,
Harsh Nathani
 
 
 
ibarrau
Super User
Super User

Hi! If you just need those two measures let's see.

I think this should be enough.

Agreements1 =
CALCULATE (
    COUNT(Table[Customer]),
    Table[Type] = "Agreement1"
)

In the second case 

Agreements2 =
CALCULATE (
    DISTINCTCOUNT(Table[Customer]),
    Table[Type] = "agreement2"
)

Hope this help,

Regards,


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

Happy to help!

LaDataWeb Blog

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

Top Solution Authors