Forum Discussion
Month over Month Amount Calculation for each customer by stage
- Anonymous6 years ago
Hi,
If I get it right, your customer has a status for each order he makes, based on the billing date, his first purchase date and an eventual cancellation date. This status can be new/existing/churn and can evolve over time for a customer.
In this case, I would go with a calculated column. But this scenario could be also solved using several dax measures or even other techniques. You can find the .pbix file containing what I have done.
Option 1: using a calculated column
Why would I go for a calculated column?
- better performance at query time, especially when addressing resource intensive calculations like this one that are not dynamic (for one row in Customer Billing, the value for the Customer Status would remain the same)
- less DAX code
- possibility to slice and dice on the Customer Status
Here as your status depends on the order date, I would add it to the Customer Billing table as it is the sole table containing this level of detail.
The code for 'Customer Billing'[Customer Status]
Customer Status = VAR OrderDate = Sales[Order Date] VAR FirstBillingDate = RELATED( Customer[First Billing Date] ) VAR CancellationDate = RELATED( Customer[Cancellation Notice Date] ) VAR CustomerCategory = SWITCH( TRUE(), OrderDate >= CancellationDate, "Churn", OrderDate <= (FirstBillingDate + 120), "New", "Existing" ) RETURN CustomerCategoryI hope I reproduced your business logic in the SWITCH as you intended.
You then simply need to add your field to your visual and your MoM measure that you already authored and you get:
Results using Option 1
But are the results correct? I did a quick spot check looking at the customer detail:
Are the results correct? We can check for a customer
=> We see that In July, our customer spends 1222 and is considered as Existing (first billing in 22 of Feb.)
=> Then in August, the customer is still "Existing" but spends only 1100, which is indeed 122 less than the 1222 of July
Option 2, using DAX measures
If you can't or don't want to use a calculated column, you can still address the scenario with DAX measures. One of the main cons is that you'll have to write each measure three times: one for new, one for existing and one for churn.
The measures for Sales for New would be the following:
Sales for New = VAR SalesLinesMadeByNew = FILTER( SUMMARIZE(Sales, Sales[Order Date] , Sales[CustomerKey], Customer[First Billing Date] ), Sales[Order Date] <= Customer[First Billing Date] + 120 ) VAR SalesAmountMadeByNew = CALCULATE( [Sales Amount], SalesLinesMadeByNew ) Return SalesAmountMadeByNewHere I first compute a table that I will then use as a filter.
This table is stored in the variable SalesLinesMadeByNew and contains all the values of Customer/OrderDate that are considered New.
Note that there are surely other DAX alternatives to perform this calculation.
And you can see that Option 2 yields the same results as Option 1 :
I have added the calculated column as a column header and then the different measures.
What are the performances differences?
On my machine the query to generate the report year/month with Sales + Sales MoM for each Customer Status:
- Option 1 => 60ms
- Option 2 => 240ms
Option 1 is 4x faster than Option 2
What do I do if my parameters have to change? For example I need to let the user decide what duration defines the "New Category" with a slicer (instead of using always 120 days).
Then option 1 doesn't work anymore and using DAX measures with a parameter would be imho the best option.
Best regards,
Thomas
Anonymous
Thank you so much. I updated and have the following result below.
I put the pbix file here (file link). Would you mind taking a look at it please?
I updated the calculated column, Customer Status, as you prescribed and I can see that each month has a unique status now.
However, the M2M calculation is still off. You'll see that for Customer B, for the month of June 2019, following happens:
- Existing: Current Billing=$11,513, Previous Mo Billing=0, causing M2M=$11,513
- Churn: Current Billing=0, Previous Mo Billing=$11,377, causing M2M=$(11,377)
I think the issue is with M2M calculation for the overlapping month in question.
What I was hoping to see is below (I mocked it up in Excel):
Hi @p2c,
I had a close look at your screenshots and thanks for it, very clear.
After some thinking, I am not sure that the MoM computes the wrong figure. Actually it really depends on what we want it to compute.
If we look at the customer level, so at the 3 columns on the far right, we see that the total computed is still correct:
- +136$ in june 2019 as customer went from 11 377 to 11 513
- +6$ in june 2020 as customer went from 11 153 to 11 159
If we process the way you intended in your mockup, it will mean that your New category will add sales for customers becoming new, but not substracting the sales for customers that are leaving the category. Which will yield too high results.
Here's an example, let's say we look at what happened between February and March for the New category:
- Customer A: was and stays in New. He does +500$ in March (from 1000$ to 1500$)
- Customer B: was New in February, switch to Existing in March. Was 11 000$ in February. As per your explanation in the second screenshot, it would yield 0.
- Customer C: wasn't there in February, but is New in March. He does +10 000$
=> Result : the New category grew for 10 100$ in March. We have more and more new customers... when actually we do not as we did -500$
Maybe I am reading the whole situation from the wrong angle, please tell me what's your thinking on this one.
Best,
Thomas
- pc26 years ago
Helper I
Anonymous
Thanks for your reply. I hear you that the Total M2M is correct and I agree. But, I do think the logic on M2M needs to change to get what I'm after Thomas. Please see picture below.
Is there any way to update DAX to calculate M2M so that the Prev Mo number is included in the right Status?
For June 2019, this is "Existing" status and I want both Current and Prev Mo Billing to show up here.
Same thing for June 2020, this is "Churn" status and I want both Current and Prev Mo Billing to show up here.
- pc26 years ago
Helper I
Anonymous
Thomas, I think I may have solved this. I'm doing some testing to ensure this is right though.. I'll let you know.
I've added another Calculated Column to grab Prev Mo Billing. Then, instead of using time intelligence to calculate prev mo billing, I'm summing this new column to get the Prev Mo Biling, which is then used to calculated M2M. It seems to give me what I want (see 2nd picture below).
Can you take a look and see if what I did makes sense and it should work at all times?
- pc26 years ago
Helper I
Anonymous
Thomas, my suggestion above has one flaw. If the customer does not buy a specific product in later month, the Prev Mo Billing will not be captured.
For example, if customer buys in
- Feb 2020, Product 1 for $100, Product 2 for $50,
- Mar 2020, Product 1 for 80 and no Product 2
The Prev Mo Billing calculated column for Mar 2020 will only pick up for Product 1 and Product 2 will be zero and this should be $50. This is because there is no Product 2 line in Mar 2020. 😞
Any idea how to come around this?