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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
smpa01
Super User
Super User

Value.Native Query and CTE

@bbrowne @ImkeF @AlexisOlson 

 

I am querying a table from sql server (lakehouse) and I am noticing that Value.NativeQuery generates and error

E.g.

 

//fn1
let
    sql = (server as text, db as text, query as text) as any => Sql.Database(server, db, [Query=query])
in
    sql

//fn2
let
    sql = (server as text, db as text, query as text) as any => 
        let
            source = Sql.Database(server, db),
            Q = Value.NativeQuery(source, query, null, [PreserveTypes = true, EnableFolding = true])
        in
            Q
in
    sql

 

If you invoke both with following 3rd parameter, the 2nd one errors out.

 

 

";with cte as (select 1 as colA) select * from cte"

 

But in reality , I want the 2nd one to work as power bi  does not let me apply incremental refresh on the query result from 1st function callback but IR is available to the result set of the 2nd callback.

 

How can I make the 2nd function to work with CTE? The workaround is 1) to use a table variable with  DECLARE but table variables are not avaialble in lakehouse sql or 2) convert ctes somehow to subquery and compromise performance and query readability.

 

Thank you in advance. CTEs are unavoidable in  this case.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

I just read about this issue on Reddit the other day:

CTEs don't really work with query folding because they are in-memory tables and don't typically work when treated as a sub-query which folding attempts to do.

Need guidance on Value.NativeQuery() limitation when using SQL CTEs, and why these CTE expressions d...

It sounds like you might not be able to get both query folding and CTEs working at the same time.

View solution in original post

3 REPLIES 3
AlexisOlson
Super User
Super User

I just read about this issue on Reddit the other day:

CTEs don't really work with query folding because they are in-memory tables and don't typically work when treated as a sub-query which folding attempts to do.

Need guidance on Value.NativeQuery() limitation when using SQL CTEs, and why these CTE expressions d...

It sounds like you might not be able to get both query folding and CTEs working at the same time.

MS must do something about it by adding support for CTEs/temp table/table variable.

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

HI @smpa01,

In fact, this type of operations already been limited to use in fabric.

You can't directly create in-memory temporary tables at the query data operations. In my opinion, I'd like to suggest you split these to multiple steps and create fact data to instead temporary table.(you can remove them after processing)

In addition, you can also submit idea for these types of operations.

Microsoft Fabric Ideas

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.