Willie Doyle
11/18/2023, 1:26 AMFrancisco Antunes
11/18/2023, 2:03 AMOrder Amount Total (inc. 0)
metric.
Both your conditions on the final metric depend on this value, and I think that when either of them returns a null you get the result we’re seeing on the screenshots.
My first suspicion is where the IFNULL statement is located right now. May I suggest trying something like this instead:
1. Start by creating a metric without the IFNULL, let’s call it `Order Amount Total`:
SELECT SUM(Total) WHERE Order Status NOT IN (canceled, declined)
2. Then, create an IFNULL metric that envelopes the entire metric, like so:
SELECT IFNULL (Order Amount Total, 0) *1
I ran some tests, and this ensures that the IFNULL calculations are done after the WHERE filters are applied.
Based on my tests, I think that the metric was giving you nulls due to the WHERE not being “included” in the IFNULL (i.e., it would not check if the conditional would be a null, only the SUM). Kindly give that a shot and let me know if it works out better!Willie Doyle
11/20/2023, 6:55 PMFrancisco Antunes
11/20/2023, 7:29 PMChurn - At Risk Locations
metric; or if a statement such as SELECT Order Amount Total (inc. 0) BY Location ID) = 0
actually gives out results. The goal is to identify where the failure is occurring.
I would also make sure that all of these objects are properly connected in the LDM.Michal Hauzírek
11/20/2023, 10:05 PMCOUNT(Customer,Order ID),0) FOR PREVIOUS (Month/Year (Order Date)
and then there are conditions to filter it only to months where there are no current orders and at the same time are some orders in previous month.
I used BY Customer, Month/Year ALL OTHER
within the inner metrics for conditions so it should work well even for insights that do not have customer/location and month in them.
In my data I had orders for the same customer in different months and this metric also detects “repeated” churn risk.
One issue might be that thanks to its date ever shifting nature it will warn about churn risk also for month which is not yet in the data, but that should be possible to be filtered out.
Here is the full metric that worked for me. See the screenshots with my data if it is what you were trying to achieve:
SELECT
IFNULL( COUNT(Customer,Order ID),0) FOR PREVIOUS (Month/Year (Order Date),1 )
WHERE
(SELECT IFNULL(COUNT(Order ID),0) BY Customer, Month/Year (Order Date) ALL OTHER)=0
AND
(SELECT IFNULL(COUNT(Order ID),0) BY Customer, Month/Year (Order Date) ALL OTHER FOR PREVIOUS (Month/Year (Order Date),1) )>0
BTW in one of your metrics you are using Month
, I think you might want to use Month/Year
instead Month is really only month without a year so it might give you some weird results if your data go across more than one year.
BTW2: if you don’t care about showing 0 for those where churn risk is not detected, you can remove the very first IFNULL around the COUNTMichal Hauzírek
11/20/2023, 10:13 PM... AND Month/Year (Order Date) <= (SELECT MAX( (SELECT Month/Year (Order Date) BY Order ID) ) BY ALL OTHER WITHOUT PF)
so
SELECT
COUNT(Customer,Order ID) FOR PREVIOUS (Month/Year (Order Date),1 )
WHERE
(SELECT IFNULL(COUNT(Order ID),0) BY Customer, Month/Year (Order Date) ALL OTHER)=0
AND
(SELECT IFNULL(COUNT(Order ID),0) BY Customer, Month/Year (Order Date) ALL OTHER FOR PREVIOUS (Month/Year (Order Date),1) )>0
AND
Month/Year (Order Date) <= (SELECT MAX( (SELECT Month/Year (Order Date) BY Order ID) ) BY ALL OTHER WITHOUT PF)