Forum Discussion

Fab117's avatar
Fab117
Icon for Helper IV rankHelper IV
3 years ago
Solved

[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

 

  • 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"

     

    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.

     

     

4 Replies

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    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"

     

    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.

     

     

    • Fab117's avatar
      Fab117
      Icon for Helper IV rankHelper IV

      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

  • 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)

     

    Select "New column"

     

    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:
     

    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

     

     

     

     

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    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])

    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.