Count number of cities in state

rdlib

New Member
Joined
May 7, 2015
Messages
5
I have a table that lists States and Cities. I need a measure that counts how many distinct cities exist for each state.

As an example. Say there are 500 cities in CA and 10 Cities in NV. My result should look like

Los Angeles 500
San Diego 500
Las Vegas 10
Reno 10

Obviously if I wanted the results for each state I could do a simple distinctcount and be done with it. However since my results are for each city that's where I'm having some problems.

I tried the following with no success
StateID:=values(Table1[State])
Measure2 = CALCULATE(DISTINCTCOUNT(Table1[City]),filter(all(table1),Table1[State]=[StateID]))


If you can somehow get this working the second part is to add additional filters, so do a count of the cities but only those cities with populations over x.


Any help is greatly appreciated. Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Give this a try and am sure there is an easier way but worked on a test file for me.

CityCount:=CALCULATE(COUNTROWS(DISTINCT(Table1[City])),FILTER(ALL(Table1[City]),Table1[City]=Table1[City]))

StateCityCount:=SUMX(DISTINCT(Table1[State]),[CityCount])
 
Upvote 0
Wow thank you Kazlik. Seems strange that such a simple measure requires such a complex solution but it worked perfectly so that's fantastic!

Can you explain what "Table1[City]=Table1[City]" is doing? It works but don't understand the logic behind it. Thanks
 
Upvote 0
Kazlik, I wanted to include a filter for population and it works

=CALCULATE(sumx(DISTINCT(Table1[State]),[CityCount]),Table1[Population]>100)

The table headings for table1 are State, City, Population, Date. I have data going back a year so each city has multiple entries. I have multiple other tables and they all link back to a parent table called 'StateCityUnique'.

When I use my City column from the 'StateCityUnique' table in my PivotTable my StateCityCount measure no longer works. Any ideas what to do?
 
Upvote 0
If you have a relationship say on city between StateCityUnique and this table then you should be able to update the measure and then pull in the sate, city from StateCityUnique and then your counts would be based of the data in table1. I am still new to this like you so they could be a simpler way to do this as well.


CALCULATE(COUNTROWS(DISTINCT(StateCityUnique[City])),FILTER(ALL(StateCityUnique[City]),StateCityUnique[City]=StateCityUnique[City]))
 
Upvote 0
Even though my list of cities come from StateCityUnique table, I need to do the count off of Table1 because I need to filter based on month and population (the StateCity Table doesn't have this data). In any case i greatly appreciate all the help you've been so far. I feel like i'm a lot closer to a solution.
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,226
Members
449,303
Latest member
grantrob

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top