Forum Discussion

kyliemcpoland's avatar
kyliemcpoland
New Member
2 years ago
Solved

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. 

  • Hi kyliemcpoland 

     

    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

5 Replies

  • Hi kyliemcpoland 

     

    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

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi kyliemcpoland ,

    Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or Accept the helpful suggestions to help others who faced similar requirements.

    If these also don't help, please share more detailed information and description to help us clarify your scenario to test.

    How to Get Your Question Answered Quickly 

    Regards,

    Xiaoxin Sheng

    • PhilipTreacy's avatar
      PhilipTreacy
      Super User

      Anonymous Anonymous 

       

      How is this accepted answer actually an answer?  Surely my response is better, and a solution.

       

      Phil

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi  PhilipTreacy ,

         

        I am sorry, I did make an error mark. I had intended to accept your response as the solution. I will be more careful next time. Thank you for your reminder. Have a good day.

         

        Best Regards,

        Liu Yang

  • dufoq3's avatar
    dufoq3
    Community 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