Forum Discussion
Cohort Analysis Revenue
- Anonymous5 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
Anonymous
2 things to address. First, if my code does what you need, please mark the answer as the answer. Kudos would be appreciated as well if you don't mind. Second, you should never create calculated columns in your model via DAX. There are many reasons behind it, several chapters in a book could be written. Always use your source or Power Query to calculate columns and then load them into the model. ALWAYS, if you want to have a peace of mind... that is.
I ended up solving the memory issue by removing the 2 calculated columns from my fact table and adding a calculated "Cohort Date" table to filter my partners table. Just needed to swap out one of the SELECTEDVALUE columns in your measure. The command works quickly now. Very much appreciate your help on this, have a great day!!!!
- Anonymous5 years agoNot applicable
Glad you've pulled this off. If you have a good healthy star-schema model, you can count yourself among those who are lucky enough to understand that it's the only way to do it RIGHT and have simple, fast DAX. Give yourself a pat on the back.