Forum Discussion
kyliemcpoland
2 years agoNew Member
Zip code to place
We have data that contains the 5 digit zip codes. Is there a way to transform these numbers into the name of the city.
- 2 years ago
Yes, you'll need to create a table that contains the zip codes and the corresponding city names. Then you can create a relationship between that table and your main data table, and write any measures/create visuals you need.
Phil
dufoq3
2 years agoCommunity Champion
Hi kyliemcpoland, if you find web page with your_country zip codes - you can do something like this. This is for Slovakia for example:
The code is not so simple, because I had to use Fn_Unzip (the reason is that I've found Slovakian zip codes only zipped version available online).
let
Fn_Unzip =
(ZIPFile) =>
let
Header = BinaryFormat.Record([
MiscHeader = BinaryFormat.Binary(14),
BinarySize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
FileSize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
FileNameLen= BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
ExtrasLen = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian)
]),
HeaderChoice = BinaryFormat.Choice(
BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
each if _ <> 67324752 // not the IsValid number? then return a dummy formatter
then BinaryFormat.Record([IsValid = false, Filename=null, Content=null])
else BinaryFormat.Choice(
BinaryFormat.Binary(26), // Header payload - 14+4+4+2+2
each BinaryFormat.Record([
IsValid = true,
Filename = BinaryFormat.Text(Header(_)[FileNameLen]),
Extras = BinaryFormat.Text(Header(_)[ExtrasLen]),
Content = BinaryFormat.Transform(
BinaryFormat.Binary(Header(_)[BinarySize]),
(x) => try Binary.Buffer(Binary.Decompress(x, Compression.Deflate)) otherwise null
)
]),
type binary // enable streaming
)
),
ZipFormat = BinaryFormat.List(HeaderChoice, each _[IsValid] = true),
Entries = List.Transform(
List.RemoveLastN( ZipFormat(ZIPFile), 1),
(e) => [FileName = e[Filename], Content = e[Content] ]
)
in
Table.FromRecords(Entries),
SourceWebZipCodes = Web.Contents("https://www.posta.sk/subory/322/psc-obci-a-ulic.zip"),
Unzipped = Fn_Unzip(SourceWebZipCodes),
FilteredRows = Table.SelectRows(Unzipped, each ([FileName] = "POBoxy.xlsx")),
Content = Excel.Workbook(FilteredRows{0}[Content], true),
ZipCodesTable = Content{[Item="PSČ_POBOXov!_xlnm._FilterDatabase",Kind="DefinedName"]}[Data],
RenamedColumns1 = Table.RenameColumns(ZipCodesTable,{{"PSČ pre P.O.BOXy", "Zip Code"}}),
ChangedType = Table.TransformColumnTypes(RenamedColumns1,{{"Zip Code", Int64.Type}}),
// Table with zip codes I want to transoform to city name.
SourceZipCodesToDecode = #table(type table[ZIP=Int64.Type], {{96261},{96001},{96233},{90055}}),
MergedQueries = Table.NestedJoin(SourceZipCodesToDecode, {"ZIP"}, ChangedType, {"Zip Code"}, "ZipCodesWeb", JoinKind.LeftOuter),
ExpandedZipCodesWeb = Table.ExpandTableColumn(MergedQueries, "ZipCodesWeb", {"Názov pošty"}, {"Názov pošty"}),
RenamedColumns2 = Table.RenameColumns(ExpandedZipCodesWeb,{{"Názov pošty", "City"}}),
RemoveSpecialCharsAndNumbers = Table.TransformColumns(RenamedColumns2, {{"City", each Text.FromBinary(Text.ToBinary(Text.Remove(_, {"0".."9"}), 1251), TextEncoding.Ascii), type text}})
in
RemoveSpecialCharsAndNumbers