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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
aangers
Regular Visitor

Direct Query + Snowflake resulting in queries with cast as date for every unique date

When generating reports using DirectQuery with a Snowflake Connector, I am encountering badly transposed queries in Snowflake for queries where the data is filtered on a Date or Date/Time range. The queries will have a CAST as DATE for every unique Date, or a TO_TIMESTAMP for every unique Date/Time entry. Since our data covers many years, the queries get very long and complex for filtered date ranges. Yet, the date variable in my data is set to Date format in Power BI as well as in Snowflake.

 

Is there any way to optimize the query and make sure that the date variable is well integrated in the query?

Screenshot 2023-12-04 092545.png

 

 

9 REPLIES 9
enjoytheride
Frequent Visitor

I'm running into the same problem, did you happen to find a solution?

Nope haven't found a solution unfortunately

sofia_pc
Regular Visitor

I'm having the same issue. Did you find a solution?

I have not unfortunately...

aangers
Regular Visitor

Thanks for the response.

I've read about Value.NativeQuery and tried to apply this to the Power Query configuration of my Snowflake connexion. Here's the full code in the Advanced Editor. I have no other steps, all other data processing steps are made in DAX calculated measures.

 

let

Source = Snowflake.Databases("XXXX","WH_XXXX"),
DB_XXXX = Source{[Name="DB_XXXX",Kind="Database"]}[Data],
SCHEMA_XXXX = DB_XXXX{[Name="SCHEMA_XXXX",Kind="Schema"]}[Data],
MY_TABLE = SCHEMA_XXXX{[Name="MYTABLE",Kind="Table"]}[Data],
SqlStatement = "SELECT * FROM DB_XXXX.SCHEMA_XXXX.MYTABLE",
NativeQuery = Value.NativeQuery(MY_TABLE, SqlStatement, null, [EnableFolding=true])

in

NativeQuery

 

However, when I apply this, I get the following error 

Expression.Error: Native queries aren't supported by this value.
Details:

[Table]

 

Do you know what would cause this error?

 

Secondly, I also checked all my DAX measures so that when dates are filtered, it is done with ">=" instead of using "IN {range}". None of my DAX measures had an "IN {range}" statement.

 

Here is one of my DAX measures, maybe I'm not seeing something?

 

my_measure =
VAR metric = SELECTEDVALUE(Table[ID])

VAR ThisDate = SWITCH(TRUE(),
    metric = "LASTWKPCT", MAX(Series1[EndDate]),
    metric = "LASTMTHPCT", MAX(Series2[StartDate]),
    metric = "WKYOYPCT", MAX(Series1[EndDate]),
    metric = "MTHYOYPCT", MAX(Series2[StartDate])
)

VAR my_list = DISTINCT(SELECTCOLUMNS(FILTER(Series1, Series1[EndDate] = ThisDate && NOT ISBLANK(Series1[Count])), "SiteID", [SiteID]))
VAR my_list_m= DISTINCT(SELECTCOLUMNS(FILTER(Series2, Series2[StartDate] = ThisDate && NOT ISBLANK(Series2[Count])), "SiteID", [SiteID]))

VAR Vol_ThisDate = SWITCH(TRUE(),
    metric = "LASTWKPCT", CALCULATE(SUM(Series1[Count]), Series1[Categ]=1),
    metric = "LASTMTHPCT", CALCULATE(SUM(Series2[Count]), Series2[Categ]=1),
    metric = "WKYOYPCT", CALCULATE(SUM(Series1[Count]), Series1[Categ]=1),
    metric = "MTHYOYPCT", CALCULATE(SUM(Series2[Count]), Series2[Categ]=1)
)

VAR Vol_PrevDate = SWITCH(TRUE(),
    metric = "LASTWKPCT", CALCULATE(SUM(Series1[Count2]), Series1[Categ]=1 && Series1[SiteID] IN my_list),
    metric = "LASTMTHPCT", CALCULATE(SUM(Series2[Count2]), Series2[Categ]=1 && Series2[SiteID] IN my_list_m),
    metric = "WKYOYPCT", CALCULATE(SUM(Series1[Count2]), Series1[Categ]=1 && Series1[SiteID] IN my_list),
    metric = "MTHYOYPCT", CALCULATE(SUM(Series2[Count2]), Series2[Categ]=1 && Series2[SiteID] IN my_list_m)
)

RETURN SWITCH(TRUE(),
    ThisDate < [DATE_PrevDate] || ThisDate > [DATE_ThisDate], BLANK(),
    Vol_ThisDate > 0 && Vol_PrevDate > 0, DIVIDE(Vol_ThisDate - Vol_PrevDate, Vol_PrevDate)
)

You need to run your native query against the database, not the table.

Sorry I misunderstood how to apply the Value.NativeQuery. Here is how I managed to bypass this error : 

 

= Value.NativeQuery(Snowflake.Databases("XXXX","WH_XXXX"){[Name="DB_XXXX"]}[Data], "SELECT * FROM ""SCHEMA_XXXX"".""MYTABLE""", null, [EnableFolding=true])

 

However, the problem persists, the queries sent to snowflake still have the "cast as date"... Do you know how to control the queries that are created and sent to Snowflake from DAX calculated measures?

lbendlin
Super User
Super User

great question.  Please read about Value.NativeQuery  (and its folding options).  BUT - you will also want to change your query from   "[Date] IN {Range}  " to "[Date] >= MinDate && [Date]<= MaxDate"

 

What you have hit upon is a frequent issue in composite models where the join column cardinality is too high. They use a similar set based query which can result in truly ginormous queries - I had a case where the query text (just the text!!!) was 6GB.  FOR. EVERY. SINGLE. QUERY. 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.