Forum Discussion

lyderic's avatar
lyderic
Regular Visitor
3 years ago
Solved

Power Query Editor: custom column based on another table

Hi,

I have two tables with the fields as follow:

TransactionTbl:

Material, transaction date, quantity, cost, plant, supplier.

SupplierTbl:

MaterialNbr, Preferred supplier, plant code, contract start date, contract end date.

 

In Power Query Editor, I'd like to add a Preferred Supplier field that is equal to the Preferred supplier field of the SupplierTbl, to the TransactionTbl, if the conditions below are met:

Material = MaterialNbr,

plant = plant code,

transaction date >= contract start date,

transaction date <= contract end date.

If not, then leave the filed blank.

 

Thank you all for your help!

14 Replies

  • Syk's avatar
    Syk
    Resident Rockstar

    Bring your supplier field into the transaction table with a merge. Select both the Material number and plant code as the keys to join, then with your newly merged column you can create a new calculated column to handle the transaction date logic.

    You can then delete your merged column and just have the calculated one.

    • lyderic's avatar
      lyderic
      Regular Visitor

      Thank you for your help!

       

      That's progress, but after merging, I'm getting a new column with "Table" instead of actual records. What should I do next?

      An important detail: in SupplierTbl, I may have multiple records with teh same MaterialNbr and PlantCode, but different contract start date and contract end date and different supplier. For example:

      Material NbrPreferred SupplierPlant codeContract start dateContract end date
      123456ABC CorpXYZ1/1/20234/30/2023
      123456DEF Inc.XYZ5/1/2023 
      987654GHI & CoWER2/1/2023 
      • Syk's avatar
        Syk
        Resident Rockstar

        Can you provide some sample data from both tables?

  • Hi,

    This will be easier to do with DAX as a calculated column.  Would you be OK with this approach?

    • lyderic's avatar
      lyderic
      Regular Visitor

      hi Ashish,

      for sure. I'd love to see your approach. Thank you!

  • Hi,

    Write this calculated column formula in the TransactionTbl

    =calculate(max(suppliertbl[preferred supplier]),filter(suppliertbl,suppliertbl[materialnbr]=earlier(TransactionTbl[material])&&suppliertbl[plant code]=earlier(TransactionTbl[plant])&&suppliertbl[contract start date]<=earlier(TransactionTbl[transaction date])&&suppliertbl[contract End date]>=earlier(TransactionTbl[transaction date])))

    Hope this helps.