Forum Discussion

VHosamane's avatar
VHosamane
Frequent Visitor
7 years ago
Solved

Match multiple columns and find corresponding values using DAX

Here is the scenario that I have and need help with DAX calculated field syntax to arrive with the two fields in the Transaction-Table.

 

I have a lookup table as shown, which has distinct rows with combination of ClientName and ProjectName.

LookUp-Table

ClientNameProjectNameProjectTypeProduct
Client1Project1TechSAAS
Client2Project2ServiceIT
Client1Project2TechR&D
Client2Project1ServiceIT
Client3Project3BothSAAS
Client3Project1ServiceR&D

 

 

Transaction-Table

Client

Name

Project

Name

Project

Cost

Emp

Name

LookUp

EmpType

Timesheet

Date

Input

Hr

LookUp

EmpRate

Calc

EmpCost

Calc

Product

Calc

ProjectType

Client1Project1$9,999,999EmpAEmp2-Jan4$100$400SAASTech
Client1Project1$9,999,999EmpBContract2-Jan4$200$800SAASTech
Client1Project1$9,999,999EmpCEmp2-Jan4$100$400SAASTech
Client1Project1$9,999,999EmpDEmp2-Jan4$100$400SAASTech
Client1Project2$1,000,000EmpAEmp9-Feb8$100$800R&DTech
Client1Project2$1,000,000EmpBContract9-Feb8$200$1,200R&DTech
Client2Project1$5,000,000EmpMEmp2-Jan8$100$800ITService
Client2Project1$5,000,000EmpMEmp12-Jan8$100$800ITService
Client4Project1$7,500,000Emp AEmp10-Apr8$100$800N/AN/A
Client1Project7$100,000EmpAEmp2-Feb8$100$800N/A

N/A

          

 

 

Based on ClientName and Project name in Transaction-Table, need to look up ProjectType & Product in the Look-Up table and get the value into the Transaction-Table, Calc-Product & Calc-ProjectType respectively. If the combination of ClientName and ProjectName does not exist, then enter N/A in both the columns.

 

Can you please help with the DAX calculated field syntax in the Transaction-Table? Would it be possible without joining/ linking the two tables?

  • Use Power Query, Merge the queries on ClientName and ProjectName using a LEFT join (with the transaction table first).

    Expand the 'Table' column to return Product and Project Type.

    Replace any null value with N/A

7 Replies

  • HotChilli's avatar
    HotChilli
    Community Champion

    Use Power Query, Merge the queries on ClientName and ProjectName using a LEFT join (with the transaction table first).

    Expand the 'Table' column to return Product and Project Type.

    Replace any null value with N/A

    • calerof's avatar
      calerof
      Impactful Individual

      Hi HotChilli ,

      Thank you very much for this response, I was not aware that it was possible to do nested joints, i.e. merge two tables with more than one key columns. 

      Great,

      happy learning!

      Fernando

  • Anonymous's avatar
    Anonymous
    Not applicable

    Yes, it is possible but I'd do it in Power Query as suggested by HotChilli. If your transaction table is really big, then creating calculated columns like this is a NO-NO.

     

    But one of the columns would be (I don't recommend doing it this way, TT - transaction table, LT - lookup table):

    Calc-Product =
    var __client = TT[Client Name]
    var __proj = TT[Project Name]
    var __product = 
    	CALCULATE(
    		VALUES( LT[Product] ),
    		TREATAS(
    			{__client, __proj},
    			LT[Client Name],
    			LT[Project Name]
    		),
    	)
    return
    	if( __product = blank(), "N/A", __product )

    If this expression errors out somewhere, it means that VALUES( LT[Product] ) returns more than 1 value and you have a problem with duplication in the LT. Bear in mind that this will work OK when there are NO RELATIONSHIPS between the two tables. If there are, then you should use the RELATED function.

     

    Best

    Darek

    • VHosamane's avatar
      VHosamane
      Frequent Visitor

      Hi Darek,

       

      Calc-Product = 
      var __client = TT[Client_Name]
      var __proj = TT[Project_Name]
      var __product =
          CALCULATE(
                  VALUES(LT[Product]),
                  TREATAS(
                      {__client, __proj},
                      LT[ClientName],
                      LT[ProjectName]
                      ),
                  )
      return
          IF(__product = BLANK(), "N/A", __product)
      I tried this code, but gets this error: Argument '3' in CALCULATE function is required.
    • VHosamane's avatar
      VHosamane
      Frequent Visitor

      What's wrong in the calculated formula syntax?

      • Anonymous's avatar
        Anonymous
        Not applicable

        Remove the comma from before the parenthesis that ends CALCULATE.

         

        Best

        Darek