Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Power BI Query Editor: How to merge two tables with a condition (Date)

Dear all,

 

I am trying to perform a merge between two tables in Power BI but I need to use conditional merging in one way or another.

 

Can someone help me with it?

 

I have prepared dummy data to show what is required in this scenario:

 

Table 1:

Export date        ÍD       Company       Product     QTY

01/08/20211PhilipsLamps50
02/08/20212DellLaptop10
03/08/20213HoneywellHD150
04/08/20214BenzA-Class2
01/08/20215BMW2-Serie2
01/07/20211PhilipsLamps60
02/07/20212DellLaptop52
03/07/20213HoneywellHD100
04/07/20214BenzA-Class5
01/07/20215BMW2-Serie58
01/06/20211PhilipsLamps45
02/06/20212DellLaptop30
03/06/20213HoneywellHD22
04/06/20214BenzA-Class455
01/06/20215BMW2-Serie8
01/05/20211PhilipsLamps654
02/05/20212DellLaptop622
03/05/20213HoneywellHD5
04/05/20214BenzA-Class24
01/05/20215BMW2-Serie12
01/04/20211PhilipsLamps59
02/04/20212DellLaptop24
03/04/20213HoneywellHD58
04/04/20214BenzA-Class99
01/04/20215BMW2-Serie321

 

Table2:

Export date    ID      Company   Product   Unit Price

01/08/20211PhilipsLamps €                     1,5
01/08/20212DellLaptop €             1.500,0
01/08/20213HoneywellHD €                100,0
01/08/20214BenzA-Class €          40.000,0
01/08/20215BMW2-Serie €          65.000,0
01/07/20211PhilipsLamps €                     3,0
01/07/20212DellLaptop €             1.000,0
01/07/20213HoneywellHD €                120,0
01/07/20214BenzA-Class €          35.000,0
01/07/20215BMW2-Serie €          80.000,0
01/06/20211PhilipsLamps €                     5,0
01/06/20212DellLaptop €             3.000,0
01/06/20213HoneywellHD €                   50,0
01/06/20214BenzA-Class €          75.000,0
01/06/20215BMW2-Serie €        100.000,0

 

I need to merge based on ID to find the right unit price. but:

For all ID's with export date (Table2) <= 1-6-2021 find unit price with export date (Table1) = 1-6-2021, 

else, find a unit price where export date  month/Year (Table2) =  export date month/year (Table1)

 

I hope this is clear 🙂

 

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Anonymous 

     

    Is it something you are looking for? I changed the Table names as TableA, TableB. TableB has the Unit Price, TableA to find the price

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdNBC4IwFAfw7+LZcG7vTT1WHjwUBB06iIcOgwQrySDq07dZbgvH6/CYsL/u/XizriOWJixPOONpFEemdqe2a/tBP22O53FFFjWxTnIvyXWVquvGWH+/9ubtb054OaGrul7U8/EJV6UJTl8ELwm6Vury0styse6OwzAeM+b8HtHktgezudirW6t+YhlNkY6SURTklpLRFOYoGUnBeY8hCuY2J2kLoLVIyiLcWCRp4dxSJEkBxHmTIYyz4J+5IFgMUhjJ3WSQ1KDFIH3FYN5kyJK6SwZ//pfCWoCyTCcLLxem5NYCpKUo5j2GLELvNM0b", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Export date" = _t, ÍD = _t, Company = _t, Product = _t, QTY = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Export date", type date}, {"ÍD", Int64.Type}, {"Company", type text}, {"Product", type text}, {"QTY", Int64.Type}}),
        AddTableB = Table.AddColumn( #"Changed Type","TableB", (OT)=> Table.SelectRows(TableB, each [ÍD]=OT[ÍD] and [Company]=OT[Company] and [Product]=OT[Product])),
        #"Added Custom" = Table.AddColumn(AddTableB, "Unit Price", (OT)=> if OT[Export date] <= #date(2021,6,1) 
    then Table.SelectRows(OT[TableB],each [Export date]=#date(2021,6,1))[Unit Price]{0}? 
    else Table.SelectRows(OT[TableB],each [Export date]=Date.StartOfMonth( OT[Export date]))[Unit Price]{0}?),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"TableB"})
    in
        #"Removed Columns"

     

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

     

    Is it something you are looking for? I changed the Table names as TableA, TableB. TableB has the Unit Price, TableA to find the price

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdNBC4IwFAfw7+LZcG7vTT1WHjwUBB06iIcOgwQrySDq07dZbgvH6/CYsL/u/XizriOWJixPOONpFEemdqe2a/tBP22O53FFFjWxTnIvyXWVquvGWH+/9ubtb054OaGrul7U8/EJV6UJTl8ELwm6Vury0styse6OwzAeM+b8HtHktgezudirW6t+YhlNkY6SURTklpLRFOYoGUnBeY8hCuY2J2kLoLVIyiLcWCRp4dxSJEkBxHmTIYyz4J+5IFgMUhjJ3WSQ1KDFIH3FYN5kyJK6SwZ//pfCWoCyTCcLLxem5NYCpKUo5j2GLELvNM0b", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Export date" = _t, ÍD = _t, Company = _t, Product = _t, QTY = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Export date", type date}, {"ÍD", Int64.Type}, {"Company", type text}, {"Product", type text}, {"QTY", Int64.Type}}),
        AddTableB = Table.AddColumn( #"Changed Type","TableB", (OT)=> Table.SelectRows(TableB, each [ÍD]=OT[ÍD] and [Company]=OT[Company] and [Product]=OT[Product])),
        #"Added Custom" = Table.AddColumn(AddTableB, "Unit Price", (OT)=> if OT[Export date] <= #date(2021,6,1) 
    then Table.SelectRows(OT[TableB],each [Export date]=#date(2021,6,1))[Unit Price]{0}? 
    else Table.SelectRows(OT[TableB],each [Export date]=Date.StartOfMonth( OT[Export date]))[Unit Price]{0}?),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"TableB"})
    in
        #"Removed Columns"