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

SAMEPERIODLASTYEAR makes my report too slow in DirectQuery

Hi everyone,

 

I have a report with 4 visuals. One of them works without SAMEPERIODLASTYEAR and it's DAX query takes 75ms. But the other 3 takes at least 300ms. Debugging the slowest query I see this huge union:

 

((SELECT CAST( '20190301 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190302 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190303 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190304 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190305 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190306 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190307 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190308 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190309 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190310 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190311 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190312 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190313 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190314 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190315 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190316 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190317 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190318 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190319 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190320 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190321 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190322 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190323 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190324 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190325 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190326 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190327 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190328 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190329 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190330 00:00:00' AS datetime) AS [c94] )  UNION ALL 
(SELECT CAST( '20190331 00:00:00' AS datetime) AS [c94] ) )

 

Can be this the reason for those times?

Is there a way to avoid all this SELECT CAST?

5 REPLIES 5
Anonymous
Not applicable

Since you're using Direct Query DAX is being translated into SQL and this native query is sent to your data source. Your data source (say, SQL Server) must have the right constructs in place (e.g., indexes) for the queries to be fast. Slow response times are not a DAX problem. It's your data source problem. You can also try to write a different DAX formula to calculate what you want and see if the underlying SQL changes...

I don't know what can I do to solve this problem.

My database has a date table, with datetime format. 

Do you know other ways to calculate SAMEPERIODLASTYEAR? I tried DATEADD(date, -1, YEAR) and returns the same SQL query.

Anonymous
Not applicable

@hortiz4

I can't do anything without the .pbix file... Sorry. Troubleshooting slow SQL is not something one can do without the problem in front of one's eyes.

Thanks @Anonymous , I completely ignored the title that this was DirectQuery. Ignore my response, you can't use M with DirectQuery.

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

AllisonKennedy
Super User
Super User

This looks like SQL statement, not DAX. What is it being used for? You can just create a simple date list or table in M using Power Query instead.

let
startDate = #date(2019, 1, 1),
endDate = Date.From(DateTime.LocalNow()),
Dates = List.Dates(startDate, Duration.Days(endDate - startDate), #duration (1,0,0,0)),

https://excelwithallison.blogspot.com/2020/04/dimdate-what-why-and-how.html

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

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