1. in
How to build
10 a metric in a metric
Using BY in MAQL
with Petr Olmer, GoodData Evangelist
2. What is a metric in a metric?
example 1: average monthly revenue
There are aggregations
average from monthly numbers outer level
on two levels.
total revenue
inner level (by month)
for each month
example 2: average number of new leads in a quarter
average from quarterly numbers In most cases,
the aggregations are different.
number of new leads
for each quarter Exception:
average of averages.
GoodData in10 with Petr Olmer: How to build a metric in a metric 2
3. Solution
average monthly revenue
average from monthly numbers Average Monthly Revenue:
SELECT AVG(Monthly Revenue)
total revenue
for each month Monthly Revenue:
SELECT SUM(Revenue) BY Close Month/Year
average number of new leads in a quarter
average from quarterly numbers Average Quarterly Leads:
SELECT AVG(Quarterly Leads)
number of new leads
for each quarter Quarterly Leads:
SELECT COUNT(Lead) BY Created Quarter/Year
GoodData in10 with Petr Olmer: How to build a metric in a metric 3
4. Solution in detail
Average Monthly Revenue:
SELECT AVG(Monthly Revenue)
Outer metric is an aggregation of the inner metric.
Monthly Revenue:
SELECT SUM(Revenue) BY Close Month/Year
Inner metric uses BY to define the aggregation level.
GoodData in10 with Petr Olmer: How to build a metric in a metric 4
5. Why BY?
You need to define the border between the inner aggregation and the outer one.
inner SUM outer AVG
BY Month
Without BY, the inner SUM would not know
where to stop and hand over to the outer AVG.
GoodData in10 with Petr Olmer: How to build a metric in a metric 5
6. Behind the scenes
inner SUM outer AVG
BY Month
Nov 2010 $450K
Dec 2010 $580K
Jan 2011 $320K $400K
Feb 2011 $360K
March 2011 $430K
April 2011 $260K
your
datamart behind the scenes report
GoodData in10 with Petr Olmer: How to build a metric in a metric 6
7. Behind the scenes
You’ve asked for one number and that’s what you get:
Average monthly revenue
The monthly report is computed but you cannot see it.
Nov 2010 $450K
Dec 2010 $580K
Jan 2011 $320K $400K
Feb 2011 $360K
March 2011 $430K
April 2011 $260K
your
datamart report behind the scenes report
GoodData in10 with Petr Olmer: How to build a metric in a metric 7
8. Automatic (in-report) BY
You don’t need to use BY when the attribute is in the report.
Month/Year Revenue Monthly Revenue Revenue:
Nov 2010 $450K $450K SELECT SUM(Revenue)
Dec 2010 $580K $580K
Jan 2011 $320K $320K Monthly Revenue:
Feb 2011 $360K $360K SELECT SUM(Revenue) BY Month/Year
March 2011 $430K $430K
April 2011 $260K $260K
automatic BY
Both metrics return the same numbers
because Month/Year attribute is in the report.
BY says: Include this attribute in the
computation. But it’s already there!
GoodData in10 with Petr Olmer: How to build a metric in a metric 8
9. BY and BY
You can include more than one attribute in the BY clause.
SELECT SUM(Revenue) BY Month/Year, Department
It will return a number for each month and department.
You can have a metric in a metric in a metric in a...
Best Region Leads: one number only
SELECT MAX(Average Monthly/Region Leads)
Average Monthly/Region Leads: one number for each region
SELECT AVG(Month/Region Leads) BY Region
Month/Region Leads: one number for each month and region
SELECT COUNT(Lead) BY Month/Year, Region
GoodData in10 with Petr Olmer: How to build a metric in a metric 9
10. Off you go...
Find the border.
average from region numbers
total revenue
for each region
Put BY into the inner metric.
SELECT SUM(Revenue) BY Region
GoodData in10 with Petr Olmer: How to build a metric in a metric 10