How to calculate a percentage based on another beastmode
Hi
I am trying to calculate a percentage of customers who repeated, out of all customers.
Basically count(distinct repeat customers) / count(distinct all customers). This has to be done in beastmode so users can select a date and the repeat customer % will be dynamic.
This is the raw data, Cumulative sessions is used to determine if a customer was a repeat customer.
Cumulative sessions beastmode: count(PhoneNo
) over (partition by Location.ID
, PhoneNo
order by TransactionDateUTC
).
Based on the above screenshot the Repeat Customer Percentage should = 33.33%
My goal to calculate the repeat customer percent is to count the distinct phone numbers whose cumulative sessions > 1 divided by the count of all distinct phone numbers:
count(distinct case when count(PhoneNo
) over (partition by Location.ID
, PhoneNo order by TransactionDateUTC
) > 1 then PhoneNo
end)
/
count(distinct PhoneNo
) * 100
But this is throwing a calculation error.
Is there a way around this? I don't have access to referencing one beastmode inside another beastmode.
Best Answer
-
I don't have the Location.ID or TransactionDateUTC in my data sample to play with. But they look to be further quantifying the condition. I can't test this, but what about something like this….
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0
Answers
-
How about this:
SUM(CASE WHEN COUNT([PhoneNo]) OVER (PARTITION BY [Location.ID], [PhoneNo]) > 1 THEN 1 ELSE 0 END) / COUNT(DISTINCT [PhoneNo]) * 100** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
That also throws a calculation error unfortunately.
0 -
Hmm. Okay, maybe we can simplify.
SUM(CASE WHEN COUNT([PhoneNo]) > 1 THEN 1 ELSE 0 END) / COUNT(DISTINCT [PhoneNo]) * 100- We count the instances where a phone number appears more than once (indicating a repeat customer).
- We divide this count by the total count of distinct phone numbers (representing all customers).
- Finally, we multiply by 100 to obtain the percentage.
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
That also throws an error thank you for trying
0 -
@nickcorona - Did you do it as typed, or did you change the [] to back ticks? I typed brackets because I don't know how to make back ticks show up in these comments.
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
Thanks to Anna Yardley, I now know how to access the rich editor to paste code. Apologies.
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
Yes I removed the brackets and used `` instead
0 -
As code block…
SUM(CASE WHEN Cumulative Sessions = 2 THEN 1 END) OVER() / SUM(CASE WHEN Cumulative Sessions = 1 THEN 1 END) OVER()
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
Sorry….should have explained my answer.
When I review the way you have the data, you only want to count the repeat customer once per each customer that is repeating. We know they repeat when your cumulative sessions goes above 1. Thus we only check for 2 and count that. In my code, I am using SUM since my condition is "THEN 1".
This method ensures the repeating customer is counted only once for each. And the denominator is based on your cumulative sessions being a 1.
What's a little odd here is that we're putting this in a field on the end of the table. It's really sort of a value to display after the table. But its your data so I don't know the goal.** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
Thank you for your efforts still. That was a creative way to think of a solution to count only the '2' cumulative sessions. This unfortunately won't work because
Cumulative Sessions
is a beastmode function which calculates based on a user selected date range so I can't directly reference it's name inside theRepeat Customer Percentage
beastmode.And when I double-click to copy my
Cumulative Sessions
code into theRepeat Customer Percentage
code domo throws another calculation error when trying to validate the function:I also tried partitioning the two sums by location.id and it still would not validate. Starting to think this may just be a limitation of domo.
Also, you are right this data would not make sense in the table. It is going in a gauge card, I just provided the table for context into the data.
0 -
I don't have the Location.ID or TransactionDateUTC in my data sample to play with. But they look to be further quantifying the condition. I can't test this, but what about something like this….
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
@ArborRose FYI I took this same question to the Brilliance Bar at Domopalooza and multiple experts were stumped and deemed this was not possible with the current technology around window functions.
Thanks for your effort.
0 -
It's been a minute. I have to re-read the problem to see what it is you are trying to solve. Then I need to recreate a data sample. You say, "It is going in a gauge card, I just provided the table for context into the data."
So you want a date filter, a table of data, and a gauge showing the percentage of repeat customers over the number of new customers. In your original screenshot you say 33.33%. But 4 repeat customers over 3 new customers would be 1.33.
If we create a field on the table called Repeat Customer Indicator with the following formula:CASE
WHEN Customer Status = 'Repeat Customer' THEN 1
ELSE 0
ENDAnd we create a gauge card with the following formula:
SUM(Repeat Customer Indicator) / COUNT(DISTINCT PhoneNo)
And create a filter card for date filtering. You would have results like this:
With the gauge representing the percent of repeat customers / new customers.
4/3 = 1.33
3/3 = 1
1/3 = .33** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **1
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 616 Datasets
- 2.2K Magic ETL
- 3.8K Visualize
- 2.5K Charting
- 731 Beast Mode
- 55 App Studio
- 40 Variables
- 682 Automate
- 175 Apps
- 451 APIs & Domo Developer
- 46 Workflows
- 10 DomoAI
- 35 Predict
- 14 Jupyter Workspaces
- 21 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 122 Manage
- 119 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 107 Community Announcements
- 4.8K Archive