Skip to main content

Calculate Percent of Total

Alternative

This is the AQL-centric approach to calculate Percent of Total. Holistics also provides a UI-friendly solution: Percent of Total

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.

Video Tutorial

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 Holistics, 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

Model order_items {
...
dimension id {...}
dimension product {...}
dimension amount {...}

}

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
}
}

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
}
}

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
}
}

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
}
}

Let us know what you think about this document :)