Forum Discussion
Help! Return earliest date with condition in calculated column
Hi fellow PBI'ers
I've spend hours trying to work this out... I've trying to get the earliest instance of a date to calculate cumulative lifetime of a customer.
Basically, I need the earliest start date repeated until the start date is greater than 90 days from the last latest end date (defined as a churned customer).
Hopefully the "Desired result" column makes sense. Where there is "RESET" is where there is a 90 day or greater difference between the current row start date and the previous latest end date. In this instance, it would then use the new start date in the row
| Account | Start_Date__c | End_Date__c | Contract_days | Desired result | |
| Psuedo Company | 12-May-20 | 19-May-20 | 7 | 12-May-20 | |
| Psuedo Company | 7-Feb-22 | 25-Feb-22 | 18 | 7-Feb-22 | RESET |
| Psuedo Company | 7-Feb-22 | 25-Feb-22 | 18 | 7-Feb-22 | |
| Psuedo Company | 26-Feb-22 | 22-Mar-22 | 24 | 7-Feb-22 | |
| Psuedo Company | 26-Feb-22 | 22-Mar-22 | 24 | 7-Feb-22 | |
| Psuedo Company | 25-Mar-22 | 25-Apr-22 | 31 | 7-Feb-22 | |
| Psuedo Company | 25-Mar-22 | 25-Apr-22 | 31 | 7-Feb-22 | |
| Psuedo Company | 25-Apr-22 | 25-May-22 | 30 | 7-Feb-22 | |
| Psuedo Company | 25-Apr-22 | 25-May-22 | 30 | 7-Feb-22 | |
| Psuedo Company | 1-Oct-22 | 1-Nov-22 | 31 | 1-Oct-22 | RESET |
| Psuedo Company | 1-Oct-22 | 1-Dec-22 | 61 | 1-Oct-22 | |
| Psuedo Company | 1-Dec-22 | 1-Jan-23 | 31 | 1-Oct-22 |
Here is what i have so far...
Getting the previous end date. This is used to determine if if datediff between start date and this column is > 90 days.
Previous_end =
var account = 'Table'[Account]
var previous_start = 'Table'[Start_Date__c]
var previous_end = 'Table'[End_Date__c]
var last_end_date = //find the last start date
CALCULATE(max('Table'[End_Date__c]),
FILTER(
ALL('Table'),
'Table'[Account] = account &&
'Table'[End_Date__c] < previous_end))
var fill_end = IF ( last_end_date = blank(), previous_end, last_end_date)
RETURN
fill_end
Then i tried to find the start dates but it's just pretty much getting the previous rows start date...
earliest_start =
var account = 'Table'[Account]
var previous_start = 'Table'[Start_Date__c]
var previous_end = 'Table'[End_Date__c]
var first_start_date = //find the last start date
CALCULATE(
min('Table'[Start_Date__c]),
FILTER(
ALL('Table'),
'Table'[Account] = account &&
'Table'[Start_Date__c] >= previous_start &&
datediff( 'Table'[Previous_end], 'Table'[Start_Date__c] , day) < 90 ))
RETURN
first_start_date
Any help would be appreciated and huge life saver!! 🙂
Hi,
Thank you for your feedback.
Please check the below picture and the attached pbix file.
I think i've managed to tweak the first step DAX to get it to work!
Basically added an additional condition to get each latest previous end date per row and check to see if the current row start date is less than that.
Jihwan_Kim thank you so much for your help! True lifesaver 🙂
Step one CC = var _starting = MIN('Data V3'[Start_Date__c]) VAR _currentrowstartdate = 'Data V3'[Start_Date__c] VAR _currentrowenddate = 'Data V3'[End_Date__c] VAR _previousrowenddate = MAXX ( FILTER ( 'Data V3', 'Data V3'[End_Date__c] <= _currentrowstartdate), 'Data V3'[End_Date__c] ) VAR _latestlastenddate = MAXX ( FILTER ( 'Data V3', 'Data V3'[End_Date__c] <= _currentrowenddate && 'Data V3'[Start_Date__c] < _currentrowstartdate), 'Data V3'[End_Date__c] ) VAR _diff = DATEDIFF ( _previousrowenddate, _currentrowstartdate, DAY ) VAR _condition = IF ( 'Data V3'[Start_Date__c] = _starting || _diff > 90 && 'Data V3'[Start_Date__c] >= _latestlastenddate , 1, 0 ) RETURN _condition
6 Replies
- Jihwan_KimSuper User
- awffHelper III
Hi Jihwan_Kim
Amazing! Thank you, it's so close!
I just applied this to a larger scale dataset and came across an issue where if there are dates that start and end on the same day, the desired result would appear incorrectly
see image in the last row, reading 1/08/24 rather than 1/08/23. It seems to be common across contract days greater than 90
below sample table with those dates:
Account Start_Date__c End_Date__c Contract_days Desired result Psuedo Company 12-May-20 19-May-20 7 12-May-20 Psuedo Company 7-Feb-22 25-Feb-22 18 7-Feb-22 RESET Psuedo Company 7-Feb-22 25-Feb-22 18 7-Feb-22 Psuedo Company 26-Feb-22 22-Mar-22 24 7-Feb-22 Psuedo Company 26-Feb-22 22-Mar-22 24 7-Feb-22 Psuedo Company 25-Mar-22 25-Apr-22 31 7-Feb-22 Psuedo Company 25-Mar-22 25-Apr-22 31 7-Feb-22 Psuedo Company 25-Apr-22 25-May-22 30 7-Feb-22 Psuedo Company 25-Apr-22 25-May-22 30 7-Feb-22 Psuedo Company 1-Oct-22 1-Nov-22 31 1-Oct-22 RESET Psuedo Company 1-Oct-22 1-Dec-22 61 1-Oct-22 Psuedo Company 1-Dec-22 1-Jan-23 31 1-Oct-22 Psuedo Company 1-Aug-23 1-Aug-24 365 1-Aug-23 RESET Psuedo Company 1-Aug-24 1-Aug-25 365 1-Aug-23 - Jihwan_KimSuper User
Hi,
Thank you for your feedback.
Please check the below picture and the attached pbix file.
- awffHelper III
I think i've managed to tweak the first step DAX to get it to work!
Basically added an additional condition to get each latest previous end date per row and check to see if the current row start date is less than that.
Jihwan_Kim thank you so much for your help! True lifesaver 🙂
Step one CC = var _starting = MIN('Data V3'[Start_Date__c]) VAR _currentrowstartdate = 'Data V3'[Start_Date__c] VAR _currentrowenddate = 'Data V3'[End_Date__c] VAR _previousrowenddate = MAXX ( FILTER ( 'Data V3', 'Data V3'[End_Date__c] <= _currentrowstartdate), 'Data V3'[End_Date__c] ) VAR _latestlastenddate = MAXX ( FILTER ( 'Data V3', 'Data V3'[End_Date__c] <= _currentrowenddate && 'Data V3'[Start_Date__c] < _currentrowstartdate), 'Data V3'[End_Date__c] ) VAR _diff = DATEDIFF ( _previousrowenddate, _currentrowstartdate, DAY ) VAR _condition = IF ( 'Data V3'[Start_Date__c] = _starting || _diff > 90 && 'Data V3'[Start_Date__c] >= _latestlastenddate , 1, 0 ) RETURN _condition