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
lbarretta
Regular Visitor

Problem with a relationship many to many

Have 2 tables, the relationship between the is one to many.

Table1 is like this:

 

Level

facctor

1

1.06

2

0.13

3

40.92

2

1.06

 

Table2 is:

Level

Cost

1

30.96

1

12.25

2

13.25

1

50

3

48.02

 

The result I want is:

level

factor

cost

final cost

1

1.06

30.96

factor * cost

1

1.06

12.25

factor * cost

2

0.13

13.25

factor * cost

1

1.06

50

factor * cost

3

40.92

48.02

factor * cost

2

1.06

13.25

factor * cost

Becoming crazy!!!

1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi @lbarretta ,

 

Please try:

First, duplicate table1, then add a new column cost:

vjianbolimsft_0-1672635916938.png

Expanded the cost column:

vjianbolimsft_1-1672635965760.png

Then add a new column final cost:

vjianbolimsft_2-1672636014185.png

Here is the M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLUMzBTitWJVjICcgz0DI3BHGMgx8RAz9IILgVRFwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Level = _t, factor = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Level", Int64.Type}, {"factor", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "cost", each Table.SelectColumns(
Table.SelectRows(Table2, (x)=>x[Level]=[Level]),
"Cost")),
    #"Expanded cost" = Table.ExpandTableColumn(#"Added Custom", "cost", {"Cost"}, {"cost.Cost"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded cost", "final cost", each [factor]*[cost.Cost])
in
    #"Added Custom1"

Final output:

vjianbolimsft_3-1672636057278.png

Best Regards,

Jianbo Li

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

5 REPLIES 5
v-jianboli-msft
Community Support
Community Support

Hi @lbarretta ,

 

Please try:

First, duplicate table1, then add a new column cost:

vjianbolimsft_0-1672635916938.png

Expanded the cost column:

vjianbolimsft_1-1672635965760.png

Then add a new column final cost:

vjianbolimsft_2-1672636014185.png

Here is the M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLUMzBTitWJVjICcgz0DI3BHGMgx8RAz9IILgVRFwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Level = _t, factor = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Level", Int64.Type}, {"factor", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "cost", each Table.SelectColumns(
Table.SelectRows(Table2, (x)=>x[Level]=[Level]),
"Cost")),
    #"Expanded cost" = Table.ExpandTableColumn(#"Added Custom", "cost", {"Cost"}, {"cost.Cost"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded cost", "final cost", each [factor]*[cost.Cost])
in
    #"Added Custom1"

Final output:

vjianbolimsft_3-1672636057278.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Mikelytics
Resident Rockstar
Resident Rockstar

Hi @lbarretta 

 

I recommend you to do it in Power Query like this

 

Table 1

Mikelytics_0-1670318478245.png

Table2

Mikelytics_5-1670318709199.png

 

Execute a merge

Mikelytics_2-1670318561479.png

Mikelytics_3-1670318619568.png

 

Expand the cost column from table 2

Mikelytics_6-1670318788144.png

 

add the final cost column

Mikelytics_7-1670318839385.png

Result

Mikelytics_8-1670318857249.png

Best regards

Michael

-----------------------------------------------------

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your thumbs up!

@ me in replies or I'll lose your thread.

-----------------------------------------------------

LinkedIn

 

 

 

 

 

 

 

 

 

 

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

Hi Mike, sorry if i didn't answer before. what you did look interesting but i didn't realize what the first 2 steps do.

MAwwad
Super User
Super User

Hello,

 

Its not possible to have a relationship between two tables where the primary key between these two tables is duplicated.

You will have a duplication error. or Multiple tables where only 1 table expected message.

 

What you can do to make the Primary Key between the two tables as unique is to try to Concatenate the Primary key with any other key or identifier, this purpose is to make the PK unique or no valid relationship between the two tables can happen

Hi!

thanks for your prompt answer.

at lest I don't need to modify the structure of the relationship and what i need is that for every record in the second table with level 2 to show 2 records with a different computed final cost... 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.