Forum Discussion
DateDiff between different stages
Hi, community
I'm trying to calculate the average sales cycle length.
In my case, I'm trying to make a difference between data from different rows.
For example, the opportunity 0060Y00000J1tMvQAJ was created on 19/03/2018 (Each new opportunities automatically takes the Challenge stage). This opportunity staid 490 days in Challenge then moved to the stage Value on 22/07/2019, and 3 days later moved to Solution. As you can see an opportunity doesn't have the requirement to go through all the stage (Challenge to Solution, Challenge to Closed Won).
Many thanks. :smileyhappy:
- Anonymous7 years ago
Hi,
I finally find a way to do it I think there is others way to simplify the formula but it's working.
Many thanks for your contribution.
Days Challenge = IF ( 'Opportunity Field History'[OldValue] = "Challenge"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[Field] = "created" ); 'Opportunity Field History'[CreatedDate]; DAY ); IF ( 'Opportunity Field History'[OldValue] = "Closing"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[NewValue] = "Closing" ); 'Opportunity Field History'[CreatedDate]; DAY ); IF ( 'Opportunity Field History'[OldValue] = "Solution"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[NewValue] = "Solution" ); 'Opportunity Field History'[CreatedDate]; DAY ); IF ( 'Opportunity Field History'[OldValue] = "Value"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[NewValue] = "Value" ); 'Opportunity Field History'[CreatedDate]; DAY ); BLANK () ) ) ) )
8 Replies
- StachuCommunity Champion
try this code for the measure
Measure = VAR __OpportunityStartEnd = GROUPBY ( 'Opportunites', Opportunites[OpportunityId], "Start", MINX ( CURRENTGROUP (), 'Opportunites'[CreatedDate] ), "End", MAXX ( CURRENTGROUP (), 'Opportunites'[CreatedDate] ) ) VAR __OpportunityLength = ADDCOLUMNS ( __OpportunityStartEnd, "Length", [End] - [Start] ) RETURN AVERAGEX ( __OpportunityLength, [Length] )- AnonymousNot applicable
Hi Stachu,
Many thanks for your help but it's not working. I did it using excel and for each opportunity ID, I have the number of days staid per stage. For that I was using this formula:
=IF(D427="Value";MAXIFS(F:F;C:C;C427;D:D;D427)-MAXIFS(F:F;C:C;C427;E:E;D427);"")
Many thanks
- StachuCommunity Champion
I think the easiest way to do it is to calculate the cycle length in the calculated column, like this:
Cycle Length = VAR __OpportunityId = 'Opportunites'[OpportunityId] VAR __CreatedDate = 'Opportunites'[CreatedDate] VAR __PreviousStageDate = CALCULATE ( MIN ( 'Opportunites'[CreatedDate] ), FILTER ( ALL ( 'Opportunites' ), 'Opportunites'[OpportunityId] = __OpportunityId && 'Opportunites'[CreatedDate] > __CreatedDate ) ) VAR __PreviousStageDateNoBlanks = IF ( ISBLANK ( __PreviousStageDate ), __CreatedDate, __PreviousStageDate ) RETURN __PreviousStageDateNoBlanks - __CreatedDateand then calculate the average based on that (I filter out 0 length cycles, as the end of the process is a point in time, and therefore doesn't have length):
Cycle Avg Length = VAR __OpportunityCycleLengthNo0s = FILTER ( 'Opportunites', 'Opportunites'[Cycle Length] <> 0 ) VAR __AverageCyclePerOpportunity = GROUPBY ( __OpportunityCycleLengthNo0s, Opportunites[OpportunityId], "Avg", AVERAGEX ( CURRENTGROUP (), 'Opportunites'[Cycle Length] ) ) RETURN AVERAGEX ( __AverageCyclePerOpportunity, [Avg] )I used the average to average the averages of multiples IDs (this only shows when you have multiple IDs in the filter context, e.g. in the Total line), is this your intended aggregation on this level?
- AnonymousNot applicableHow is a cycle defined? Since you can have opportunities in different stages, one has to know what terminates an opportunity in order to calculate its cycle length. Opportunities that have not been terminated should not have a cycle length. Or am I missing anything?
Best
Darek- AnonymousNot applicable
Hi Darek,
What I'm after is the cycle length per stage, so the average number of days an opportunity is staying in a specific stage.
Many Thanks
- AnonymousNot applicable
Hi,
I finally find a way to do it I think there is others way to simplify the formula but it's working.
Many thanks for your contribution.
Days Challenge = IF ( 'Opportunity Field History'[OldValue] = "Challenge"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[Field] = "created" ); 'Opportunity Field History'[CreatedDate]; DAY ); IF ( 'Opportunity Field History'[OldValue] = "Closing"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[NewValue] = "Closing" ); 'Opportunity Field History'[CreatedDate]; DAY ); IF ( 'Opportunity Field History'[OldValue] = "Solution"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[NewValue] = "Solution" ); 'Opportunity Field History'[CreatedDate]; DAY ); IF ( 'Opportunity Field History'[OldValue] = "Value"; DATEDIFF ( CALCULATE ( MIN ( 'Opportunity Field History'[CreatedDate] ); ALLEXCEPT ( 'Opportunity Field History'; 'Opportunity Field History'[OpportunityId] ); 'Opportunity Field History'[NewValue] = "Value" ); 'Opportunity Field History'[CreatedDate]; DAY ); BLANK () ) ) ) )