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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Generate a list of values from a range of poscal codes

Hi,

 

I have 2 tables that I need to combine (examples of the tables are below).

- Table A contains the company name and the target area based on postal code and described as ranges of postal codes.
- Table B contains the name of the township and the accompanying postcal code.

 

What I need to do is that in table A I need to transform the range of postcal codes to individual lines of a single postal code. And of course the result could be that I get 15 lines of company A because the range consist of 15 postcal codes.

After this is done I can connect the company name with the name of the township based on postal code. And the result of this should be that per company I get a list of distinct townships (that is because large townships consists of more than 1 postal code).

I should probably use one of the M list functions to transform my range of postal codes to a number of columns per company with an individual postal code. But I don't seem to get it working.

 

Desired end result 
  
Company nameTownship name
AL;G;Z;W
BD
CA;C
DB;T;U
EO
FP

 

Table A  Table B 
     
Company namePostal code Township namePostal code
A1000-1400;1201-1220;1250-1271 D1201
B1453-1469 D1202
C1701-1720;1721-1743 D1203
   D1204
   E1468
   F1701
   F1702
   F1703
   F1704
   F1705
1 ACCEPTED SOLUTION
v-danhe-msft
Employee
Employee

Hi @Anonymous ,

You could refer to below code in M:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HcrBDYAwDATBXu6dSD7HwUK8gDKs9N8GOX6r1VThRgPNrDPMLrqx01019/MkVis8UjHHVsf5n1cnpVM6XRUDa30=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Company name" = _t, #"Postal code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Company name", type text}, {"Postal code", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Postal code", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Postal code.1", "Postal code.2", "Postal code.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Postal code.1", type text}, {"Postal code.2", type text}, {"Postal code.3", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Company name"}, "Attribute", "Value"),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Unpivoted Columns", "Value", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Value.1", "Value.2"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Value.1", Int64.Type}, {"Value.2", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type2", "Custom", each List.Numbers([Value.1],[Value.2]-[Value.1]+1)),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Attribute", "Value.1", "Value.2"})
in
    #"Removed Columns"

Rsult:

1.PNG

You could also download the pbix file to have a view.

 

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.

View solution in original post

4 REPLIES 4
v-danhe-msft
Employee
Employee

Hi @Anonymous ,

You could refer to below code in M:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HcrBDYAwDATBXu6dSD7HwUK8gDKs9N8GOX6r1VThRgPNrDPMLrqx01019/MkVis8UjHHVsf5n1cnpVM6XRUDa30=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Company name" = _t, #"Postal code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Company name", type text}, {"Postal code", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Postal code", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Postal code.1", "Postal code.2", "Postal code.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Postal code.1", type text}, {"Postal code.2", type text}, {"Postal code.3", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Company name"}, "Attribute", "Value"),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Unpivoted Columns", "Value", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Value.1", "Value.2"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Value.1", Int64.Type}, {"Value.2", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type2", "Custom", each List.Numbers([Value.1],[Value.2]-[Value.1]+1)),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Attribute", "Value.1", "Value.2"})
in
    #"Removed Columns"

Rsult:

1.PNG

You could also download the pbix file to have a view.

 

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.
Anonymous
Not applicable

Hi @v-danhe-msft ,

 

Thx for the list function!

I still got 2 questions.

 

1. Why do you use each?

2. Some companies in my table have multiple ranges. Each time I split a column by - I get columns like "Value1.1", "Value1.2", "Value2.1", "Value2.2", "Value3.1" etc.  Is is possible for the list function to work on all of those columns instead of the 1th 2 as it does now?

Anonymous
Not applicable

The problem is that e.g. 1 company has 46 postal code ranges.
I can split al those columns with the split column option in the query editor (apparently you can only do it for 1 column at a time). This will create 92 columns in total with seperate postal codes in my model.

But then I have the problem that I need to apply the function you provided to all 92 columns at once. And I have no idea how...

Anonymous
Not applicable

I should split the columns not to columns but to rows. 

Than the above mentioned M formula works.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.