Modern data analysis is not just about viewing raw tables or basic aggregations, it’s about defining business-specific metrics that reflect how an organization operates. This is where custom metrics in Apache Superset become extremely powerful.
Out of the box, Superset provides standard aggregations like SUM, COUNT, and AVG. While useful, they often fall short when you need deeper insights such as customer behavior trends, operational efficiency, or performance KPIs that are unique to your domain.
Custom metrics allow you to go beyond these limitations by writing SQL-based expressions directly at the dataset level. Once defined, these metrics become reusable building blocks that can be used across multiple charts and dashboards without rewriting logic every time.
In this topic, we will explore how to create and use custom metrics in Apache Superset using a practical dataset. You will learn how simple SQL expressions can be transformed into meaningful business indicators, enabling more insightful and decision-ready dashboards.
To demonstrate the examples, let’s use following customer orders dataset.
customer_orders.csv
order_id,customer_id,order_date,delivery_date,order_status,order_value 1,C001,2026-01-01,2026-01-03,Delivered,1200 2,C002,2026-01-02,2026-01-06,Delivered,800 3,C001,2026-01-03,2026-01-06,Delivered,1500 4,C003,2026-01-04,,Cancelled,600 5,C002,2026-01-05,2026-01-09,Delivered,950 6,C004,2026-01-06,2026-01-08,Delivered,400 7,C001,2026-01-07,2026-01-10,Delivered,700 8,C003,2026-01-08,2026-01-12,Delivered,1100 9,C005,2026-01-09,,Cancelled,500 10,C006,2026-01-10,2026-01-13,Delivered,1300 11,C002,2026-01-11,2026-01-15,Delivered,900 12,C007,2026-01-12,2026-01-14,Delivered,450 13,C001,2026-01-13,2026-01-17,Delivered,1600 14,C003,2026-01-14,2026-01-18,Delivered,700 15,C008,2026-01-15,,Cancelled,650 16,C004,2026-01-16,2026-01-18,Delivered,300 17,C002,2026-01-17,2026-01-21,Delivered,1050 18,C005,2026-01-18,2026-01-20,Delivered,980 19,C001,2026-01-19,2026-01-22,Delivered,1250 20,C009,2026-01-20,,Cancelled,550 21,C006,2026-01-21,2026-01-25,Delivered,1400 22,C003,2026-01-22,2026-01-26,Delivered,820 23,C002,2026-01-23,2026-01-27,Delivered,930 24,C007,2026-01-24,2026-01-26,Delivered,470 25,C001,2026-01-25,2026-01-28,Delivered,1700 26,C004,2026-01-26,,Cancelled,600 27,C005,2026-01-27,2026-01-30,Delivered,990 28,C008,2026-01-28,2026-01-31,Delivered,750 29,C006,2026-01-29,2026-02-02,Delivered,1350 30,C009,2026-01-30,2026-02-03,Delivered,500 31,C002,2026-01-31,,Cancelled,880 32,C001,2026-02-01,2026-02-04,Delivered,1180 33,C003,2026-02-02,2026-02-06,Delivered,720 34,C004,2026-02-03,2026-02-05,Delivered,410 35,C005,2026-02-04,2026-02-08,Delivered,970 36,C006,2026-02-05,,Cancelled,1450 37,C007,2026-02-06,2026-02-09,Delivered,460 38,C008,2026-02-07,2026-02-10,Delivered,780 39,C009,2026-02-08,2026-02-11,Delivered,530 40,C010,2026-02-09,2026-02-12,Delivered,600 41,C001,2026-02-10,,Cancelled,1220 42,C002,2026-02-11,2026-02-14,Delivered,910 43,C003,2026-02-12,2026-02-16,Delivered,840 44,C004,2026-02-13,2026-02-15,Delivered,390 45,C005,2026-02-14,2026-02-18,Delivered,1010 46,C006,2026-02-15,2026-02-19,Delivered,1500 47,C007,2026-02-16,,Cancelled,480 48,C008,2026-02-17,2026-02-20,Delivered,800 49,C009,2026-02-18,2026-02-21,Delivered,560 50,C010,2026-02-19,2026-02-23,Delivered,620 51,C001,2026-02-20,2026-02-23,Delivered,1300 52,C002,2026-02-21,2026-02-25,Delivered,940 53,C003,2026-02-22,,Cancelled,700 54,C004,2026-02-23,2026-02-26,Delivered,420 55,C005,2026-02-24,2026-02-28,Delivered,1020 56,C006,2026-02-25,2026-02-28,Delivered,1550 57,C007,2026-02-26,2026-03-01,Delivered,490 58,C008,2026-02-27,2026-03-02,Delivered,810 59,C009,2026-02-28,,Cancelled,570 60,C010,2026-03-01,2026-03-04,Delivered,650 61,C001,2026-03-02,2026-03-05,Delivered,1350 62,C002,2026-03-03,2026-03-06,Delivered,960 63,C003,2026-03-04,2026-03-08,Delivered,750 64,C004,2026-03-05,,Cancelled,430 65,C005,2026-03-06,2026-03-09,Delivered,1030 66,C006,2026-03-07,2026-03-10,Delivered,1600 67,C007,2026-03-08,2026-03-11,Delivered,510 68,C008,2026-03-09,2026-03-12,Delivered,820 69,C009,2026-03-10,2026-03-13,Delivered,590 70,C010,2026-03-11,,Cancelled,680 71,C001,2026-03-12,2026-03-15,Delivered,1400 72,C002,2026-03-13,2026-03-16,Delivered,980 73,C003,2026-03-14,2026-03-18,Delivered,760 74,C004,2026-03-15,2026-03-17,Delivered,440 75,C005,2026-03-16,,Cancelled,1040 76,C006,2026-03-17,2026-03-20,Delivered,1650 77,C007,2026-03-18,2026-03-21,Delivered,520 78,C008,2026-03-19,2026-03-22,Delivered,830 79,C009,2026-03-20,2026-03-23,Delivered,600 80,C010,2026-03-21,2026-03-24,Delivered,690 81,C001,2026-03-22,,Cancelled,1450 82,C002,2026-03-23,2026-03-26,Delivered,990 83,C003,2026-03-24,2026-03-27,Delivered,770 84,C004,2026-03-25,2026-03-28,Delivered,450 85,C005,2026-03-26,2026-03-29,Delivered,1050 86,C006,2026-03-27,,Cancelled,1700 87,C007,2026-03-28,2026-03-31,Delivered,530 88,C008,2026-03-29,2026-04-01,Delivered,840 89,C009,2026-03-30,2026-04-02,Delivered,610 90,C010,2026-03-31,2026-04-03,Delivered,700 91,C001,2026-04-01,2026-04-04,Delivered,1500 92,C002,2026-04-02,,Cancelled,1000 93,C003,2026-04-03,2026-04-06,Delivered,780 94,C004,2026-04-04,2026-04-07,Delivered,460 95,C005,2026-04-05,2026-04-08,Delivered,1060 96,C006,2026-04-06,2026-04-09,Delivered,1750 97,C007,2026-04-07,,Cancelled,540 98,C008,2026-04-08,2026-04-11,Delivered,850 99,C009,2026-04-09,2026-04-12,Delivered,620 100,C010,2026-04-10,2026-04-13,Delivered,720
Follow below step-by-step procedure to define custom metrics.
Step 1: Upload csv file to superset.
Data -> Upload CSV to database
· Upload customer_orders.csv file
· Specify database name and schema name where you want to upload the csv file
· Set the table name as customer_orders
Expand Columns section.
· Select all the columns
· Set 'Column data types' field for this: {"order_id": "int64", "customer_id": "string", "order_date" : "datetime64[ns]", "delivery_date" : "datetime64[ns]", "order_status" : "string", "order_value" : "int64"}
Click on Upload button. Upon uploading the data, navigate to Datasets listing page, you can see customer_orders dataset listed there.
Step 2: Create custom metrics
Click on Edit action against customer_orders dataset.
Navigate to Metrics tab.
Click on ‘Add item’ button.
· Set the Metric Key as "avg_orders_per_customer"
· Set the Label as "AVG_ORDERS_PER_CUSTOMER"
· Set SQL Expression as "COUNT(order_id) / COUNT(DISTINCT customer_id)"
Click on Save button to save this metric.
Click on OK button.
Step 3: Create a chart with this Custom metric
Navigate to Charts listing page and click on + Chart button to create new chart.
Select customer_orders dataset and Big Number chart.
Click on ‘Create new chart’ button. You will be taken to the chart configuration page.
You can see the metric AVG_ORDERS_PER_CUSTOMER metric is configured under Metrics section.
Drag and drop AVG_ORDERS_PER_CUSTOMER metric to the Query -> Metrics section.
Click on Create chart button, you can be able to see average orders per customer.
That’s it, you're good to go.
Previous Next Home












No comments:
Post a Comment