Basic Measure Drilling
One of the unique things about Looker is that it connects directly to your database. That means you can always access the freshest data, and you can always drill down to the most granular level available. So, while of course you can see yearly or monthly summaries, Looker also gives you the option of drilling in to the day, hour, or even second instantly.
We can see this below, where we start with an all-time category overview, then drill to a monthly sales chart of a single category (Jeans).
Drilling Can be More Powerful in Several Ways
Looker's web-native, modern architecture means it is possible to do far more than just drill from one level to the next-most granular. We can build out any custom drill path your heart desires, no matter how sophisticated, with just a few simple parameters.
To demonstrate this, we show sample code below for some of the most common drill patterns, including:
- Sorting and limiting (for example, show me the top 20)
- Pivoting data
- Drilling to advanced visualizations with trendlines
- Drilling to any custom visualization and building it using D3 and Javascript
- Drilling to a table calculation with conditional formatting
Adding Custom Limits (up to 5000)
To show only the first 20 results, you can use the link
parameter and set the url
subparameter to "{{ link }}&limit=20"
, as in the following LookML code.
measure: returned_count { type: count_distinct sql: ${id} ;; filters: [is_returned: "yes"] drill_fields: [detail*] link: { label: "Explore Top 20 Results" url: "{{ link }}&limit=20" } } set: detail { fields: [id, order_id, status, created_date, sale_price, products.brand, products.item_name, users.email] }
This allows users to drill into the top 20 results by selecting Explore Top 20 Results on the drill menu:
Adding Sorts
You can also display the 20 results by Sale Price, as in the following example:
measure: returned_count { type: count_distinct sql: ${id} ;; filters: [is_returned: "yes"] drill_fields: [detail*] link: { label: "Explore Top 20 Results by Sale Price" url: "{{ link }}&sorts=order_items.sale_price+desc&limit=20" } } set: detail { fields: [id, order_id, status, created_date, sale_price, products.brand, products.item_name, users.email] }
This allows users to drill into the top 20 results by sale price by selecting Explore Top 20 Results by Sale Price on the drill menu:
Adding Pivots
You can add a pivot on the Age Tier field to show the year and gross margin percentage tier for each age group when a user selects Total Sale Price by Month for Each Age Tier on the drill menu:
measure: order_count { type: count_distinct drill_fields: [created_year, item_gross_margin_percentage_tier, users.age_tier, total_sale_price] link: { label: "Total Sale Price by Month for Each Age Tier" url: "{{link}}&pivots=users.age_tier" } sql: ${order_id} ;; }
Using Visual Drilling
As of Looker 21.4, the new dashboard experience supports visual drilling using the link
parameter without the need to enable the Visual Drilling Labs feature.
This has all been good for looking at data, but what if we want to visualize it? Looker has a Labs feature called Visual Drilling, which allows users to drill into a Look or Explore chart or legacy dashboard tile. With zero customization and a limited drill set, we can get lots of different visualizations.
As an example, to show how many items have been sold by day, you can create a measure that drills into the Created Date and Total Sale Price fields:
measure: count { type: count_distinct sql: ${id} ;; drill_fields: [created_date, total_sale_price] }
Visual Drilling was based on a simple approach; the next question you may be asking is, "How can I control what visualization Looker shows?"
Make a Scatter Plot with Limit and Moving Average
Suppose you want to show how many items have been sold by day, with a 30-day moving average:
To do this, you can define the following LookML:
measure: count { type: count_distinct sql: ${id} ;; drill_fields: [created_date, total_sale_price] link: { label: "Show as scatter plot" url: " {% assign vis_config = '{ \"stacking\" : \"\", \"show_value_labels\" : false, \"label_density\" : 25, \"legend_position\" : \"center\", \"x_axis_gridlines\" : true, \"y_axis_gridlines\" : true, \"show_view_names\" : false, \"limit_displayed_rows\" : false, \"y_axis_combined\" : true, \"show_y_axis_labels\" : true, \"show_y_axis_ticks\" : true, \"y_axis_tick_density\" : \"default\", \"y_axis_tick_density_custom\": 5, \"show_x_axis_label\" : false, \"show_x_axis_ticks\" : true, \"x_axis_scale\" : \"auto\", \"y_axis_scale_mode\" : \"linear\", \"show_null_points\" : true, \"point_style\" : \"circle\", \"ordering\" : \"none\", \"show_null_labels\" : false, \"show_totals_labels\" : false, \"show_silhouette\" : false, \"totals_color\" : \"#808080\", \"type\" : \"looker_scatter\", \"interpolation\" : \"linear\", \"series_types\" : {}, \"colors\": [ \"palette: Santa Cruz\" ], \"series_colors\" : {}, \"x_axis_datetime_tick_count\": null, \"trend_lines\": [ { \"color\" : \"#000000\", \"label_position\" : \"left\", \"period\" : 30, \"regression_type\" : \"average\", \"series_index\" : 1, \"show_label\" : true, \"label_type\" : \"string\", \"label\" : \"30 day moving average\" } ] }' %} {{ link }}&vis_config={{ vis_config | encode_uri }}&toggle=dat,pik,vis&limit=5000" } }
What's Happening Here?
We are passing Looker's visualization settings into a URL using Liquid variables. These settings will control the visual drilling window. Below are some more examples.
Pivots and Stacked Line Chart
You can create a stacked line chart showing the Total Sale Price for each Month Number, pivoting on Created Year:
This is the LookML for the above example:
measure: total_sale_price { type: sum value_format_name: usd sql: ${sale_price} ;; drill_fields: [total_sale_price, created_month_name, created_year] link: { label: "Show as stacked line" url: " {% assign vis_config = '{ \"stacking\" : \"normal\", \"legend_position\" : \"right\", \"x_axis_gridlines\" : false, \"y_axis_gridlines\" : true, \"show_view_names\" : false, \"y_axis_combined\" : true, \"show_y_axis_labels\" : true, \"show_y_axis_ticks\" : true, \"y_axis_tick_density\" : \"default\", \"show_x_axis_label\" : true, \"show_x_axis_ticks\" : true, \"show_null_points\" : false, \"interpolation\" : \"monotone\", \"type\" : \"looker_line\", \"colors\": [ \"#5245ed\", \"#ff8f95\", \"#1ea8df\", \"#353b49\", \"#49cec1\", \"#b3a0dd\" ], \"x_axis_label\" : \"Month Number\" }' %} {{ link }}&vis_config={{ vis_config | encode_uri }}&sorts=order_items.created_year+asc,order_items.created_month_name+asc&pivots=order_items.created_year&toggle=dat,pik,vis&limit=500&column_limit=15" } # NOTE the &pivots= }
Drilling to a Custom Visualization
You can allow users to drill to a custom visualization:
To do this, include the custom visualization in the visualization settings you specify for the link
parameter:
measure: average_shipping_time { type: average value_format_name: decimal_2 sql: ${shipping_time} ;; drill_fields: [products.category, users.age_tier, average_shipping_time] link: { label: "See as custom viz (heatmap)" url: " {% assign vis_config = '{ \"minColor\" : \"#d6d6d6\", \"maxColor\" : \"#9a33e3\", \"dataLabels\" : false, \"custom_color_enabled\" : false, \"custom_color\" : \"forestgreen\", \"show_single_value_title\": true, \"show_comparison\" : false, \"comparison_type\" : \"value\", \"comparison_reverse_colors\": false, \"show_comparison_label\" : true, \"show_view_names\" : true, \"show_row_numbers\" : true, \"truncate_column_names\" : false, \"hide_totals\" : false, \"hide_row_totals\" : false, \"table_theme\" : \"editable\", \"limit_displayed_rows\" : false, \"enable_conditional_formatting\": false, \"conditional_formatting_include_totals\": false, \"conditional_formatting_include_nulls\": false, \"type\" : \"highcharts_heatmap\", \"stacking\" : \"\", \"show_value_labels\" : false, \"label_density\" : 25, \"legend_position\" : \"center\", \"x_axis_gridlines\" : false, \"y_axis_gridlines\" : true, \"y_axis_combined\" : true, \"show_y_axis_labels\" : true, \"show_y_axis_ticks\" : true, \"y_axis_tick_density\" : \"default\", \"y_axis_tick_density_custom\": 5, \"show_x_axis_label\" : true, \"show_x_axis_ticks\" : true, \"x_axis_scale\" : \"auto\", \"y_axis_scale_mode\" : \"linear\", \"ordering\" : \"none\", \"show_null_labels\" : false, \"show_totals_labels\" : false, \"show_silhouette\" : false, \"totals_color\" : \"#808080\", \"series_types\" : {}, \"hidden_fields\" : [ \"order_items.count\", \"order_items.total_sale_price\" ] }' %} {{ link }}&vis_config={{ vis_config | encode_uri }}&sorts=products.category+asc,users.age_tier+asc&toggle=dat,pik,vis&limit=5000" } }
Drilling to a Table Calculation with Conditional Formatting
You can also allow users to drill to a table calculation with conditional formatting:
This can be defined in LookML as in the following example:
measure: total_sale_price { type: sum value_format_name: usd sql: ${sale_price} ;; drill_fields: [created_month, users.gender, total_sale_price] link: { label: "Table Calc & Total" url: " {% assign table_calc = '[ { \"table_calculation\": \"percent_of_total\", \"label\": \"Percent of Total\", \"expression\": \"${order_items.total_sale_price:row_total} / sum(${order_items.total_sale_price:row_total})\", \"value_format\": null, \"value_format_name\": \"percent_2\", \"_kind_hint\": \"supermeasure\", \"_type_hint\": \"number\" }, { \"table_calculation\": \"growth_rate\", \"label\": \"Growth Rate\", \"expression\": \"${order_items.total_sale_price} / offset(${order_items.total_sale_price},1) - 1\", \"value_format\": null, \"value_format_name\": \"percent_2\", \"_kind_hint\": \"measure\", \"_type_hint\": \"number\" }]' %} {% assign vis_config = '{ \"type\": \"table\", \"show_view_names\": false, \"show_row_numbers\": false, \"truncate_column_names\": false, \"table_theme\": \"gray\", \"enable_conditional_formatting\": true, \"conditional_formatting\": [ { \"type\": \"low to high\", \"value\": null, \"background_color\": null, \"font_color\": null, \"palette\": { \"name\": \"Custom\", \"colors\": [ \"#FFFFFF\", \"#6e00ff\" ]}, \"bold\": false, \"italic\": false, \"strikethrough\": false, \"fields\": [ \"growth_rate\" ]},{ \"type\": \"low to high\", \"value\": null, \"background_color\": null, \"font_color\": null, \"palette\": { \"name\": \"Custom\", \"colors\": [ \"#FFFFFF\", \"#88ff78\" ]}, \"bold\": false, \"italic\": false, \"strikethrough\": false, \"fields\": [ \"percent_of_total\" ]}]}' %} {{link}}&total=on&row_total=right&dynamic_fields={{ table_calc | replace: ' ', '' | encode_uri }}&pivots=users.gender&vis_config={{ vis_config | replace: ' ', '' | encode_uri }}" } }