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

Table Relatioship when there is Primary Key

I need to compare three tables dollar amounts. They do not have primary key since they come from spreadsheets and a DB table. I need to compared at a Customer ID, PO# level, Job Card and Manufacture Level. What I have done is created a primary key on each of the tables using the four categories. It works for the most part as long as all the fields are typed exactly the same on all three tables. If I use the Customer ID only it only gives me the total of all the items, first question, is there a better way around this. I am okay with using this method.

 

The main question I have is when I compare the data, by creating measurements to add to my graph. It is only providing the data that matches my left table. I have tried changing the relationships but it does not seem to work. If I have switched the tables around always the table to left is the one that provided the detail.

Thanks,

Table 1     
Customer IDManufacture#PO#Job#CostMy Manual Primary Key
N12345ABCP123Job134$ 10.00N12345-ABC-P123-Job134
N12345ABDP123Job134$ 15.00N12345-ABD-P123-Job134
N12345ABTP124Job254$     5.00N12345-ABT-P124-Job254
N123451BTP138Job256$ 25.00N12345-1BT-P138-Job256

 

Table 2     
Customer IDManufacture#PO#Job#CostMy Manual Primary Key
N12345ABCP123Job134$ 11.00N12345-ABC-P123-Job134
N12345ABDP123Job134$ 15.00N12345-ABD-P123-Job134
N12345ABTP124Job254$     5.00N12345-ABT-P124-Job254
N123451BT2P138Job256$ 23.00N12345-1BT2-P138-Job256

 

 

Table3     
Customer IDManufacture#PO#Job#CostMy Manual Primary Key
N12345ABCP123Job134$     10.00N12345-ABC-P123-Job134
N12345ABDP123Job134$     14.00N12345-ABD-P123-Job134
N12345ABTP124Job254$       5.00N12345-ABT-P124-Job254
N123451BT3P138Job256$     25.00N12345-1BT3-P138-Job256

 

Getting this:

     Table1Table2Table3
My Manual Primary KeyCustomer IDManufacture#PO#Job#CostCostCost
N12345-ABC-P123-Job134N12345ABCP123Job134$                                 10.00$     11.00$           10.00
N12345-ABD-P123-Job134N12345ABDP123Job134$                                 15.00$     15.00$           14.00
N12345-ABT-P124-Job254N12345ABTP124Job254$                                   5.00$       5.00$             5.00
N12345-1BT-P138-Job256N123451BTP138Job256$                                 25.00  

 

I am looking for something like this

My Manual Primary KeyCustomer IDManufacture#PO#Job#CostCostCost
N12345-ABC-P123-Job134N12345ABCP123Job134$                                 10.00$     11.00$           10.00
N12345-ABD-P123-Job134N12345ABDP123Job134$                                 15.00$     15.00$           14.00
N12345-ABT-P124-Job254N12345ABTP124Job254$                                   5.00$       5.00$             5.00
N12345-1BT-P138-Job256N123451BTP138Job256$                                 25.00  
N12345-1BT2-P138-Job256N123451BT2P138Job256$                                         -  $     23.00$               -  
N12345-1BT3-P138-Job256N123451BT3P138Job256$                                         -   $           25.00
1 ACCEPTED SOLUTION

@Anonymous 

 

You are getting the "amibguity" message when trying to create the relationships because the detail in the data generates "conflict" when setting up the independent slicers. (As I mentioned in my previous post, I actually added rows to the data you provided to avoid this in the example, based on your comment that the actual dataset would actually contain many more IDs etc...)

To see what I mean, if you look at the dataset, there is only 1 customer id and the "Manufacture#" has single corresponding PO values.

So depending on what the final dataset looks like, you can set up the model with a single lookup table in which you establish a single field as your "primary key" (though you have to be careful when removing duplicates to ensure you are not removing rows with disctinct values. As an example, if you removed duplicates from the customer id field, you would be left with a single row, which obviously doesn't reflect the nature of your data).

Sticking strictly to the dataset you provided, you could have a model set up like this (the unique values - remove duplicates - is on the "Manufacture#"):

2020-03-05.png

 

and get this result:

result.JPG

 

Having said all that, and taking into account that the difference between the tables is a timeframe (Initial Quote, before Billing, Final Cost), I would set up the model differently. Namely, I would add a column in each of your tables to identify the quote timeframe and then append the tables into a single table.

1) add a column to each table identifying the quote timeframe:

Add column.JPG

 2) append all three tables into a single table of data:

Append Tables.JPG

 3) add Dim tables as in the example above + a new dim table for timeframe. The model should look like this:

Model.JPG

 4) create a mesaure for the sum of Costs:

 

 

Cost = SUM('Data Table'[Cost])

 

 

 

5) And finally set up your matrix using the fields from the Dim tables (notice the "Quote Timeframe" in th column bucket)

Result.JPG

 

And here is the PBIX file for your reference:

PBIX file Append Tables 

 

Hope that helps!

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

7 REPLIES 7
rainer1
Resolver III
Resolver III

Hi @Anonymous,

 

you dont need a Primary Key in Power BI because you can choose every Column you want for the relation.

 

I recommend you build a star schema (for further information see: https://docs.microsoft.com/en-us/power-bi/guidance/star-schema) and create a "Fact Table" to achive your needings. 

 

Based on your Sample Data the a good Column to start is the manufacture column this can be your "Key".

 

So the "Fact Table" shoud look like this

 

Customer ID

Manufacture#PO#Job#

N12345

ABCP123Job134
N12345ABDP123Job134
N12345ABTP124Job254
N123451BT3P138Job256
N123451BT2P138Job256
N123451BTP138Job256

 

The Relation should identified by Power BI itself but should look like this

Anmerkung 2020-03-03 083122.png

 

At the end your visual is shown as you are looking for:

Anmerkung 2020-03-03 083211.png

 

-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

Anonymous
Not applicable

I am new to Power of BI how do i build a ID table, where it pulls all the data from all the tables as they will constantly change.  Also a note not sure that i can use the manufacture part#. In my sample i only used one Customer ID, I could have hundreds of different customer ID's and they could posibly use the same manufacture part #.

 

I am thinking that i need to append all four fields from each table to this new ID table. 

 

Thanks,

@Anonymous 

Just a question: what is the difference between each of your tables (1,2,3) since all three have the same structure? 

Depending on what the difference is (or why you need to keep them separate), you might have to keep them as separate entities or actually append them into a single table . This is a very relevant question and will determine what is the best way to set up your model.

 

And as @rainer1  suggests, you should create lookup or dim tables for the fields you wish to "filter" by.

To show how this is done, and since you did mention that your real data would have more than one ustomer ID for example, I have firstly taken the liberty of including a couple of extra rows (the data as provided was creating ambiguities when establishing the rleationships).

Once you have imported the tables into the model, you can reference each (right-click on each imported table), remove the columns with values ("costs")  and then append them into a single table. You can then reference this new table to create 4 new tables. In each of these 4 new tables tables keep only one of the columns (select and use the function "remove other columns") to create the Dim or lookup table for each column. Remove the duplicate rows in each of these tables.

Once you have loaded the tables to the model, establish a single to many relationship between each of your dim tables and the other fact tables (1,2 and 3) by joining the corresponding fields in each.

Your model should look like this:

Full Model (Ideal).JPG

 

Just to get a clearer view, here is the model showing just two of the 4 Dim tables linked to each fact table

Dim Tables (Ideal).JPG

 

Next create measures ofr the sum of each of the costs (one for each) in each fact table like this.

Cost Table 1 = SUM(Table1[Cost])

 

And finally create your visual using the fields from your Dim tables as rows and the measures as Values:

Result.JPG

 

and here is the PBIX file for your reference:

Example of Schema structure 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Thanks for the instructions. I builded the report as you suggested and was able to recreate each of the 4 tables.  The issue i ran in to is when i tried link the tables relationship. 

I am not able to do Many to  One / Single Cross Filter Direction.  Only Many to Many and I can do both or Single crosss filter direction,  but this only allows me to do it to one of the 3 tables. Once i added to the other ones i get a error saying that "CREATE RELATIONSHIP "There are ambiguous paths between.

I did see in your example that i downloaded you were able to do Many to One relationship.  Am I missing anything.  I did had other tables in my model which I removed all the relatioships only left the model to look like your example and still not able to do single relationship.

 

As to your earlier question.  The difference between the three tables is that one has the initial quote cost, the other one has the cost before it was billed, and the other the final cost.   What we trying to see is the cost between the three different stages and why they might have changed.  One of the detail comparison to is the Manufacture# maybe they added additional parts after the quote or they removed parts. They could also been charge to a different PO under the same Job number.

 

Thanks for your help!

@Anonymous 

 

You are getting the "amibguity" message when trying to create the relationships because the detail in the data generates "conflict" when setting up the independent slicers. (As I mentioned in my previous post, I actually added rows to the data you provided to avoid this in the example, based on your comment that the actual dataset would actually contain many more IDs etc...)

To see what I mean, if you look at the dataset, there is only 1 customer id and the "Manufacture#" has single corresponding PO values.

So depending on what the final dataset looks like, you can set up the model with a single lookup table in which you establish a single field as your "primary key" (though you have to be careful when removing duplicates to ensure you are not removing rows with disctinct values. As an example, if you removed duplicates from the customer id field, you would be left with a single row, which obviously doesn't reflect the nature of your data).

Sticking strictly to the dataset you provided, you could have a model set up like this (the unique values - remove duplicates - is on the "Manufacture#"):

2020-03-05.png

 

and get this result:

result.JPG

 

Having said all that, and taking into account that the difference between the tables is a timeframe (Initial Quote, before Billing, Final Cost), I would set up the model differently. Namely, I would add a column in each of your tables to identify the quote timeframe and then append the tables into a single table.

1) add a column to each table identifying the quote timeframe:

Add column.JPG

 2) append all three tables into a single table of data:

Append Tables.JPG

 3) add Dim tables as in the example above + a new dim table for timeframe. The model should look like this:

Model.JPG

 4) create a mesaure for the sum of Costs:

 

 

Cost = SUM('Data Table'[Cost])

 

 

 

5) And finally set up your matrix using the fields from the Dim tables (notice the "Quote Timeframe" in th column bucket)

Result.JPG

 

And here is the PBIX file for your reference:

PBIX file Append Tables 

 

Hope that helps!

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Thanks for the new input.  I was able to resolve the Many to one relationship, by formating each of the column to Upper Case.  I am not sure if this is a PBI issue.  i had previously had Trim and Clean the data prior to removing duplicates and it only worked for one of the tables.  After i changed the format to all be upper case. It allow me to do Many to one relationship.

I will try your new format as well.

 

Thanks again!

@Anonymous 

Thanks for getting back. 
That's weird: once in the model, PBI is theoretically "case"-agnostic I believe (not so Power Query).

ref: From Curbal: case sensitivity 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






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.