Setting up region and country tables

Hello DNA community!

I am struggling with something so basic…but so important to my reporting. In my Orders table and my Shipped table, we have a column for Country and Region.

I am simply trying to create a dimension table(s) for Country and Region. Then relate those to my Orders table and Shipping table so that I can apply a “global” filter to on my report that will filter Country and/or Region at the page level.

Been working in the query editor to try and figure this out but keep getting many to many errors etc. Any advice how to set this up would be much appreciated!

Hello @richmont,

Thank you for posting your query onto the Forum.

You can follow the steps given below in order to avoid “Many-to-Many” relationships that you’re facing.

Step 1: Create a “Duplicate table” from your Order Table.

Step 2: From the duplicate generated table remove all other columns and only keep “Regions” and “Country” column.

Step 3: Right - click on the Regions column and select the “Remove Duplicates” options. And now you’ll be only left with the unique value list of Regions and Country.

Step 4: From your Order Table as well as Shipping table remove the “Country” column from both the tables. This step is “Optional”. I’ve just recommended this step just to reduce your data size from both these tables.

Step 5: Click on the “Close & Apply” button.

Step 6: Under the Modelling Tab view, now try your Order Table and Shipping Table to get connected with the newly created Locations table.

Please Note: In your Order Table and Shipping Table you should’ve same number of “Regions” and “Countries”. If there’s any error in designing your data for these table then get it sorted first.

Hoping you find this useful and helps you to avoid “Many-to-Many” relationship. :slightly_smiling_face:

Thanks & Warm Regards,
Harsh

Hi @richmont,

You can work with stage tables, referencing them and create index for the columns you need.

The main point here is to create a distinct table using columns from both tables and create an index to reference it later on them:

let
Source =
Table.Distinct(
Table.Combine(
{
Table.SelectColumns(Orders_Stage, {“Country”, “Region”}),
Table.SelectColumns(Shipped_Stage, {“Country”, “Region”})
}
)
),
#“Added Index” = Table.AddIndexColumn(Source, “Index”, 1
, 1),
#“Renamed Columns” = Table.RenameColumns(#“Added Index”,{{“Index”, “CD_COUNTRY”}})
in
#“Renamed Columns”

Check this example:
richmont.pbix (53.4 KB)

Hi Harsh - Thank you. This might be workable, however, we have another table (our CRM table) that definitely will include countries that have not shown up on the Orders table. For example, opportunities in our pipeline in Sweden that have not turned into Orders. So if I use the Orders table as my base table, it will miss Sweden.

Ideally I’d like a solution that allowed me to use a Country/Region table that I already have that list all of the regions and countries of the world.

Hello @richmont,

So you can do one thing. You can apply this same steps on the Shipping Table as well and then Append this two tables and convert them into one. By this way you’ll have “Regions” and “Countries” from both these tables.

Thanks & Warm Regards,
Harsh

Hi Harsh - If I remove “country” from my orders and shipments table (and CRM), how will I be able to relate them to my Locations table?

Hello @richmont,

Since you’ll still have Regions in all of your table and relationship will be created based on the “Regions” column so then you’ll still be able to analyze your Countries. And as I said this step is simply optional.

Thanks & Warm Regards,
Harsh

This looks very interesting and I think it will work. Thanks!

Thanks Harsh - I believe your solution will work as well…thank you!

Hi Harsh - Another question.

As you can imagine, a table with Regions and Countries is going to have many duplicates in the Region column. And of course the Orders table and Shipments table will also. This was my original problem really…which is how to construct the tables and data model based on this scenario. The Regions field can of course be unique…but as soon as you add Countries to that table there are many countries part of one region, so you are back to a many to many problem when trying to relate to the Orders table or Shipments table.

@richmont when I run across this type of issue in my functions, I create a hybrid column.

Example, in my primary data set, I have “Customer Number” and “Ship-To Number” - each customer can have multiple Ship-Tos in my data, and the Ship-To numbers can be duplicated (in fact in many of my customers, they have Ship-Tos 001, 002, & 003).

And the invoice data is related to both the Customer and the Ship-To (and yes, I have a need to report to the Ship-To level).

So, my tables look like this:

I have taken the Customer Number and Ship-To number and created a new column (in the Query Editor) that combines the two with a unique identifier (in my case I use double colons :: ). This is done on both the Invoice and the Ship-To tables, and the column is only used to create a unique key to join the tables.

2 Likes

Hi @richmont,

I’m kinda wondering if this might be easier. I did a google search " Region and countries in the world"

this is the link that have all 197 countries with Region.

You could load into excel and query into power bi or maybe there is a way to do it directly from the web. This way you have all countries in world in one table.

I know you have some solutions that @Heather, @Harsh and @ricardocamargos88 has given.

I just thought, this might be a option for you too.

Thanks
Keith

1 Like