+1 (726) 227-2971

Percent of Total, Running Totals, and Percentiles in LookML: Built-In Measure Types That Replace Table Calculations

Share-of-total, cumulative, and median metrics are among the most requested in any BI project. Many teams build them as table calculations in each Explore, so the logic is copied from dashboard to dashboard. LookML has built-in measure types for these, and defining them once in the model keeps numbers consistent.

What you will build

On an order_items view you will add:

  • a share-of-total revenue measure
  • a running total of revenue
  • a median and a 90th-percentile order value

1. Start with a base measure

The window-style types reference another measure, so define that first.

view: order_items {
  measure: total_revenue {
    type: sum
    sql: ${sale_price} ;;
    value_format_name: usd_0
  }
}

2. Percent of total

measure: revenue_percent_of_total {
  type: percent_of_total
  sql: ${total_revenue} ;;
  direction: "column"
  value_format_name: percent_1
}

By default the direction is column, so each row is divided by the column total. If you pivot, set direction: "row" to get the share across the pivot instead. The denominator is the total of the returned rows. If a row limit truncates the result, shares will not add up to 100% of the whole dataset. Raise the row limit or check the totals before trusting the numbers.

3. Percent of previous

measure: revenue_vs_previous_row {
  type: percent_of_previous
  sql: ${total_revenue} ;;
  value_format_name: percent_1
}

This compares each row with the row above it, so the sort order matters. It works well with a date dimension sorted ascending, and badly with a sort by value.

4. Running total

measure: cumulative_revenue {
  type: running_total
  sql: ${total_revenue} ;;
  direction: "column"
}

Running totals accumulate down the rows in the current sort order. Sort by a date field ascending, or the result will not be meaningful. Running totals are only valid on measures that can be summed, so base them on sum or count, not on average.

5. Median and percentile

measure: median_order_value {
  type: median
  sql: ${sale_price} ;;
}

measure: p90_order_value {
  type: percentile
  percentile: 90
  sql: ${sale_price} ;;
}

These compile to your database's native percentile functions, so support varies by dialect. Check the generated SQL in the Explore's SQL tab. On large tables, percentiles are expensive, so consider an aggregate_table or a PDT if they are queried often. Medians also suffer from join fanout. Looker's symmetric aggregates do not apply to them, so aggregate at the right grain first.

6. Test it

  1. Validate the LookML.
  2. Open the Explore, select a dimension such as product_category, and add total_revenue and revenue_percent_of_total.
  3. Confirm that the percentages sum to 100% for the unfiltered result.
  4. Add a filter and a row limit and observe how the denominator changes.
  5. Open the SQL tab to see how the percentile is compiled.

Common pitfalls

  • Window types reference measures, not dimensions. Point sql at a measure.
  • Totals depend on the result set. Filters and row limits change the denominator.
  • Sort order drives running totals and percent of previous.
  • Different dashboards, different answers. If you need a fixed denominator, such as share of the whole company regardless of filters, use a derived table or a separate measure with a filtered scope instead.

When to use table calculations instead

Table calculations remain the right tool for one-off analysis. For any metric that appears in more than one dashboard, put it in the model so it has one definition, one label, and one description.

If your team needs help moving ad-hoc calculations into a governed LookML model, Vistelio's senior Looker developers can review your project and build the measures with you.