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
itimatic
Frequent Visitor

if a condition is true then select a value from another table

Hello! Can someone please help me out with this SQL query?
How to achieve this SQL case in Power Query?
I'm trying to create a New custom column with a formula that can ahieve this:

if [subject_type] = "User" then SELECT username from users table
else if [subject_type] = "Group" then SELECT name from groups table
else if [subject_type] = "ResourceOpportunity" then SELECT title from resource_occasions table
else if [subject_type] = "QuestionAnswer" then SELECT name from questions_answers table
else [subject_type]

But this doesn't work at all. Can someone help please. I'm trying to resolve this since few days. Thanks !

 

CASE
    WHEN al.subject_type LIKE '%User' THEN (SELECT  CONCAT_WS(" ",u.fname,u.lname) from users WHERE users.id = al.subject_id )
	WHEN al.subject_type LIKE '%Group' THEN (SELECT name from `groups` WHERE `groups`.id = al.subject_id )
	WHEN al.subject_type LIKE '%ResourceOpportunity' THEN (SELECT title from resource_occasions ro JOIN resources r ON  r.id= ro.resource_id AND ro.id = al.subject_id)
	WHEN al.subject_type LIKE '%QuestionAnswer' THEN 
    (SELECT ri.name from question_answers qa 
    JOIN questions q ON  q.id= qa.question_id 
    JOIN resource_items ri ON ri.id = q.item_id
    AND qa.id = al.subject_id)
    ELSE al.subject_type
END Resource

 

 

1 REPLY 1
Anonymous
Not applicable

If you want to broaden the potential set of helpers to include even those unfamiliar with SQL, you should describe the structure of your data (table names with their column names). You should also, I understand, explain the relationships in terms of which columns of the different pairs of tables are associated. In itself, the problem of selecting one (or more rows) of a table as a function of a value of some column of the current table is not a difficult problem (although performance problems may arise)

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.

Top Solution Authors