Forum Discussion
Calcuate the closest less than date
Additional Note:
I am looking into the possible of using the Calculate function and nesting the Filter function in the calculation as well. Any guidance on using this method would be appreciated.
// DAX
// Calculated Column in 'Charge Activity' table
RateOnDate =
CALCULATE( VALUES( 'Rate Information'[Rate] ) ,TOPN( 1 ,CALCULATETABLE( 'Rate Information' ,'Rate Information'[Employee ID] = EARLIER( 'Charge Activity'[Employee ID] ) ,'Rate Information'[Rate Effective Date] <= EARLIER( 'Charge Activity'[Charge Date] ) ) ,'RateTable'[RateEffectiveDate] ,DESC ) )
CALCULATE() allows us to set a filter context in which to evaluate an expression. Argument 1 is the expression to evaluate. Arguments 2..N define filter contexts that are combined in a logical and to evaluate Argument 1 in.
Argument 1 is VALUES( 'Rate Information'[Rate] ). VALUES() returns the distinct values in a field based on filter context. If the number is distinct values is <= 1, then the result of VALUES() can be coerced to a scalar.
TOPN() returns the top N rows of some table based on sort criteria we define. We want to return one row of the table defined in CALCULATETABLE() sorted by [RateEffectiveDate] in descending order.
CALCULATETABLE() does for table expressions what CALCULATE() does for scalar expressions. We want to return a subset of 'Rate Information'. The first filter we apply to 'Rate Information' is to return only values where [Employee ID] matches the [Employee ID] in 'Charge Activity', where we are defining this calculated column. EARLIER() allows us to reach back to the row context in 'Charge Activity' from inside the CALCULATETABLE().
The second filter argument (third overall argument) to CALCULATETABLE() does a similar pattern, but based on the value of [Charge Date] in the row context of 'Charge Activity'.
Using the result of this TOPN() as a filter, CALCULATE() will return only the [Rate] that matches the criteria you defined in your post.
A relationship is unnecessary between these tables for this calculated column to work.
- kreese10 years agoFrequent Visitor
Thank you so much for your response. I'm receiving this error. Here is how I wrote out the calculation.
EARLIER/EARLIEST refers to an earlier row context which doesn't exist
// DAX // Calculated Column in ‘Charge Activity Report’ table RateOnDate = CALCULATE( VALUES( 'Rate Information'[Rate] ) ,TOPN( 1 ,CALCULATETABLE( 'Rate Information' ,'Rate Information'[Employee ID] = EARLIER( ‘Charge Activity Report’[Employee Number] ) ,'Rate Information'[Rate Effective Date] <= EARLIER( ‘Charge Activity Report’[Charge Date] ) ) ,'RateTable'[Rate Effective Date] ,DESC ) ) - Anonymous9 years agoNot applicable
This was a really good trick! In my case it works really well, but sometimes there is a "draw". Meaning there are several records with the same date. Is it possible to add an Average component to your model to cope with this type od situations? Meaning in the case there is more than one record as a result of the filter and ranking - the result is the average between them? Many thanks!
- Anonymous9 years agoNot applicable
Just figured it out. For me it is a good solution to wrap the whole TopN part in a FirstNonBlank statement, even if it was not exactly what I asked for.