Forum Discussion
DATEDIFF with EARLIER Function
- 7 years ago
Hey,
i guess you can use this DAX statement to create a Calculated Column
DateDiff Previous TransactiondDate = DATEDIFF( CALCULATE( MAX(Table1[TransactionDate]) ,FILTER( ALL('Table1') ,'Table1'[DeviceID] = EARLIER(Table1[DeviceID]) && 'Table1'[TransactionDate] < EARLIER(Table1[TransactionDate]) ) ) ,'Table1'[TransactionDate] ,DAY )If you use Power BI or an Excel version that supports variables you can also this variant, there is no performance difference in both variants, but personally I prefer to use variables because I thing variables make more complex statements more readable. And often there is also a performance improvement because variables are able to cash the result of an expression, and hence will be evaluated just once, but this of course depends from the given situation.
variables DateDiff Previous TransactiondDate = var currentDeviceID = 'Table1'[DeviceID] var currentTransactionDate = 'Table1'[TransactionDate] return DATEDIFF( CALCULATE( MAX(Table1[TransactionDate]) ,FILTER( ALL('Table1') ,'Table1'[DeviceID] = currentDeviceID && 'Table1'[TransactionDate] < currentTransactionDate ) ) ,'Table1'[TransactionDate] ,DAY )Hopefully this is what you are looking for.
Regards,
Tom
Hey,
i guess you can use this DAX statement to create a Calculated Column
DateDiff Previous TransactiondDate =
DATEDIFF(
CALCULATE(
MAX(Table1[TransactionDate])
,FILTER(
ALL('Table1')
,'Table1'[DeviceID] = EARLIER(Table1[DeviceID]) && 'Table1'[TransactionDate] < EARLIER(Table1[TransactionDate])
)
)
,'Table1'[TransactionDate]
,DAY
)
If you use Power BI or an Excel version that supports variables you can also this variant, there is no performance difference in both variants, but personally I prefer to use variables because I thing variables make more complex statements more readable. And often there is also a performance improvement because variables are able to cash the result of an expression, and hence will be evaluated just once, but this of course depends from the given situation.
variables DateDiff Previous TransactiondDate =
var currentDeviceID = 'Table1'[DeviceID]
var currentTransactionDate = 'Table1'[TransactionDate]
return
DATEDIFF(
CALCULATE(
MAX(Table1[TransactionDate])
,FILTER(
ALL('Table1')
,'Table1'[DeviceID] = currentDeviceID && 'Table1'[TransactionDate] < currentTransactionDate
)
)
,'Table1'[TransactionDate]
,DAY
)
Hopefully this is what you are looking for.
Regards,
Tom
- Anonymous7 years agoNot applicable
Worked great. Thanks!