# How to calculate a percentage based on another beastmode

Options
Member
edited March 13

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.

Tagged:

• Coach
Options

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….

** Did this solve your problem? Accept it as a solution! **

• Coach
Options

SUM(CASE WHEN COUNT([PhoneNo]) OVER (PARTITION BY [Location.ID], [PhoneNo]) > 1 THEN 1 ELSE 0 END) / COUNT(DISTINCT [PhoneNo]) * 100

** Did this solve your problem? Accept it as a solution! **

• Member
Options

That also throws a calculation error unfortunately.

• Coach
Options

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.

** Did this solve your problem? Accept it as a solution! **

• Member
Options

That also throws an error thank you for trying

• Coach
Options

@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.

** Did this solve your problem? Accept it as a solution! **

• Coach
Options

Thanks to Anna Yardley, I now know how to access the rich editor to paste code. Apologies.

** Did this solve your problem? Accept it as a solution! **

• Member
Options

Yes I removed the brackets and used `` instead

• Coach
Options

As code block…

```SUM(CASE WHEN Cumulative Sessions = 2 THEN 1 END) OVER() / SUM(CASE WHEN Cumulative Sessions = 1 THEN 1 END) OVER()
```

** Did this solve your problem? Accept it as a solution! **

• Coach
Options

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.

** Did this solve your problem? Accept it as a solution! **

• Member
Options

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 the `Repeat Customer Percentage` beastmode.

And when I double-click to copy my `Cumulative Sessions` code into the `Repeat 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.

• Coach
Options

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….

** Did this solve your problem? Accept it as a solution! **

• Member
Options

@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.

• Coach
Options

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
END
```

And 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