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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

How to sort area codes using DAX

Hi,

 

I have a bunch of area codes that I want to sort out to make it more presentable in a graph. So for example, I have BR1, BR3, E10, E7, E5, SE9, SW19, WC2B etc. Is there a way for me to sort it out so that at any time the area code is E or SW it sorts it out into another column with the area such as East or South-West? 

 

Thanks in advance. 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@goncalogeraldes helped me figure out a solution using nested if statements 🙂 

 

For anyone else struggling, here's an example of what I used: 

= Table.AddColumn(#"Renamed Columns3", "Region", each 
if Text.Start([AreaCode],2) = "SW" then "South-West" else /* for conditions where you need the first 2 letters */
if Text.Start([AreaCode],2) = "SE" then "South-East" else
if Text.Start([AreaCode],2) = "BR" then "Bromley" else
if Text.Start([AreaCode],2) = "WC" then "West-Central" else
if Text.Start([AreaCode],2) = "EC" then "East-Central" else
if Text.Start([AreaCode],2) = "NW" then "North-West" else
if Text.Start([AreaCode],1) = "E" then "East" else /* for conditions where you only need the first letter */
if Text.Start([AreaCode],1) = "N" then "North" else
if Text.Start([AreaCode],1) = "W" then "West" 
else "London" )

 

If anyone else is doing something similar, I would note that I had issues with putting the single letter statements first as it would lump the double letter regions into the same one. For example, if I put North first, it would lump North-West into the same region. 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

@goncalogeraldes helped me figure out a solution using nested if statements 🙂 

 

For anyone else struggling, here's an example of what I used: 

= Table.AddColumn(#"Renamed Columns3", "Region", each 
if Text.Start([AreaCode],2) = "SW" then "South-West" else /* for conditions where you need the first 2 letters */
if Text.Start([AreaCode],2) = "SE" then "South-East" else
if Text.Start([AreaCode],2) = "BR" then "Bromley" else
if Text.Start([AreaCode],2) = "WC" then "West-Central" else
if Text.Start([AreaCode],2) = "EC" then "East-Central" else
if Text.Start([AreaCode],2) = "NW" then "North-West" else
if Text.Start([AreaCode],1) = "E" then "East" else /* for conditions where you only need the first letter */
if Text.Start([AreaCode],1) = "N" then "North" else
if Text.Start([AreaCode],1) = "W" then "West" 
else "London" )

 

If anyone else is doing something similar, I would note that I had issues with putting the single letter statements first as it would lump the double letter regions into the same one. For example, if I put North first, it would lump North-West into the same region. 

lbendlin
Super User
Super User

Please provide more context.  Are you expecting this to be based on geocoding calls, or on the starting characters of the area codes (for example the letters before the first number digit) ?

Anonymous
Not applicable

Hi, 

 

Yes, I'd like to return all the letters before the digits and sort them by region. So at the end instead of having: 

E1 I would have E for a regional breakdown of East 

SW19 -> SW -> South-West

SE9 -> SE -> South-East 

 

I hope this makes sense. Sorry I'm still quite new to PowerBI 🙂

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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