Percent of Total
A detailed walkthrough on how to calculate the distribution of a metric using AQL.
Introduction
The Percent of Total analysis is a commonly used reporting technique that helps understand the contribution of each row value to the overall population.
In SQL, you have to divide your calculation into multiple CTEs (Common Table Expressions) and then cross-join those together to perform Percent of Total. Whereas with AQL, you can achieve this calculation in a more concise and reusable way by focusing on the business logic rather than the technical semantics.
Recommended Prerequisites
- of_all: Exclude your metric from being evaluated against certain dimensions/grains.
High-level Flow
Suppose we want to see how each product's sales contribute to global sales.
To illustrate the calculation using AQL, we'll explore two different ways of performing it. First, we will break down the calculation into manageable steps or components. Then, we'll demonstrate how to write it as a single-line query.
Setup
To make it simple, we'll be working on the order_items modeling definition only with the following fields.
Measures and Metrics
You may see the use of measure in code snippets throughout this article. In AML, metrics defined inside data models are syntactically referred to as measure. We will use the word "metric" outside of code snippets to avoid confusions. Read more about Metrics here
We'll show two equivalent ways to define percent_of_total. Pick whichever style suits the metric:
Model order_items {
...
dimension id {...}
dimension product {...}
dimension amount {...}
measure total_sales {
label: 'Total Sales'
type: 'number'
definition: @aql sum(order_items.amount) ;;
}
measure _total_sales_of_all {
label: 'Total Sales of All'
definition: @aql sum(order_items.amount) | of_all(order_items.id) ;;
hidden: true
}
measure percent_of_total {
label: "Percent of Total"
type: 'number'
definition: @aql (order_items.total_sales*1.0) / order_items._total_sales_of_all ;;
}
}Implementation
1. Define the aggregation
Calculate the total sales value for each product by defining the metric total_sales
Model order_items {
...
measure total_sales {
label: 'Total Sales'
type: 'number'
definition: @aql sum(order_items.amount) ;;
}
}explore {
dimensions {
products.name
}
measures {
order_items.sales
}
}| Product | Total Sales |
|---|---|
| Chocolate 2 | 2187 |
| Samsung Tablet 2 | 101313 |
| H&M T-shirt | 25194 |
| Face Treatment | 4743 |
| Adidas Shoes | 38901 |
| Xbox | 102797 |
| Jordan Shoes | 18760 |
| Bunk Bed | 13893 |
| AFC Biscuits | 2350 |
| Sony Headphone 1 | 10665 |
| Sofa 1 | 50334 |
| Body Scrub | 211359 |
| Flour 1 | 800 |
| Converse Shoes | 40575 |
| Outdoor Sofa | 24999 |
| Bathroom Mirror | 3960 |
| Face Moisturizers | 4576 |
| Wii | 24090 |
| Cookies 2 | 6049 |
| Face Cream | 17365 |
| Nokia C1 | 87307 |
| High Cabinet | 8432 |
| Lacoste T-shirt | 21994 |
| Body Cleanser | 161143 |
| Reebox Shoes | 21231 |
| Ticwatch 1 | 74019 |
| KitKat 1 | 1037 |
2. Define the aggregation with coarser level-of-detail
Total sales of all order items is also needed to be used as denominator for the percent of total. This metric can be definied by using of_all() metric function.
Model order_items {
...
measure total_sales {...}
measure _total_sales_of_all {
label: 'Total Sales of All'
type: 'number'
definition: @aql order_items.total_sales | of_all(order_items) ;;
}
}explore {
dimensions {
products.name
}
measures {
order_items.total_sales,
order_items._total_sales_of_all
}
}| Product | Total Sales | Total Sales of All |
|---|---|---|
| Chocolate 2 | 2187 | 4391797 |
| Samsung Tablet 2 | 101313 | 4391797 |
| H&M T-shirt | 25194 | 4391797 |
| Face Treatment | 4743 | 4391797 |
| Adidas Shoes | 38901 | 4391797 |
| Xbox | 102797 | 4391797 |
| Jordan Shoes | 18760 | 4391797 |
| Bunk Bed | 13893 | 4391797 |
| AFC Biscuits | 2350 | 4391797 |
| Sony Headphone 1 | 10665 | 4391797 |
| Sofa 1 | 50334 | 4391797 |
| Body Scrub | 211359 | 4391797 |
| Flour 1 | 800 | 4391797 |
| Converse Shoes | 40575 | 4391797 |
| Outdoor Sofa | 24999 | 4391797 |
| Bathroom Mirror | 3960 | 4391797 |
| Face Moisturizers | 4576 | 4391797 |
| Wii | 24090 | 4391797 |
| Cookies 2 | 6049 | 4391797 |
| Face Cream | 17365 | 4391797 |
| Nokia C1 | 87307 | 4391797 |
| High Cabinet | 8432 | 4391797 |
| Lacoste T-shirt | 21994 | 4391797 |
| Body Cleanser | 161143 | 4391797 |
| Reebox Shoes | 21231 | 4391797 |
| Ticwatch 1 | 74019 | 4391797 |
| KitKat 1 | 1037 | 4391797 |
| Umbrella | 29855 | 4391797 |
| Guess Jeans | 13881 | 4391797 |
| 10 Deep T-shirt | 9398 | 4391797 |
| iPad 1 | 130244 | 4391797 |
3. Calculate the percent of total
Finally, the Percent of Total is determined by dividing the total_sales by _total_sales_of_all. Since _total_sales_of_all remains constant, the percent of total will adjust according to total_sales metric and its corresponding dimension
Model order_items {
...
measure total_sales {...}
measure _total_sales_of_all {...}
measure percent_of_total {
label: "Percent of Total"
type: 'number'
definition: @aql (order_items.total_sales*1.0) / order_items._total_sales_of_all ;;
}
}To maintain the decimal format for the percent of total metric, multiplying by 1.0 explicitly casts the left side to decimal.
explore {
dimensions {
products.name
}
measures {
order_items.total_sales,
order_items.percent_of_total
}
}| Product | Total Sales | Percent of Total |
|---|---|---|
| Chocolate 2 | 2187 | 0.0498% |
| Samsung Tablet 2 | 101313 | 2.307% |
| H&M T-shirt | 25194 | 0.5737% |
| Face Treatment | 4743 | 0.1080% |
| Adidas Shoes | 38901 | 0.8858% |
| Xbox | 102797 | 2.3407% |
| Jordan Shoes | 18760 | 0.4272% |
| Bunk Bed | 13893 | 0.3163% |
| AFC Biscuits | 2350 | 0.0535% |
| Sony Headphone 1 | 10665 | 0.2428% |
| Sofa 1 | 50334 | 1.1461% |
| Body Scrub | 211359 | 4.8126% |
| Flour 1 | 800 | 0.0182% |
| Converse Shoes | 40575 | 0.9239% |
| Outdoor Sofa | 24999 | 0.5692% |
| Bathroom Mirror | 3960 | 0.0902% |
| Face Moisturizers | 4576 | 0.1042% |
| Wii | 24090 | 0.5485% |
| Cookies 2 | 6049 | 0.1377% |
| Face Cream | 17365 | 0.3954% |
| Nokia C1 | 87307 | 1.9879% |
| High Cabinet | 8432 | 0.1919% |
| Lacoste T-shirt | 21994 | 0.5008% |
| Body Cleanser | 161143 | 3.6692% |
| Reebox Shoes | 21231 | 0.4834% |
| Ticwatch 1 | 74019 | 1.6854% |
| KitKat 1 | 1037 | 0.2361% |
| Umbrella | 29855 | 0.6798% |
| Guess Jeans | 13881 | 0.3161% |
| 10 Deep T-shirt | 9398 | 0.2140% |
| Gucci T-shirt | 7826 | 0.1782% |
| Nike Shoes | 62529 | 1.4238% |
| Chips 2. Dried Fruit | 5435 | 0.1238% |
| Plant Pot | 21953 | 0.5000% |
| Sony Smart TV | 70961 | 1.6158% |
| Wrangler Jeans | 8562 | 0.1950% |
| Baking Soda 1 | 4536 | 0.1033% |
| iPhone X | 72053 | 1.6406% |
| TV Stand | 53058 | 1.2081% |
| Dining Chair 1 | 31056 | 0.7071% |
| Xbox 360 | 90142 | 2.0525% |
| Playstation 4 | 39596 | 0.9016% |
| Single Bed | 2340 | 0.0533% |
| Oil 2 | 26525 | 0.6040% |
| Apple Watch 1 | 197375 | 4.4942% |
| Samsung Smart TV | 72128 | 1.6423% |
| iPhone 7 | 77086 | 1.7552% |
| Kindle Fire 2 | 69054 | 1.5723% |
| Kindle Fire 1 | 203271 | 4.6284% |
| BlackBerry Key 1 | 146508 | 3.3359% |
| Samsung Bluetooth Earphone | 84791 | 1.9307% |
| Essence Lotions | 18550 | 0.4224% |
| Flour 2 | 12157 | 0.2768% |
| Oil 1 | 17379 | 0.3957% |
| Face Mask | 11484 | 0.2615% |
| LG Smart TV | 67865 | 1.5453% |
| Samsung Galaxy 2 | 127246 | 2.8974% |
| Bedroom Mirror | 23857 | 0.5432% |
| Fireplace | 73942 | 1.6836% |
| iPad 2 | 108816 | 2.4777% |
| Chips 1 | 7673 | 0.1747% |
| Panasonic Smart TV | 129750 | 2.9544% |
| Outdoor Lighting | 11568 | 0.2634% |
| Bathroom Slippers | 7764 | 0.1768% |
| Samsung Tablet 1 | 50690 | 1.1542% |
| Shower curtains | 8347 | 0.1901% |
| Towels | 4363 | 0.0993% |
| Playstation 3 | 32682 | 0.7442% |
| Sony Headphone 2 | 35732 | 0.8136% |
| Samsung Galaxy 1 | 45974 | 1.0468% |
| Smart Lighting | 14635 | 0.3332% |
4. Percent of total in one line
The entire steps described above can also be straightforwardly condensed into one single-line expression.
Model order_items {
...
measure percent_of_total {
label: "Percent of Total"
type: 'number'
definition: @aql (sum(order_items.amount) * 1.0) / (sum(order_items.amount) | of_all(order_items));;
}
}explore {
dimensions {
products.name
}
measures {
order_items.total_sales,
order_items.percent_of_total
}
}| Product | Total Sales | Percent of Total |
|---|---|---|
| Chocolate 2 | 2187 | 0.0498% |
| Samsung Tablet 2 | 101313 | 2.307% |
| H&M T-shirt | 25194 | 0.5737% |
| Face Treatment | 4743 | 0.1080% |
| Adidas Shoes | 38901 | 0.8858% |
| Xbox | 102797 | 2.3407% |
| Jordan Shoes | 18760 | 0.4272% |
| Bunk Bed | 13893 | 0.3163% |
| AFC Biscuits | 2350 | 0.0535% |
| Sony Headphone 1 | 10665 | 0.2428% |
| Sofa 1 | 50334 | 1.1461% |
| Body Scrub | 211359 | 4.8126% |
| Flour 1 | 800 | 0.0182% |
| Converse Shoes | 40575 | 0.9239% |
| Outdoor Sofa | 24999 | 0.5692% |
| Bathroom Mirror | 3960 | 0.0902% |
| Face Moisturizers | 4576 | 0.1042% |
| Wii | 24090 | 0.5485% |
| Cookies 2 | 6049 | 0.1377% |
| Face Cream | 17365 | 0.3954% |
| Nokia C1 | 87307 | 1.9879% |
| High Cabinet | 8432 | 0.1919% |
| Lacoste T-shirt | 21994 | 0.5008% |
| Body Cleanser | 161143 | 3.6692% |
| Reebox Shoes | 21231 | 0.4834% |
| Ticwatch 1 | 74019 | 1.6854% |
| KitKat 1 | 1037 | 0.2361% |
| Umbrella | 29855 | 0.6798% |
| Guess Jeans | 13881 | 0.3161% |
| 10 Deep T-shirt | 9398 | 0.2140% |
| Gucci T-shirt | 7826 | 0.1782% |
| Nike Shoes | 62529 | 1.4238% |
| Chips 2. Dried Fruit | 5435 | 0.1238% |
| Plant Pot | 21953 | 0.5000% |
| Sony Smart TV | 70961 | 1.6158% |
| Wrangler Jeans | 8562 | 0.1950% |
| Baking Soda 1 | 4536 | 0.1033% |
| iPhone X | 72053 | 1.6406% |
| TV Stand | 53058 | 1.2081% |
| Dining Chair 1 | 31056 | 0.7071% |
| Xbox 360 | 90142 | 2.0525% |
| Playstation 4 | 39596 | 0.9016% |
| Single Bed | 2340 | 0.0533% |
| Oil 2 | 26525 | 0.6040% |
| Apple Watch 1 | 197375 | 4.4942% |
| Samsung Smart TV | 72128 | 1.6423% |
| iPhone 7 | 77086 | 1.7552% |
| Kindle Fire 2 | 69054 | 1.5723% |
| Kindle Fire 1 | 203271 | 4.6284% |
| BlackBerry Key 1 | 146508 | 3.3359% |
| Samsung Bluetooth Earphone | 84791 | 1.9307% |
| Essence Lotions | 18550 | 0.4224% |
| Flour 2 | 12157 | 0.2768% |
| Oil 1 | 17379 | 0.3957% |
| Face Mask | 11484 | 0.2615% |
| LG Smart TV | 67865 | 1.5453% |
| Samsung Galaxy 2 | 127246 | 2.8974% |
| Bedroom Mirror | 23857 | 0.5432% |
| Fireplace | 73942 | 1.6836% |
| iPad 2 | 108816 | 2.4777% |
| Chips 1 | 7673 | 0.1747% |
| Panasonic Smart TV | 129750 | 2.9544% |
| Outdoor Lighting | 11568 | 0.2634% |
| Bathroom Slippers | 7764 | 0.1768% |
| Samsung Tablet 1 | 50690 | 1.1542% |
| Shower curtains | 8347 | 0.1901% |
| Towels | 4363 | 0.0993% |
| Playstation 3 | 32682 | 0.7442% |
| Sony Headphone 2 | 35732 | 0.8136% |
| Samsung Galaxy 1 | 45974 | 1.0468% |
| Smart Lighting | 14635 | 0.3332% |
Level of Detail Patterns
Worked examples of Level of Detail in AQL: nested aggregation, dimensionalize(), customer segmentation flags, and is_at_level() for per-level percentages.
Period Comparison
AQL techniques for comparing metrics across time periods in code, covering relative offsets, fixed date ranges, granularity control, and CAGR calculations.