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
masplin
Impactful Individual
Impactful Individual

Calculating sum of locations in neighbouring postcodes

I am doing some analysis of restaurant density. i have create a list of restaurants and their postcode district (the AL1 bit). i have easily counted how many competitors to each restaurant there are in the SAME postcdoe district using a calcualted column

 

=CALCULATE(
[Count],
FILTER(
Restaurants,
Restaurants[Postcode District]=EARLIER(Restaurants[Postcode District]) &&
Restaurants[Chain]<>EARLIER(Restaurants[Chain])
)
)

 

Now i want to try to count how many others are in neighbouring districts. i have created another table caleld Postcode_NearbyDistricts that contains 2 columns, firstly the district then the neighbouring district. so for each district there might be say 10 rows. for example for District L2

 

Postcode District Nearby District

L2CH25
L2L1
L2L3
L2L5
L2L67
L2L69
L2L70
L2L71
L2L73
L2L74

 

My restaurant table looks like this

 

Branch Postcode District

LiverpoolL1
Albert DockL3
AughtonL39
OtterspoolL17
LiverpoolL1
LiverpoolL1
LiverpoolL2
LiverpoolL1
LiverpoolL37

 

 

So for a row on the restaurant table i need to look up the nearby district rows and then filter the restaurant table for the 10 that match and count the rows.  i just can't work out how to do this if anyone can help. There are multiple districts on both tables so cant use any relationship

 

Much appreciated

 

Mike

 

2 REPLIES 2
v-danhe-msft
Employee
Employee

Hi @masplin,

Based on my test, you can refer to below steps:

1.I have entered some sample data to test for your problem in below pictures.

A.PNGB.PNG

 

2.Create a calculated column to count the relative district.

Related district number = COUNTROWS(FILTER(RELATEDTABLE('Postcode District Nearby Distri'),Restaurant[District]='Postcode District Nearby Distri'[District]))

 

3.Create a Table visual and add the [Name], [District] and [Related district number] field and you can see the result.

C.PNG

You can also download the PBIX file to have a view.

https://www.dropbox.com/s/geu61x1iv20wqa4/Calculating%20sum%20of%20locations%20in%20neighbouring%20p...

 

Regards,

Daniel He

 

 

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Sorry maybe wasnt clear as problem is more complicated than that. In my example I have one restaurant that is in L2

 

L2 has 10 neighbouring postcode districts. Now i need to take these 10 districts and go back to the restaurant table and count how many are in those. so in my example the restaurants in L1 & L3 are neighbouring and there are 5 of those. The restaurants in L2,L39, L17 and L37 are no neighbouring so dont count.  The answer is therefore 5.

 

I tried this code, but it is a circular reference, but logically seems sort of right process

 

In middle CALCULATE I tried to get a table of values of the nearby districts for the specific row I was on.

Then FILTER the restaurants table for just the districts in the table found from the NearbyDistricts table that are related

Then SUM the [count] (just a row count measure) over the fitlered table

 

=CALCULATE(
            SUMX(
                 FILTER(Restaurants,
                 [Postcode District]=
                  CALCULATE(
                            VALUES(Postcode_NearbyDistrict[Nearby District]),
                            Restaurants[Postcode District]=Postcode_NearbyDistrict[Postcode district]
                            )
                      )  ,
                   [Count]                           
                   ))

I have hacked this together in excel usingh a terrible method.  I have all my restaurants down the side and all the possible postcode districts with neighbouring ditricts underneath along the top.  Massive matrix looking up each restaurants related districts then a formula in each column to go and count how many for each of the related ditricts. Then sum the row. It works but its rubbish

 

formula is =IF($D4=F$2,SUMIF($D:$D,F$3,$E:$E),"").  First column is D, first row is 2. so if Restaurant district = top row district then use the 2nd row of ditricts to sum the count column where the districts match

 

 

Postcode DistrictCount

  AB10AB10AB10AB10
  AB1AB11
W1B1    
NW31    
SW31    
EC3V1    

 

One question is how come you can use RELATEDTABLE when it is a Many to Many relationship?  Is this because as a clauted column the relationship is context filtered to only have one value coming from the Restaurant table?  if so that would mean your idea of RELATEDTABLE gnerates the table of values needed to feedback to the restaurant table. I suspect you still get a circular reference.

 

Mike

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.