Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Cohort Analysis Revenue

Hi All - I'm stuck on developing a cohort analysis.  I'm looking for a measure that will calculate the Revenue generated 0-12 months after the cohort date according to my column values.  I've watched...
  • Anonymous's avatar
    Anonymous
    5 years ago

     

    // original
    Cohort Revenue =
    VAR CohortStartMonth =
        SELECTEDVALUE( 'Transactions'[Cohort] )
    VAR CurrentMonth =
        EOMONTH(
            CohortStartMonth,
            SELECTEDVALUE( 'Periods Out'[Value] )
        )
    VAR TransactionsInCohortPeriod =
    	// You should never attempt a filter over
    	// a fact table involving RELATED. This kills
    	// performance because RELATED uses
    	// CALCULATE behind the scenes and this
    	// in turn effects context transition which
    	// makes your system die... as you've witnessed.
        FILTER(
            'Transactions',
            RELATED( 'Date'[EndOfMonth] ) = CurrentMonth
        )
    VAR Invoices =
    	// SUMMARIZE should never be used to do
    	// any calculations in it. NEVER. There are myriads
    	// of reasons but I don't have time to explain.
    	// SUMMARIZE is safe only if you do grouping and
    	// nothing else. On top of that, it looks like this
    	// summarization is completely redundant since
    	// later on you do a SUMX over the total column
    	// anyway. Partitioning the table by PartnerID
    	// is also totally useless in this case because of
    	// the final summation.
        SUMMARIZE(
            TransactionsInCohortPeriod,
            'Transactions'[PartnerID],
            "Invoice Total",
                SUMX(
                    'Transactions',
                    Transactions[amount]
                )
        )
    RETURN
        SUMX(
            Invoices,
            [Invoice Total]
        )
    
    
    // Here's probably what you want:
    
    [Cohort Revenue] =
    VAR CurrentMonth =
        EOMONTH(
            SELECTEDVALUE( 'Transactions'[Cohort] ),
            SELECTEDVALUE( 'Periods Out'[Value] )
        )
    VAR InvoicesTotal =
    	calculate(
    		sum( Transactions[amount] ),
    		keepfilters( 
    			'Date'[EndOfMonth] = CurrentMonth 
    		)
    	)
    return
    	InvoicesTotal