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
// 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
- Anonymous5 years agoNot applicable
Thanks, daxer!! This is a much simpler measure for me to understand and accomplishes the exact same result in my sample data set. Unfortunately, I still get the memory error.
I have to assume this is a problem with the calculated columns I added to my model at this point (2 in DIM table, 2 in fact table). I will try to set the model up differently and see if I can get this to work with my real dataset.
Also thanks for tips on the use of RELATED and SUMMARIZE...will keep that in mind moving forward.
- Anonymous5 years agoNot applicable
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.
- Anonymous5 years agoNot applicable
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!!!!