Overview
Looker is a modern data platform in Google Cloud that lets business users analyze and visualize data interactively. LookML developers curate the data used by business users by creating new fields, tables, and views to customize and organize data.
In Looker, LookML developers can use derived tables to create new tables that are not already defined in the underlying database. For example, as a LookML developer, you can create derived tables to summarize details from existing tables, such as order details for each order in an e-commerce dataset.
In this lab, you learn how to create both types of derived tables in LookML: SQL derived and native derived tables.
For this lab, a project called qwiklabs-ecommerce
has been created for you in LookML. This project is based on a mock e-commerce dataset containing information on orders, products, and users. You can learn more about LookML modeling in the Looker documentation.
Task 1. Create a SQL derived table summarizing details for each order
order_details.view
view: order_details {
derived_table: {
sql: SELECT
order_items.order_id AS order_id
,order_items.user_id AS user_id
,COUNT(*) AS order_item_count
,SUM(order_items.sale_price) AS order_revenue
FROM cloud-training-demos.looker_ecomm.order_items
GROUP BY order_id, user_id
;;
}
measure: count {
type: count
drill_fields: [detail*]
}
dimension: order_id {
type: number
sql: ${TABLE}.order_id ;;
}
dimension: user_id {
type: number
sql: ${TABLE}.user_id ;;
}
dimension: order_item_count {
type: number
sql: ${TABLE}.order_item_count ;;
}
dimension: order_revenue {
type: number
sql: ${TABLE}.order_revenue ;;
}
set: detail {
fields: [order_id, user_id, order_item_count, order_revenue]
}
}
Task 2. Create a native derived table summarizing details for each order
order_details_summary.view
# If necessary, uncomment the line below to include explore_source.
# include: "training_ecommerce.model.lkml"
view: order_details_summary {
derived_table: {
explore_source: order_items {
column: order_id {}
column: order_item_id {}
column: order_count {}
column: total_revenue {}
}
}
dimension: order_id {
description: ""
type: number
}
dimension: order_item_id {
description: ""
type: number
}
dimension: order_count {
description: ""
type: number
}
dimension: total_revenue {
description: ""
value_format: "$#,##0.00"
type: number
}
}
Task 3. Join a new view to an Explore
training_ecommerce.model
connection: "bigquery_public_data_looker"
# include all the views
include: "/views/*.view"
include: "/z_tests/*.lkml"
include: "/**/*.dashboard"
datagroup: training_ecommerce_default_datagroup {
# sql_trigger: SELECT MAX(id) FROM etl_log;;
max_cache_age: "1 hour"
}
persist_with: training_ecommerce_default_datagroup
label: "E-Commerce Training"
explore: order_items {
join: order_details {
type: left_outer
sql_on: ${order_items.order_id} = ${order_details.order_id};;
relationship: many_to_one
}
join: users {
type: left_outer
sql_on: ${order_items.user_id} = ${users.id} ;;
relationship: many_to_one
}
join: inventory_items {
type: left_outer
sql_on: ${order_items.inventory_item_id} = ${inventory_items.id} ;;
relationship: many_to_one
}
join: products {
type: left_outer
sql_on: ${inventory_items.product_id} = ${products.id} ;;
relationship: many_to_one
}
join: distribution_centers {
type: left_outer
sql_on: ${products.distribution_center_id} = ${distribution_centers.id} ;;
relationship: many_to_one
}
}
explore: events {
join: event_session_facts {
type: left_outer
sql_on: ${events.session_id} = ${event_session_facts.session_id} ;;
relationship: many_to_one
}
join: event_session_funnel {
type: left_outer
sql_on: ${events.session_id} = ${event_session_funnel.session_id} ;;
relationship: many_to_one
}
join: users {
type: left_outer
sql_on: ${events.user_id} = ${users.id} ;;
relationship: many_to_one
}
}