It’s often desirable to capture only the top few of something–for example, to compare web traffic to our top ten countries over time, or sales of our highest-margin brands.
This can be accomplished by ranking in a derived table (view an example of doing this here), but this can also be accomplished in a simple dimension:
- dimension: is_top_ten_countries_by_quantity
type: yesno
sql: |
${country} IN
(SELECT country FROM
(
SELECT
country
FROM public.order_items AS order_items
WHERE {% condition date_paid_date %} order_items.date_paid {% endcondition %}
GROUP BY 1
ORDER BY SUM(quantity) DESC
LIMIT 10
) sub )
This example can have a templated filter for date_paid
passed in; this can be removed, or other filters (for example on category or department) could easily be added.