Forum Discussion
Slow Measures
- Anonymous8 years ago
With this measure, you are calling, and calculating the [Scr%_ApptDesir] measure 7 times...it only needs to be calculated once.
try this:
Grd_ApptDesir = VAR Score = [Scr%_ApptDesir] RETURN SWITCH ( TRUE (), Score >= .9, "A", Score >= .8, "B", Score >= .7, "C", Score >= .6, "D", "F" )
BillyT_350, no worries!
I realize that it seems buggy, but consider this:
Everytime the measure is called in an expression, the filter and/or context may be different. That is why it needs to be evaluated separately each time.
Paste the other measures that are referenced in the final measure, we may be able to optimize those as well.
Anonymous That's true, but PBI already calculates a given measure any time you change a slicer. So why isn't a given measure just stored as a variable on the back end? It only needs to change if the user changes it's filtering, and any number of other measures, columns, etc, should be able to call it at will.
I'm already at work looking to add in switch statements to speed things up in other places in this report and other reports that I've made. What other types of recursive statements are there? A "switch" is basically a "case". What about "for/foreach"s?
Here is a sample of one of the Scr%s. Some of what I do here is necesary because of the way the data is structured.
[m_ApptTimes] is simply a sum(x)/sum(y)
I used [m_ApptTimes] > AVERAGE... because I have to use a function in a CALCULATE, and the average of one item is simply itself. However, if for some weird reason there are multiple values, using average should lessen the damage, vs a sum, product, etc.
In short, the Scr% codes check to see if the a metric falls within a certain range. If it it outside the range, it will produce a 100% or 0%. Otherwise, the percentage is calculated based on the range.
Now, these still load quite quickly. The worse offender by far is still the letter grade measures above.
Scr%_ApptDesir = IF(CALCULATE([m_ApptTimes] < AVERAGE('CUS_RANGE'[MIN_VALUE])
, FILTER('CUS_RANGE'
, AND('CUS_RANGE'[BUS_UNIT] = SELECTEDVALUE('MTHLY'[IBU], "")
, AND('CUS_RANGE'[SUMMARY_TYPE] = "ACCT_NBR"
, 'CUS_RANGE'[METRIC_NAME] = "APPT_DESIR"
)
)
)
)
, 1
, IF(CALCULATE([m_ApptTimes] > AVERAGE('CUS_RANGE'[MAX_VALUE])
, FILTER('CUS_RANGE'
, AND('CUS_RANGE'[BUS_UNIT] = SELECTEDVALUE('MTHLY'[IBU], "")
, AND('CUS_RANGE'[SUMMARY_TYPE] = "ACCT_NBR"
, 'CUS_RANGE'[METRIC_NAME] = "APPT_DESIR"
)
)
)
)
, 0
, CALCULATE( (1 - ( ([m_ApptTimes] - AVERAGE('CUS_RANGE'[MIN_VALUE])) / (AVERAGE('CUS_RANGE'[MAX_VALUE]) - AVERAGE('CUS_RANGE'[MIN_VALUE])) ))
, FILTER('CUS_RANGE'
, AND('CUS_RANGE'[BUS_UNIT] = SELECTEDVALUE('MTHLY'[IBU], "")
, AND('CUS_RANGE'[SUMMARY_TYPE] = "ACCT_NBR"
, 'CUS_RANGE'[METRIC_NAME] = "APPT_DESIR"
)
)
)
)
)
)- Anonymous8 years agoNot applicable
BillyT_350, this will optimize your measure a little bit:
Scr%_ApptDesir = VAR FilteredTable = FILTER ( 'CUS_RANGE', 'CUS_RANGE'[BUS_UNIT] = SELECTEDVALUE ( 'MTHLY'[IBU], "" ) && 'CUS_RANGE'[SUMMARY_TYPE] = "ACCT_NBR" && 'CUS_RANGE'[METRIC_NAME] = "APPT_DESIR" ) RETURN IF ( CALCULATE ( [m_ApptTimes] < AVERAGE ( 'CUS_RANGE'[MIN_VALUE] ), FilteredTable ), 1, IF ( CALCULATE ( [m_ApptTimes] > AVERAGE ( 'CUS_RANGE'[MAX_VALUE] ), FilteredTable ), 0, CALCULATE ( ( 1 - DIVIDE ( ( [m_ApptTimes] - AVERAGE ( 'CUS_RANGE'[MIN_VALUE] ) ), ( AVERAGE ( 'CUS_RANGE'[MAX_VALUE] ) - AVERAGE ( 'CUS_RANGE'[MIN_VALUE] ) ) ) ), FilteredTable ) ) )You're using the same filter arugment in 3 different instances, so there's no need to scan the table 3 times to make the same table. Just assign it to a variable and off you go.
I'm also a little curious about this expression. is it a calculated column, or a measure? If it's a measure, I would do this to further optimize:
Scr%_ApptDesir = VAR FilteredTable = FILTER ( 'CUS_RANGE', 'CUS_RANGE'[BUS_UNIT] = SELECTEDVALUE ( 'MTHLY'[IBU], "" ) && 'CUS_RANGE'[SUMMARY_TYPE] = "ACCT_NBR" && 'CUS_RANGE'[METRIC_NAME] = "APPT_DESIR" ) VAR AverageMin = AVERAGE ( 'CUS_RANGE'[MIN_VALUE] ) /*Or this CALCULATE( AVERAGE ( 'CUS_RANGE'[MIN_VALUE] ), FilteredTable ) */ VAR AverageMax = AVERAGE ( 'CUS_RANGE'[MAX_VALUE] ) /*Or this CALCULATE( AVERAGE ( 'CUS_RANGE'[MAX_VALUE] ), FilteredTable ) */ RETURN IF ( CALCULATE ( [m_ApptTimes] < AverageMin, FilteredTable ), 1, IF ( CALCULATE ( [m_ApptTimes] > AverageMax, FilteredTable ), 0, CALCULATE ( ( 1 - DIVIDE ( ( [m_ApptTimes] - AverageMin ), ( AverageMax - AverageMin ) ) ), FilteredTable ) ) )I can't tell if you want the TRUE/FALSE statement to be in the calculate expression, or if you want to compare the [m_ApptTimes] measure to the Average of the Min/Max column, and have both of them respect the filtered table variable.
Even still, the first expression will be at least a little faster.
- BillyT_3508 years agoHelper V
Anonymous the Scr%s are measures. A few tweaks, and I think this will work well.
To answer your question, we're merely checking to see if the calculated appointment times [m_ApptTimes] are greater than the min or the max allotted (contained with many others in table 'CUS_RANGE'). So, the two ways you've coded it would need to be altered a little to be like this:
RETURN IF ( [m_ApptTimes] < CALCULATE ( AVERAGE('CUS_RANGE'[MIN_VALUE]), FilteredTable ), 1, ...or,
VAR AverageMin = CALCULATE( AVERAGE ( 'CUS_RANGE'[MIN_VALUE] ), FilteredTable ) VAR AverageMax = CALCULATE( AVERAGE ( 'CUS_RANGE'[MAX_VALUE] ), FilteredTable ) RETURN IF ( [m_ApptTimes] < AverageMin, 1, IF ( [m_ApptTimes] > AverageMax, 0, ...So, it would depend on which of the two methods used. Would it be faster to forgoe creating those variables, as each is only called twice anyways?
Thanks again! You've really advanced my knowledge of DAX. I didn't know that DAX could do half of the things that you've done in this code. I will go back and review more of my code, and see where I can use your suggestions.I think the the VARs will be particularly useful.
- BillyT_3508 years agoHelper V
Anonymous Implemented those changes. Page refreshes down to 10 seconds! This is what I've got them looking like now:
Scr%_ApptWinHrs = VAR FilterTable = FILTER('CUS_RANGE' , 'CUS_RANGE'[BUS_UNIT] = SELECTEDVALUE('MTHLY'[IBU], "") && 'CUS_RANGE'[SUMMARY_TYPE] = "ACCT_NBR" && 'CUS_RANGE'[METRIC_NAME] = "APPT_WIN_HR") VAR MinVal = CALCULATE(AVERAGE('CUS_RANGE'[MIN_VALUE]), FilterTable) VAR MaxVal = CALCULATE(AVERAGE('CUS_RANGE'[MAX_VALUE]), FilterTable) VAR Metric = [m_ApptWinHrs] RETURN SWITCH( TRUE() , Metric > MinVal, 1 , Metric < MaxVal, 0 , (Metric - MaxVal) / (MinVal - MaxVal) )- Anonymous8 years agoNot applicable
That last measure looks great! I figured that's where you were heading towards, but I couldn't quite parse it out the way you had it first written.
Always use variables. If you evaluate the same expression more than once, make it a variable.
2 other pieces of advice:
- use DIVIDE() on your RETURN statement.
- Just in case MinVal = MaxVal, you'll catch it with DIVIDE and you won't return a divide by 0 error.
- If you're 100% sure you'll never come across that error (very well may be the case, but I don't know your data), you can ignore it. It will slow down performance just a touch, but it's much faster than using an IF() to catch dividing by 0.
- Don't use FILTER() to filter the entire 'CUS_RANGE' table.
FILTER() is an iterator, so it will look at EVERY row in the table in the current filter context, and include the row if it passes the 3 criteria that you set. I would recommend the following code for VAR FilteredTable:
VAR CurrentBusUnit = SELECTEDVALUE ( 'MTHLY'[IBU], "" ) VAR FilterTable = CALCULATETABLE ( 'CUS_RANGE', 'CUS_RANGE'[BUS_UNIT] = CurrentBusUnit, 'CUS_RANGE'[SUMMARY_TYPE] = "ACCT_NBR", 'CUS_RANGE'[METRIC_NAME] = "APPT_WIN_HR" ) ...rest of the measureInstead of looking at every row in the current filter context, this expression will evaluate the entire table in the context of the 3 filters. Should take you below 10 seconds.
You may be able to replace that first parameter of CALCULATETABLE() (the 'CUS_RANGE') with a
VALUES( 'CUS_RANGE'[ColumnName] )
if you have a unique identifier column. That will then return only the distinct list of values for that specific column where the other 3 columns are as specified. That single column table will then act as the filter for everything else.
In short, don't use FILTER() on an entire table if you can get around it.
Good luck!
- use DIVIDE() on your RETURN statement.