Postcode within range

Gwill1983

Board Regular
Joined
Oct 24, 2018
Messages
123
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have been supplied a list of postcodes setup in ranges which are allocated to a zone. I need to split these to show each individual postcode and what zone these are allocated to.

Before I go through manually to do this, I wanted to see if anybody knew if there was a way to allocate individual postcodes to a zone within a range.

For example,

The sheet that I have been provided looks as below

PostcodeZone
BD 1-22, 97, 993


On a seperate sheet I need to list each BD postcode individually noting that each of these is allocated to zone 3. Is there a quick way to do this rather than manually going through the list?

Thanks

Chris
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about this?

Book1.xlsb
ABCDEF
1PostcodeZonePostcode.1Postcode.2Zone
2BD 1-22, 97, 993BD13
3XY 1-5, 7, 11-144BD23
4BD33
5BD43
6BD53
7BD63
8BD73
9BD83
10BD93
11BD103
12BD113
13BD123
14BD133
15BD143
16BD153
17BD163
18BD173
19BD183
20BD193
21BD203
22BD213
23BD223
24BD973
25BD993
26XY14
27XY24
28XY34
29XY44
30XY54
31XY74
32XY114
33XY124
34XY134
35XY144
Sheet5


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    SplitBD = Table.SplitColumn(Source, "Postcode", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Postcode.1", "Postcode.2"}),
    SplitNum = Table.ExpandListColumn(Table.TransformColumns(SplitBD, {{"Postcode.2", Splitter.SplitTextByDelimiter(", ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Postcode.2"),
    ToList = Table.TransformColumns(SplitNum,{{"Postcode.2", (val)=>
        let 
            l =  List.Transform(Text.Split(val,"-"),Number.From),
            a = {l{0}..l{List.Count(l)-1}}
        in
            a
    }}),
    Expand = Table.ExpandListColumn(ToList, "Postcode.2")
in
    Expand
 
Upvote 0
Thanks. I have copy and pasted your solution into the formula tab in power query but it comes up with the error below:

1645694203670.png


All I have changed is the table name in the source part to match my table name (Raw_Data)

Am I missing something?
 
Upvote 0
When you get into Power Query, look on the 'Home' tab and find where it says 'Advanced Editor'. That's where you will need to paste all of the code.
 
Upvote 0
Aha! Thanks for the update.

It almost works! One thing that I haven't considered is that some of the postcode entries don't have ranges. For instance,

PostcodeZone
AB9

I think it might be these that are causing the error as looking at the query steps now these are showing as null in the postcode 2 column (snip below)

1645714188445.png



The step after this is the ToList step which is showing the errors

1645714233285.png
 
Upvote 0
How about this?

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Raw_Data"]}[Content],
    SplitBD = Table.SplitColumn(Source, "Postcode", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Postcode.1", "Postcode.2"}),
    SplitNum = Table.ExpandListColumn(Table.TransformColumns(SplitBD, {{"Postcode.2", Splitter.SplitTextByDelimiter(", ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Postcode.2"),
    ToList = Table.TransformColumns(SplitNum,{{"Postcode.2", (val)=>
        let 
            l =  try List.Transform(Text.Split(val,"-"),Number.From) otherwise val,
            a = try {l{0}..l{List.Count(l)-1}} otherwise val
        in
            a 
    }}),
    Expand = Table.ExpandListColumn(ToList, "Postcode.2"),
    Merge = Table.CombineColumns(Table.TransformColumnTypes(Expand, {{"Postcode.2", type text}}, "en-US"),{"Postcode.1", "Postcode.2"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Postcode")
in
    Merge

Book1
ABCDE
1PostcodeZonePostcodeZone
2BD 1-22, 97, 993BD 13
3AB9BD 23
4XY 1-5, 7, 11-144BD 33
5BD 43
6BD 53
7BD 63
8BD 73
9BD 83
10BD 93
11BD 103
12BD 113
13BD 123
14BD 133
15BD 143
16BD 153
17BD 163
18BD 173
19BD 183
20BD 193
21BD 203
22BD 213
23BD 223
24BD 973
25BD 993
26AB 9
27XY 14
28XY 24
29XY 34
30XY 44
31XY 54
32XY 74
33XY 114
34XY 124
35XY 134
36XY 144
Sheet1
 
Upvote 0
Thats brilliant!

However, it doesn't work on postcodes which include letters (ie London postcodes)

Getting the error below
1645718935034.png


This is for the post codes E1-1W.

Any thoughts how to avoid these? There arent many so can be a manual entry, but this is stopping the lists being extended so returning as below:

1645719415198.png
 
Upvote 0
Hi, I have been playing with this all morning to find a solution, but I am really struggling.

Does anybody have any further ideas how to solve this?

I have tried to remove the postcodes that include text at the end, but then I have the problem of trying to do a partial look up which creates another problem!

Any thoughts?
 
Upvote 0
I probably can't help till Sunday, but it would be helpful if you could post a good mix of sample data.
 
Upvote 0
Thanks for your help.

The data I receive is as below with postcode range in the first column, and zone in the second:

AB9
AL32
B (excluding B1-5)1
B 1-5101
BA (excluding BA1)5
BA 1105
BB 0-6, 10-12, 941
BB 7-9, 182
BD 1-22, 97, 993
BD 23-244
BH32
BL1
BN 1-236
BN 3, 11-18, 21-24, 26-27, 41-45, 50-52, 8831
BN 5-10, 20, 256
BR31
BS 1-21, 30-41, 48-49, 80, 98-99L
BS 22-29L
CA6
CB 1-3, 8-11, 21-2332
CB 6-7, 24-254
CB 4, 55
CF 3, 5, 10-11, 14-15, 30, 37-40, 46, 61-993
CF 31-36, 41-45, 47-485
CH1
CM 0, 3, 8-931
CM 1-25
CM 4-7, 11-16, 77, 92, 98-9932
CM 17-244
CO 1-3, 5, 7, 11-1631
CO 4, 6, 8-1032
CR 0, 2-3, 5-6, 8-9, 44, 9031
CR 4, 736
CT31
CV (except 31-37, 47)1
CV 31-37, 475
CW1
DA31
DD8
DE1
DG7
DH4
DL4
DN 1-7, 10-12, 22, 551
DN 8-9, 14-21, 38-392
DN 31-37, 40-413
DT6
DY1
E 2-4, 5-18, 20, 77, 9837
E 1 - 1W38
EC 1-4, 50, 9929
EC 8838
EH7
IG31
IM24
IP 1-3332
IP 9833
IV 1-28, 30-32, 36, 55-56, 63, 999
IV 40-49*, 51-54 (excludes island of Scalpay IV49)17
JE (Import tax may apply, please call)25
KA 1-26, 29-307
KA 2722
KA 2822
KT 1-8, 10, 12, 17-19, 216
KT 9, 11, 13-16, 20, 22-2431
KW 1-3, 5-149
KW 15*-17* (exludes islands of Hoy, Flotta, Graemsay, Barray, South Ronaldsay)18
KY 2-57
KY 1, 6-16, 998
L1
LA1-104
LA11-235
LD7
LE1
LL11-142
LL15-36, 40-41, 576
LL37-39, 42-49, 51-56, 58-787
LN 1-83
LN 9-134
LS 10-11, 20-271
LS 1-9, 12-13, 14-25, 28-29, 88, 98, 992
LU32
M1
ME31
MK 1-15, 17-19, 7732
MK 16, 43, 464
MK 40-42, 44-455
ML 1-77
ML 8-127
N1-2, 4-8, 10, 15-17, 19, 2237
N 3, 9, 11-14, 18, 20-21, 8136
NE4
NG1
NN1
NP (except 15-16, 25)3
NP 15-16, 255
NR 1-10, 13-23, 29-25, 9912
NR 11, 12, 24-285
NW 1, 3, 5, 6, 837
NW 2, 4, 7, 9-11, 2636
OL1
OX 1-5, 10-11, 13-18, 25-29, 33, 39, 44, 4932
OX 7, 125
PA 1-197
PA 2019
PA 21-3821
PA 41-49, 60-78, 8022
PE 1-5, 8-9, 991
PE 6-7, 10-112
PE 12, 13-16, 20-213
PE 22-25, 30-384
PE 19, 26-2933
PH 1-7, 11-148
PH 8-10, 15-189
PH 19, 20-26, 30-39, 40-41, 49-5010
PH 42-4422
PL7
PO 1-1732
PO 18-2231
PO 30-4123
PR2
RG31
RH 1-3, 7-96
RH 4-5, 12-20, 7731
RH 6, 10-1136
RM32
S1
SA 1-18, 80, 996
SA 19-20, 31-39, 40-48, 61-69, 70-737
SE 1, 11, 16-1738
SE 2, 6, 9, 12, 18-21, 25-2836
SE 1P, 3-5, 7-8, 10, 13-15, 22-24, 9937
SG5
SK1
SL33
SM 1, 3, 436
SM 2, 5-731
SN5
SO4
SP32
SR4
SS31
ST1
SW 1, 3, 5-7, 1028
SW 2, 4, 8-9, 11-20, 95, 9937
SY 1-6, 12-14, 992
SY 7-94
SY 10-11, 15, 16-257
TA 1-3, 6-205
TA 4-5, 21-246
TD 1-4, 6-7, 9-107
TD 5, 8, 11-158
TF1
TN 1-7, 9-12, 17-4031
TN 8, 13-166
TQ7
TR 1-97
TR 10-27, 938
TR 21** - 25** (Scilly Isles - mainland only onward shipping to Scilly Isles not inc' please call for more details)8
TS4
TW 1-8, 11-2032
TW 9-1031
UB33
W 1A, B, C, G, H, J, K, M, N, P, R, S U, V, W, X, Y, 2, 6, 8-1137
W 3-5, 7, 12-1331
W 1 D, F, T28
WA1
WC29
WD32
WF1
WN1
WR1
WS1
WV1
YO (except 7, 17-18, 21-22, 60-62)3
YO 7, 17-18, 21-22, 60-624
ZE * (Excludes islands of Yell, Unst, Fetlar, Foula, Fair, Isle, Virkie)20

The main difficulty that I am having is with all of the additional text included, and the postcodes with characters at the end rather than numbers.

In an ideal world, I would paste this into my sheet and power query would spit out something as per the below:

PostcodeZone
AB1
9​
AB10
9​
AB11
9​
AB12
9​
AB13
9​
AB14
9​
AB15
9​
AB16
9​
AB2
9​
AB21
9​
AB22
9​
AB23
9​
AB24
9​
AB25
9​
AB3
9​
AB30
9​
AB31
9​
AB32
9​
AB33
9​
AB34
9​
AB35
9​
AB36
9​
AB37
9​
AB38
9​
AB39
9​
AB4
9​
AB41
9​
AB42
9​
AB43
9​
AB44
9​
AB45
9​
AB5
9​
AB51
9​
AB52
9​
AB53
9​
AB54
9​
AB55
9​
AB56
9​
AB9
9​
AB99
9​
AL1
32​
AL10
32​
AL2
32​
AL3
32​
AL4
32​
AL5
32​

I dont think this is possible so think some manual intervention might be required, but struggling to do anything with the text elements.

Your suggestions so far have worked perfectly for the 'standard' postcode ranges.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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