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
Anonymous
Not applicable

CONVERT SQL IN STATEMENT TO POWER QUERY

Hello everyone,

 

Because I am using OData as the data source, I cannot query data as I want when inputting to data model.

 

So I need to use Power Query to modify my table.

 

For example as the picture, there are table A and table B. I want to create a column in table A named ID_Name. If the value in column Name of table A exists in table B, the value in ID_Name column will be Name column only. Otherwise, it will be ID+Name.

 

Here is the example SQL statement:

SELECT CASE WHEN A.NAME IN (SELECT NAME FROM B)

             THEN A.ID

             ELSE A.ID+A.NAME END AS ID_NAME
FROM A

 

How can I convert this SQL statement into Power Query?

 

Thank you for your help.

 

Capture.PNG

1 ACCEPTED SOLUTION
v-yuta-msft
Community Support
Community Support

Hi khoahong,

 

To achieve your requirement, please refer to following steps.

1.Merge table ‘A’ and table ‘B’ with left outer join.

1.PNG2.PNG

2.Then create a custom column using M code like below:

= Table.AddColumn(#"Expanded B", "New Column", each if [B.NAME] = null then [ID] & [NAME] else [ID])

The result is like below and you can refer to PBIX here:

https://www.dropbox.com/s/zgyzfucv6ddz3vk/CONVERT%20SQL%20IN%20STATEMENT%20TO%20POWER%20QUERY.pbix?d...

 3.PNG

 

Regards,

Jimmy Tao

View solution in original post

1 REPLY 1
v-yuta-msft
Community Support
Community Support

Hi khoahong,

 

To achieve your requirement, please refer to following steps.

1.Merge table ‘A’ and table ‘B’ with left outer join.

1.PNG2.PNG

2.Then create a custom column using M code like below:

= Table.AddColumn(#"Expanded B", "New Column", each if [B.NAME] = null then [ID] & [NAME] else [ID])

The result is like below and you can refer to PBIX here:

https://www.dropbox.com/s/zgyzfucv6ddz3vk/CONVERT%20SQL%20IN%20STATEMENT%20TO%20POWER%20QUERY.pbix?d...

 3.PNG

 

Regards,

Jimmy Tao

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.