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
Fab117
Helper IV
Helper IV

[Power BI] Copy a column content from one table to another one (linked through common column)

Hello,

I've 2 tables:

Table 1
CodeQuantity
A5
B3
A4
C6
B4
A2
C5
A3
B1
C4

 

Table 2
CodePrice
A1.00 $/unit
B1.20 $/unit
C1.40 $/unit

 

To simplify future calculation, I'd like to add in column one the corresponding price in each line

 

=> 

Table 1
CodeQuantityPrice
A51.00 $/unit
B31.20 $/unit
A41.00 $/unit
C61.40 $/unit
B41.20 $/unit
A21.00 $/unit
C51.40 $/unit
A31.00 $/unit
B11.20 $/unit
C41.40 $/unit

 

Would someone know the formulae to use in Power Query?

 

Thanks

 

Fab

 

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @Fab117 

This can be done easily in Power query with a merge. Place the following M code in a blank query to see the steps. See it all at work in the attached file.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJVitWJVnICsozBLJCYCZjlDGSZwWVN4LJGcFlTuJgxXJ0hXBaoIxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Code = _t, Quantity = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type text}, {"Quantity", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Code"}, Table2, {"Code"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Price"}, {"Price"})
in
    #"Expanded Table2"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

 

View solution in original post

4 REPLIES 4
AlB
Super User
Super User

Hi @Fab117 

If you want it in DAX, you can create a calculate column in Table1. See it all at work in the attached file.

Price per unit = 
LOOKUPVALUE(Table2[Price], Table2[Code], Table1[Code])

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

AlB
Super User
Super User

Hi @Fab117 

This can be done easily in Power query with a merge. Place the following M code in a blank query to see the steps. See it all at work in the attached file.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJVitWJVnICsozBLJCYCZjlDGSZwWVN4LJGcFlTuJgxXJ0hXBaoIxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Code = _t, Quantity = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type text}, {"Quantity", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Code"}, Table2, {"Code"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Price"}, {"Price"})
in
    #"Expanded Table2"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

 

Hi,

Thank you very much for looking at my issue and for your proposal.

I looked at the Merge option, this is for sure a good solution.

I'm still also assessing the possibility to do it in Power Desktop with the LookUpValue formulae.

Not sure which one is the most appropriate in my case.

 

Happy new year

 

Fab

Fab117
Helper IV
Helper IV

Waiting to see if it was possible to do this in Power Query, I found a possibility to do it in Power Desktop in Data mode:

Methology applied

 

Target: "NetReg_file"

Source: "Prices"

In Data Mode in Power Desktop (not Power Query)

Fab117_0-1671813776562.png

 

Select "New column"

Fab117_1-1671813776571.png

 

Then type formulae

Name new column= LOOKUPVALUE(Source Table[Field to copy], Source Table [Common field],Target Table[Common field])

For me:

SP = LOOKUPVALUE(Prices[Standard price (GBP)],Prices[GMM],NetReg_file[GMM])

 

As I've 2 different prices to considere, I did excatly the same with second one:

NP = lookupvalue(Prices[Net Price (GBP)],Prices[GMM],NetReg_file[GMM])
 
But faced an unexpected issue:
Power BI issue.png
 

I'm not sure if I'm doing something wrong or if it's a bug.

Did someone see what is going wrong?

 

Tks

 

Fab

 

 

 

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors