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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Link two tables with a range in stead of one value

Hi,

 

I have this table SALES containing postal codes from Belgium

 

Sales.jpg

 

I need to make aggregation totals per province and per region.

To do so I have this POSTAL CODES table from which must be decides in which province and in which region a postal code resides.

 

Postal_Codes.jpg

 

 

e.g. Postal code 4715 lays between 4000 and 4999 and so it's in the province of Liège and in the Region of Wallonie

 

I need to link the Sales table to Postal Codes table where 'Sales'[Postal Code] lays between 'Postal Codes'[Postal Code From] and 'Postal Codes'[Postal Code To]

 

Sales-Postal_Code.jpg

 

How do I do this?

 

R.W.

1 ACCEPTED SOLUTION

With Power Query it is easy:

 

 

let

    // load of your table from your database, excel, csv, etc
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0MDAAUUaWlkDKqai0uDg1pxjE9HBXitWJVjICyRpDFJmAFYUn5uTk58EZmalgdcYgBaYQdZZgdWE5iYm5xVBGXkpqUWoeWKUJUMgIYq0RRKVjXkl5alFBah6mYlOgkDFEsbEJbmNjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Postal Code ID" = _t, #"Postal Code From" = _t, #"Postal Code To" = _t, Province = _t, Region = _t]),
    
    
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Postal Code ID", Int64.Type}, {"Postal Code From", Int64.Type}, {"Postal Code To", Int64.Type}, {"Province", type text}, {"Region", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "PostalCodePrimaryKey", each List.Numbers( [Postal Code From], [Postal Code To] - [Postal Code From] + 1 ), type list ),
    #"Expanded PostalCodePrimaryKey" = Table.ExpandListColumn(#"Added Custom", "PostalCodePrimaryKey"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded PostalCodePrimaryKey",{{"PostalCodePrimaryKey", Int64.Type}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"Postal Code ID", "Province", "Region", "PostalCodePrimaryKey"})
in
    #"Removed Other Columns"

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

View solution in original post

3 REPLIES 3
LivioLanzo
Solution Sage
Solution Sage

Hello @Anonymous

 

you need to make changes in your dimension table (postal codes) during the ETL process and repeat each row by the number of postal codes between START postdal code and END postal code, adding a column containing the sequencial postal code

which becomes the primary key that you shall use in the relationship

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Anonymous
Not applicable

Hi @LivioLanzo

 

Thanks for your reply.

How do I do this (repeat each row by the number of postal codes between Postal Code From and Postal Code To)?

 

Thanks,

 

R.W.

With Power Query it is easy:

 

 

let

    // load of your table from your database, excel, csv, etc
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0MDAAUUaWlkDKqai0uDg1pxjE9HBXitWJVjICyRpDFJmAFYUn5uTk58EZmalgdcYgBaYQdZZgdWE5iYm5xVBGXkpqUWoeWKUJUMgIYq0RRKVjXkl5alFBah6mYlOgkDFEsbEJbmNjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Postal Code ID" = _t, #"Postal Code From" = _t, #"Postal Code To" = _t, Province = _t, Region = _t]),
    
    
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Postal Code ID", Int64.Type}, {"Postal Code From", Int64.Type}, {"Postal Code To", Int64.Type}, {"Province", type text}, {"Region", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "PostalCodePrimaryKey", each List.Numbers( [Postal Code From], [Postal Code To] - [Postal Code From] + 1 ), type list ),
    #"Expanded PostalCodePrimaryKey" = Table.ExpandListColumn(#"Added Custom", "PostalCodePrimaryKey"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded PostalCodePrimaryKey",{{"PostalCodePrimaryKey", Int64.Type}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"Postal Code ID", "Province", "Region", "PostalCodePrimaryKey"})
in
    #"Removed Other Columns"

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.