Forum Discussion

nathov's avatar
nathov
Regular Visitor
6 years ago
Solved

Intersection - Retention Rate

I need to create a graph to show customer retention rate. I'll give an example of the rational behind this KPI: If I want the retention rate for April this year, I need to look at all the differe...
  • Anonymous's avatar
    Anonymous
    6 years ago

    Here's the measure:

     

     

    // Assumptions:
    // 1. There's a dimension that stores Customers.
    // 2. There's a fact table, Sales, that joins on
    // 	  CustomerId to Customers and Date to Calendar.
    // 3. There's a dimension Calendar which is a proper
    //	  calendar in the model (marked as such).
    
    [Retention Rate] =
    var __lastVisibleDate = LASTDATE( 'Calendar'[Date] )
    var __shouldCalculate =
    	// You can calculate the ratio only when
    	// you can go back in time 4 months from
    	// __lastVisibleDate.
    	NOT(
    		ISBLANK( NEXTDAY( DATEADD(__lastVisibleDate, -4, month ) ) )
    		&&
    		ISBLANK( DATEADD( NEXTDAY( __lastVisibleDate ), -4, month ) )
    	)
    var __result =
    	if( __shouldCalculate, 
    	
    		var __custWithPurchWithinLast3Months =
    			CALCULATETABLE(
    				VALUES( Sales[CustomerID] ),
    				DATESINPERIOD(
    					'Calendar'[Date],
    					__lastVisibleDate,
    					-3,
    					MONTH
    				)
    			)
    		var __lastVisibleDateMinus3Months =
    			dateadd( __lastVisibleDate, -3, month )
    		var __custWithPurch1MonthBeforeLast3Months =
    			CALCULATETABLE(
    				VALUES( Sales[CustomerID] ),
    				DATESINPERIOD(
    					'Calendar'[Date],
    					__lastVisibleDateMinus3Months,
    					-1,
    					MONTH
    				)
    			)
    		var __custThatBoughtInBothPeriods =
    			INTERSECT(
    				__custWithPurchWithinLast3Months,
    				__custWithPurch1MonthBeforeLast3Months
    			)
    		var __ratio =
    			DIVIDE(
    				countrows( __custThatBoughtInBothPeriods ),
    				COUNTROWS( __custWithPurchWithinLast3Months )
    			)			
    		return
    			__ratio
    	)
    RETURN
    	__result

     

     

     

    Please bear in mind that this works for ANY SELECTED PERIOD OF TIME, not only for months.

     

    Best

    D