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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
zenton
Helper II
Helper II

Measure is slow when using variables

I have been trying to learn more about Optimising Dax using a data sample from sqlbi (DAX Optimizations Examples with Alberto Ferrari)  Orders Table has 1.6 million rows of data. I have two identical measures except one does not use variables. The measure with variables takes 14 seconds to execute and the other 0.5 seconds. I am unsure why variables are having an impact on performance. 

Measure 1

Open Orders =
VAR StartPeriod = MIN ( 'Date'[Date] )
VAR EndPeriod = MAX ( 'Date'[Date] )
RETURN
CALCULATE (
COUNTROWS (
FILTER (
Orders,
AND (
Orders[Order Date] <= EndPeriod,
Orders[Delivery Date] >= StartPeriod )
)
),
ALL ( 'Date' )
)
 
Measure 2
Open Orders =
CALCULATE (
COUNTROWS (
FILTER (
Orders,
AND (
Orders[Order Date] <= MAX ( 'Date'[Date] ),
Orders[Delivery Date] >= MIN ( 'Date'[Date] )
)
)
),
ALL ( 'Date' )
)
3 REPLIES 3
Anonymous
Not applicable

@zenton 

 

To be able to answer the question one has to see the execution plans for both queries. There is no other way.

 

However, some things to observe:

1. The two measures (they are not queries!) are not logically equivalent.

2. The second one removes all filters from 'Date' BEFORE calculating the number of rows, so the logical expressions in filter can be totally ignored (in the presence of the assumed relationship). This is not the case in the first query when you first calculate the bounds and then use the bounds in the expression. These bounds stay put despite the ALL directive.

wdx223_Daniel
Super User
Super User

did really these querys return the same result?

Yes identical results just different execution times

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors