Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
andrewpirie
Resolver II
Resolver II

Joining to lookup table where join is conditional on two columns

I'm trying to add the name of the employee who created an entry in our Sales Order dataset in PowerBI. The relevant columns are as below:

SALESORDER
...
SALESPERSON_ID VARCHAR(20)

EMPLOYEE
OLD_FORMAT_EMPLOYEE_ID VARCHAR(20)
NEW_FORMAT_EMPLOYEE_ID INT
DISPLAYNAME VARCHAR(200)
...

In SQL I would do this by the below:

 

SELECT SALESORDER..., EMPLOYEES.DISPLAYNAME
FROM SALESORDER
   LEFT JOIN EMPLOYEE
      ON SALESORDER.SALESPERSON_ID = CAST(EMPLOYEE.NEW_FORMAT_EMPLOYEE_ID AS VARCHAR(20))
         OR SALESORDER.SALESPERSON_ID = EMPLOYEE.OLD_FORMAT_EMPLOYEE_ID


To do this in PowerBI I could either use the above, which feels like a cheat (I'm trying to become competent in PowerBI), or add a column with a DAX statement like below (which gives an error comparing Text and Number, which confuses me because I've changed the data type on 'Employee'[NEW_FORMAT_EMPLOYEE_ID] to Text in the Advanced Editor)


IF(
   IFERROR(VALUE('Sales Order'[SALESPERSON_ID]), -1) = -1,
   LOOKUPVALUE(
      'Employee'[DISPLAYNAME],
      'Employee'[OLD_FORMAT_EMPLOYEE_ID],
      'Sales Order'[SALESPERSON_ID]
   ),
   LOOKUPVALUE(
      'Employee'[DISPLAYNAME],
      'Employee'[NEW_FORMAT_EMPLOYEE_ID],
      'Sales Order'[SALESPERSON_ID]
   )
)


I'd appreciate any assistance getting this join/lookup to work in PowerBI.

Thanks,
Andrew

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi, @andrewpirie

 

First make sure that the data type of columns OLD_FORMAT_EMPLOYEE_ID and NEW_FORMAT_EMPLOYEE_ID in table EMPLOYEE are both text.

 

1.PNG2.PNG

 

Add a calculated column in table SALESORDER:

 

Name = IF(
LOOKUPVALUE(EMPLOYEE[DISPLAYNAME],EMPLOYEE[NEW_FORMAT_EMPLOYEE_ID],SALESORDER[SALESPERSON_ID])= "",
LOOKUPVALUE(EMPLOYEE[DISPLAYNAME],EMPLOYEE[OLD_FORMAT_EMPLOYEE_ID],SALESORDER[SALESPERSON_ID]),
LOOKUPVALUE(EMPLOYEE[DISPLAYNAME],EMPLOYEE[NEW_FORMAT_EMPLOYEE_ID],SALESORDER[SALESPERSON_ID])
)

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-yulgu-msft
Employee
Employee

Hi, @andrewpirie

 

First make sure that the data type of columns OLD_FORMAT_EMPLOYEE_ID and NEW_FORMAT_EMPLOYEE_ID in table EMPLOYEE are both text.

 

1.PNG2.PNG

 

Add a calculated column in table SALESORDER:

 

Name = IF(
LOOKUPVALUE(EMPLOYEE[DISPLAYNAME],EMPLOYEE[NEW_FORMAT_EMPLOYEE_ID],SALESORDER[SALESPERSON_ID])= "",
LOOKUPVALUE(EMPLOYEE[DISPLAYNAME],EMPLOYEE[OLD_FORMAT_EMPLOYEE_ID],SALESORDER[SALESPERSON_ID]),
LOOKUPVALUE(EMPLOYEE[DISPLAYNAME],EMPLOYEE[NEW_FORMAT_EMPLOYEE_ID],SALESORDER[SALESPERSON_ID])
)

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks Yuliana_Gu, that's a brilliant solution! 

 

Andrew

GilbertQ
Super User
Super User

What about the option of using the Merge Queires option in the Query Editor?

 

That will give you all the required options as shown below.

 

Merge.png





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!







Power BI Blog

ankitpatira
Community Champion
Community Champion

@andrewpirie Make sure data type on both the columns is same ie salesperson_id and new_format_employee_id.

@ankitpatira They're both set to text in PowerBI

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.