Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago

How to get value from another table based on multiple criteria

Hello,

 

I'm in stuck with my problem. Let's say I have 2 tables: Quotations and Shipments:

 

Quotations table:

BranchDepartmentQuoteIDLocal ClientSrvLevelOriginDestinationStart DateExpiry Date
BR2241QMT200002851Customer1LIFNLRTMDEBRV2018.06.022018.06.30
BR2241QMT200002852Customer2FILNLRTMDEBRV2018.06.022018.06.15
BR2241QMT200002853Customer4LILKRPUSDEBRV2018.06.022018.06.30
BR2241QMT200002854Customer5LIDKRPUSDEBRV2018.06.022018.06.30
BR2241QMT200002855Customer7LIFDEHAMDEBRV2018.06.022018.06.30
BR2241QMT200002856Customer8LIFDEHAMDEBRV2018.06.022018.06.30
BR2241QMT200002857Customer9LIDCNSHADEBRV2018.06.032018.06.15
BR2241QMT200002858Customer10LIFDEHAMDEBRV2018.06.012018.06.30

 

Shipments table:

 

BranchDepartmentShipment IDLocal ClientSrv LevelOriginDestinationDepartureQuoteID
BR2241SHP2800120Customer1LILNLRTMDEBRV2018.05.29 
BR2241SHP2800129Customer10LIFDEHAMDEBRV2018.06.06 
BR2241SHP2800130Customer10LIFDEHAMDEBRV2018.06.24 
BR2241SHP2800121Customer2LIFNLRTMDEBRV2018.06.10 
BR2241SHP2800122Customer3LIFDEHAMDEBRV2018.06.10 
BR2241SHP2800123Customer4LILKRPUSDEBRV2018.04.30 
BR2241SHP2800124Customer5LIFKRPUSDEBRV2018.06.16 
BR2241SHP2800125Customer6LIFCNSHADEBRV2018.06.16 
BR2241SHP2800126Customer7LIFDEHAMDEBRV2018.06.16 
BR2241SHP2800127Customer8LIFDEHAMDEBRV2018.06.03 
BR2241SHP2800131Customer8LIFDEHAMDEBRV2018.06.20 
BR2241SHP2800128Customer9LILCNSHADEBRV2018.06.15 

 

My goal is to transfer QuoteID from Quotations table into Shipments table if such conditions are met:

 

Branch=Branch

Department=Department

LocalClient=LocalClient

SrvLevel=SrvLevel

Origin=Origin

Destination=Destination

Departure is between StartDate and ExpiryDate

 

My expected result in Shipments table should look like this:

 

BranchDepartmentShipment IDLocal ClientService LevelOriginDestinationDepartureQuoteID
BR2241SHP2800120Customer1LILNLRTMDEBRV2018.05.29 
BR2241SHP2800129Customer10LIFDEHAMDEBRV2018.06.06QMT200002858
BR2241SHP2800130Customer10LIFDEHAMDEBRV2018.06.24QMT200002858
BR2241SHP2800121Customer2LIFNLRTMDEBRV2018.06.10 
BR2241SHP2800122Customer3LIFDEHAMDEBRV2018.06.10 
BR2241SHP2800123Customer4LILKRPUSDEBRV2018.04.30QMT200002853
BR2241SHP2800124Customer5LIFKRPUSDEBRV2018.06.16 
BR2241SHP2800125Customer6LIFCNSHADEBRV2018.06.16 
BR2241SHP2800126Customer7LIFDEHAMDEBRV2018.06.16QMT200002855
BR2241SHP2800127Customer8LIFDEHAMDEBRV2018.06.03QMT200002856
BR2241SHP2800131Customer8LIFDEHAMDEBRV2018.06.20QMT200002856
BR2241SHP2800128Customer9LILCNSHADEBRV2018.06.15 

 

 

Thanks in advance!

 

Tomas

 

 

6 Replies

  • ChrisMendoza's avatar
    ChrisMendoza
    Resident Rockstar

    Anonymous,

     

    I am unsure about your expected result. When I use the following, I get the below results:

     

    QuoteID1 = 
    IF (
        AND (
            Shipments[Departure] >= RELATED ( Quotations[Start Date] ),
            Shipments[Departure] <= RELATED ( Quotations[Expiry Date] )
        ),
        LOOKUPVALUE (
            Quotations[QuoteID],
            Quotations[Branch], Shipments[Branch],
            Quotations[Department], Shipments[Department],
            Quotations[Local Client], Shipments[Local Client],
            Quotations[SrvLevel], Shipments[Srv Level],
            Quotations[Origin], Shipments[Origin],
            Quotations[Destination], Shipments[Destination]
        )
    )
    QuoteID2 = 
    LOOKUPVALUE (
            Quotations[QuoteID],
            Quotations[Branch], Shipments[Branch],
            Quotations[Department], Shipments[Department],
            Quotations[Local Client], Shipments[Local Client],
            Quotations[SrvLevel], Shipments[Srv Level],
            Quotations[Origin], Shipments[Origin],
            Quotations[Destination], Shipments[Destination]
    )

     

    I am also unsure of performance as well.

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello ChrisMendoza,

       

      Trying to follow your guideline but below error occurs:

       

       

      what am I doing wrong?

       

      Thanks, Tomas

      • ChrisMendoza's avatar
        ChrisMendoza
        Resident Rockstar

        Anonymous, 

         

        Seems like you do not have a relationship between the two tables. When I loaded your sample tables, Power BI established the relationship as the below for me:

         

         

        Is your's not similar?

         

        For Reference RELATED ( ) 

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello


    I created following columns 

     

    In Quotations table -

    1. Concat = Quotations[Branch] & Quotations[Department] & Quotations[Local Client] & Quotations[SrvLevel] & Quotations[Origin] & Quotations[Destination]

     

    In Shipments table -

    1. Concat = Shipments[Branch] & Shipments[Department] & Shipments[Local Client] & Shipments[Srv Level] & Shipments[Origin] & Shipments[Destination]

     

    2. QuoteID =
    IF(ISBLANK(LOOKUPVALUE(Quotations[Branch],Quotations[Concat],Shipments[Concat])),"",
    CALCULATE(VALUES(Quotations[QuoteID]),
    FILTER(Quotations,Shipments[Departure]>Quotations[Start Date] && Shipments[Departure] < Quotations[Expiry Date]),FILTER(Quotations,Quotations[Concat]=Shipments[Concat]) )
    )

     

    You might have to play with QuoteID field a little to replace "greater than" or "less than" with appropriate "greater than or equal to" or "less than or equal to"

     

    It gives me correct results otherwise -

     

    QuoteIDShipment IDOriginLocal ClientDestinationBranch
     SHP2800123KRPUSCustomer4DEBRVBR2
     SHP2800120NLRTMCustomer1DEBRVBR2
     SHP2800121NLRTMCustomer2DEBRVBR2
     SHP2800122DEHAMCustomer3DEBRVBR2
     SHP2800124KRPUSCustomer5DEBRVBR2
     SHP2800125CNSHACustomer6DEBRVBR2
     SHP2800128CNSHACustomer9DEBRVBR2
    QMT200002855SHP2800126DEHAMCustomer7DEBRVBR2
    QMT200002856SHP2800127DEHAMCustomer8DEBRVBR2
    QMT200002856SHP2800131DEHAMCustomer8DEBRVBR2
    QMT200002858SHP2800129DEHAMCustomer10DEBRVBR2
    QMT200002858SHP2800130DEHAMCustomer10DEBRVBR2

     

     

    Regards

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello Anonymous,

       

      I've worked with your formula but still can't receive expected results. Please take a look at my file in below link and let me know what I am doing wrong.

       

      Shipments vs Quotes

       

      many thanks in advance!