With our Lead Query created, next, we will create an Opportunity query. Create a SELECT statement for SFDC.STAGE.OPPORTUNITY with a table alias of “O” and include the following fields:
Id, CreatedDate, Revenue, Stage, AccountId, CloseWonDate
If you find that the first attempt does not work perfectly, use the errors to help guide you along the way. Here are some tips:
- Did you include commas after the field name?
- Did you add the table Alias correctly?
- Did you declare you get single vs double quotes correct?
- Did you remember to alias your table and fields?
Opportunity Query Result:
SELECT
O.Id,
O.CreatedDate,
O.ExpectedRevenue,
O.StageName,
O.AccountId,
O.CloseDate,
O.Type
FROM SFDC.STAGE.OPPORTUNITY O;
Revenue Pipeline
Name | Object & FIeld Origin | Date Dimension | Description | Definition |
Revenue Pipeline | Opportunity.Revenue | Oppotunity.CreatedDate | Opportunity Revenue | Total Revenue pipeline (open and closed opportunities) |
Our next metric is called “Revenue Pipeline.” The distinction of pipeline indicates that we are measuring revenue at any Opportunity stage. This is a great example where having well-written definitions and a universal understanding of metrics and facts are very important. Because there are no implied constraints or calculations required, you can simply use the Opportunity Revenue field in your query. As it turns out we don’t need to change. As it turns out, the standard field as it stands within the opportunity is sufficient and already included in our query.
Result
O.ExpectedRevenue
New Customer Won Opportunities
Name | Object & FIeld Origin | Date Dimension | Description | Definition |
New Customer Won Opportunities | Opportunity.Stage | Oppotunity.CloseWonDate | Opportunity COUNT WHERE Stage = ‘Closed Won’ AND Type = ‘New Customer’ | Count of won opportunities for new customers |
The next metric is “New Customer Won Opportunities” where you get to put your conditional expression skills to use again. This time, you have two conditions. Give it a shot on your own.
- Did you declare you get single vs double quotes correctly?
- Did you utilize AND / OR to add two conditions?
Result
CASE
WHEN O.StageName = 'Closed Won' AND O.Type = 'New Customer' then 1
ELSE 0 END "New Customer Won Opportunities"
Customers
Name | Object & FIeld Origin | Date Dimension | Description | Definition |
Customers | Opportunity.Id, Opportunity.AccountId,Opportunity.Stage | Oppotunity.CloseWonDate | DISTINCT Accounts with WHERE Opportunity Stage = ‘Closed Won’ | Distinct count of Accounts where Closed-Won Opportunities > 0 |
Our final metric is designed to measure the total number of distinct customers in Salesforce. A “Customer” in this case has a very specific definition that requires an Account to have at least 1 closed-won opportunity. Unlike the other metrics, I will share how we achieved this metric in Salesforce. I would need to build an Opportunity report that filters data for Closed-Won deals and then calculate Unique Account IDs.
Creating this metric in SQL is no different. In the spirit of breaking down a problem into its smallest parts, we need to:
- Isolate AccountIds that have a Closed-Won opportunity.
- Aggregate and COUNT the distinct AccountIds to determine our “Customers” metric.
At this point, while writing our SQL for an Opportunity we can only perform 1 of the steps. Create a field that returns the AccountIDs for Opportunities that have reached a Closed-Won stage.
Hints
- You have technically built a nearly identical field.
- Rather than a 0/1, you want to return the AccountId field.
- If the condition is false (meaning that the opportunity was not closed won), you should just return a NULL value.
Result
CASE
WHEN O.StageName = 'Closed Won' THEN O.AccountId
ELSE NULL END "Closed Won AccountIDs",
Or you can use
IFF (O.StageName = 'Closed Won' ,O.AccountId, NULL)
"Closed Won AccountIDs2"
As a recap for this field, we have not calculated the final metric, but we have created a calculated field that we need to accurately measure “Customers”. We will visit this aggregate metric at the same time that we work on the Qualified Lead to Won Opportunity Ratio.
Final Opportunity Query Result
SELECT
O.Id,
O.CreatedDate,
O.ExpectedRevenue,
O.StageName,
O.AccountId,
O.CloseDate,
O.Type,
CASE
WHEN O.StageName = 'Closed Won' AND O.Type = 'New Customer' then 1
ELSE 0 END "New Customer Won Opportunities",
CASE
WHEN O.StageName = 'Closed Won' THEN O.AccountId
ELSE NULL END "Closed Won AccountIDs",
FROM SFDC.STAGE.OPPORTUNITY O;