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
Anonymous
Not applicable

Distinct Count based on overlapping values on another row

Greetings!

 

I want to create a measure that gives me the number of cities that have a store overlap for the Companies, with a given table:

 

CityCompanyExpected result
New YorkAlpha2
New YorkAlpha2
New YorkAlpha2
New YorkBeta2
New YorkBeta2
BuffaloGama1
HoustonBeta1
San FranciscoAlpha1

 

For example: New York have 2 distinct values for Companies (ALPHA and BETA), so I want the measure to return me the value '2'. I can't use a 'countif(company = alpha)' + 'countif(company = beta)' because it would return '5'. I can't also use a 'CountIf(company = alpha && beta)' because there's no row like this.

 

Is there a way to use DISTINCTCOUNT to make this work? I managed to do this creating dummies for the City*Company, but I bet there's a smarter way to do it.

 

PS: please let me know if there's a better way to name this issue, or if there's similar solution for this matter. I did find something close to this here but it solves the issue on Power Query.

 

Thanks in advance!

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Write this calculated column formula

=calculate(distinctcount(Data[company]),filter(data,data[city]=earlier(data[city])))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

Write this calculated column formula

=calculate(distinctcount(Data[company]),filter(data,data[city]=earlier(data[city])))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi Ashish, thank you so much for the prompt response!

This works, it returns the number of companies that are present in each city! Another question followed my analysis afterwards.

Is there a way to mark with a measure/dummy all the cities where Alpha and Beta are present? So, for the table below, DAX would return me 3 (New York, Miami, and Tampa).

 

CityCompany
New YorkAlpha
New YorkBeta
New YorkAlpha

Denver

Beta
Denver

Gama

Salt Lake City

Gama

Salt Lake City

Alpha

Buffalo

Alpha

Buffalo

Alpha

Miami

Beta

Miami

Alpha

TampaBeta
TampaAlpha
TampaBeta

 

I tried writing =calculate(distinctcount(Data[company]),filter(data, data[Company] = "Alpha" || data[Company] = "Beta"),filter(data,data[city]=earlier(data[city])))), but it didn't return the expected result.

 

Thank you!

Hi,

Write this measure and see the image below

Measure = CALCULATE(DISTINCTCOUNT(Data[Company]),Data[Company]="Alpha"||Data[Company]="Beta")

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.