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

Reference another query via a join using the IN function, is this possible?

Is it possible to reference another query within the current query, specifically using a join and WHERE IN clause.  

 

1) When I try 

WHERE C.REGION IN (REGION) or and C.REGION IN (REGION[REGION]) I either get column abigiouly defined or missing right parenthiss messages.     REGION is seperate query in my data sets that also contains the column I want to match on called REGION.

 

Thanks for any advice.

 

 

 

 

 

 

2 ACCEPTED SOLUTIONS

I am fairly certain that you cannot reference another Power BI Query from within a SQL statement using the Advanced feature of the connector. You can reference another query within a different Power Query query (M code) but once you use the SQL code feature it is M code that goes out and executes straight-up SQL code. 

 

@ImkeF or someone else, please correct me if I am off base here.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

I'm having difficulties to understand what's requested here.:

But you can include results of your M-queries in SQL-statements: As text.

 

let
    Source = Sql.Database("localhost", 
"AdventureWorks2008R2",
[Query="select *
from [Production].[Product] as [_]
where [_].[ProductID] = " & Text.From(ProductID_QueryResult)]) in Source

So here for example, I'm referencing a result of my query "ProductID_QueryResult" as a parameter for the ProductID in the SQL-statement. I have to transform it into text, because that's the only way to integrate it in the SQL-code.

So if you want to integrate lists, you have to transform them into text-expressions first.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

That looks like SQL code whereas Power Query is M code so I can't make heads or tails of this. Are you putting this SQL code into the Advanced option of when connecting to a SQL db?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi Greg, yes I'm using an oracle connector and the advanced option under import.  

I have multiple queries, one called REGION, which I'm trying to reference from the below query.

 

I did manage to get it to work by removing the reference to REGION and instead using a full select statement in it's place.   I thought you could reference another query by name but I was not able to get this to work.

 

This works

WHERE C.REGION IN (SELECT DISTINCT REGION FROM BI_IDL_META.PB_LOB_OFFICER_ASC_RPT_HIER)

 

This does not work

WHERE C.REGION IN (REGION)

WHERE C.REGION IN (REGION[REGION])

I am fairly certain that you cannot reference another Power BI Query from within a SQL statement using the Advanced feature of the connector. You can reference another query within a different Power Query query (M code) but once you use the SQL code feature it is M code that goes out and executes straight-up SQL code. 

 

@ImkeF or someone else, please correct me if I am off base here.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

I'm having difficulties to understand what's requested here.:

But you can include results of your M-queries in SQL-statements: As text.

 

let
    Source = Sql.Database("localhost", 
"AdventureWorks2008R2",
[Query="select *
from [Production].[Product] as [_]
where [_].[ProductID] = " & Text.From(ProductID_QueryResult)]) in Source

So here for example, I'm referencing a result of my query "ProductID_QueryResult" as a parameter for the ProductID in the SQL-statement. I have to transform it into text, because that's the only way to integrate it in the SQL-code.

So if you want to integrate lists, you have to transform them into text-expressions first.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

PBIC
Frequent Visitor

I think this would be possible either using M in Power Query or by creatung a DAX table.

 

However, is there any advice as to which is the better approach?

 

The example might be a table containg 3 years of data and you require seperate tables containing:

LastYear,

CurrentYear

NextYear. 

Creating tables or columns in DAX has the disadvantage that run length encoding cannot be applied.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.