Derived Tables in Looker: A Guide to Creating Custom Views and Reports

Derived Tables in Looker: A Guide to Creating Custom Views and Reports

  • Post category:Looker
  • Post last modified:February 20, 2023
  • Reading time:14 mins read

Derived tables in Looker, a derived table is a query whose results are used as if it were an actual table in the database. For example, you might have a database table called orders that has many columns.

In Looker, a derived table is a query whose results are used as if it were an actual table in the database.

Power Of Derived Tables In Looker: A Game Changer For Data Analysis

If you’re a data analyst or developer working with Looker, you may have come across the term “derived tables” before. Derived tables are a powerful tool in Looker that allow you to create custom views and reports by applying SQL operations to existing tables. In this post, we’ll explain what derived tables are, why they’re useful, and how you can create them in Looker.

What are Derived Tables in Looker?

Derived tables in Looker are tables that are created within Looker by applying SQL operations to existing tables. They are temporary tables that exist only within the context of a specific Looker explore or view, and they are not stored in the database. Derived tables allow you to perform complex data transformations on your data without modifying the underlying data in your database. They are useful for performing calculations, filtering data, or joining tables together in a way that is not possible with the original tables.

Types of Derived Tables in Looker

There are several types of derived tables that you can create in Looker:

  1. Query-based derived tables: These are tables that are created by executing a SQL query against one or more existing tables in the database. Query-based derived tables are useful for performing complex calculations or filtering data based on specific criteria.
  2. Persisted derived tables: These are query-based derived tables that are saved to disk and can be accessed and reused by other views or explores in Looker. Persisted derived tables are useful for improving query performance and reducing the load on your database.
  3. Datagroups: These are derived tables that are based on data from external sources, such as Google Analytics or Salesforce. Datagroups allow you to create Looker views and explores that include data from multiple sources, without having to manually integrate the data.
  4. Templated filters: These are derived tables that allow you to create custom filters that can be applied to a view or explore. Templated filters are useful for providing users with a way to interact with the data and customize their queries.
  5. Derived tables with LookML: These are derived tables that are defined using LookML, Looker’s modeling language. With LookML, you can define derived tables using a combination of SQL and LookML code, and you can customize their behavior and properties. Derived tables with LookML can be query-based or persisted, and they can include complex transformations and aggregations.

Use Cases for Derived Tables in Looker

Here are a few use cases for derived tables in Looker:

  1. Creating a cohort analysis: A cohort analysis is a common analysis in which you group users or customers into cohorts based on a specific behavior, such as the date they signed up or the region they’re in. You can use a query-based derived table to create this analysis by grouping users into cohorts and then calculating their behavior over time.
  2. Filtering out spam data: In some cases, your data may contain a large amount of spam or irrelevant data that you want to filter out. You can use a query-based derived table to filter out this data by creating a derived table that excludes any records that match a specific set of criteria.
  3. Aggregating data across multiple tables: In some cases, you may need to aggregate data across multiple tables that don’t have a direct relationship. You can use a query-based derived table to aggregate this data by creating a derived table that joins the necessary tables and performs the required calculations.
  4. Creating custom filters: In some cases, you may want to provide users with a way to customize their queries by applying custom filters. You can use a templated filter to create this functionality by creating a derived

For example, let’s say we have a database table called orders that has many columns. We can create a derived table named customer_order_summary and include a subset of the orders table’s columns:

We can now work with the customer_order_summary derived table just as if it were any other table in our database.

Native derived tables and SQL-based derived tables

To create a derived table in your Looker project, use the derived_table parameter under a view parameter. Inside the derived_table parameter, you can define the query for the derived table in one of two ways:

  • For a native derived table, you define the derived table with a LookML-based query.
  • For a SQL-based derived table, you define the derived table with a SQL query.

For example, the following view files show how you could use LookML to create a view from the customer_order_summary derived table. The two versions of the LookML illustrate how you can create equivalent derived tables using LookML or SQL to define the query for the derived table:


Both versions create a view called customer_order_summary that is based on the orders table, with the columns customer_idfirst_order, and total_amount.

Other than the derived_table parameter and its subparameters, this customer_order_summary view works just like any other view file. Whether you define the derived table’s query with LookML or with SQL, you can create LookML measures and dimensions based on the columns of the derived table.

Once you define your derived table, you can use it like any other table in your database.

Why Use Derived Tables in Looker?

One of the main reasons to use derived tables in Looker is to simplify the final view and make it easier for business users to access and understand the data. By performing complex calculations and transformations in a derived table, you can simplify the final view and make it more accessible to users who may not have technical expertise. Additionally, the use of derived tables can improve the performance of data queries, as the results of the derived table can be cached and reused in subsequent queries.

How to Use Derived Tables in Looker

Derived tables are defined within a LookML model, and can be based on one or more source tables. They are created using SQL-like syntax, and can include complex calculations and transformations. Here’s an example to demonstrate how derived tables can be used in Looker:

Let’s say you have a source table called “orders” that contains information about customer purchases, including the order ID, customer ID, order date, and order amount. You want to analyze the average order amount by month and by customer.

One way to achieve this would be to create a derived table that aggregates the order amount by month and customer ID, and then calculates the average. Here’s how you would define this derived table in LookML:

derived_table: avg_order_amount_by_month_and_customer {
  sql: SELECT DATE_TRUNC('month', order_date) as month, customer_id, AVG(order_amount) as avg_order_amount
    FROM orders
    GROUP BY 1, 2;
}

With this derived table in place, you can now create a view that uses the derived table to display the average order amount by month and customer. For example:

view: avg_order_amount_by_month_and_customer {
  derived_table: avg_order_amount_by_month_and_customer
  dimension: month {
    type: time
    sql: ${TABLE}.month
  }
  dimension: customer_id {
    sql: ${TABLE}.customer_id
  }
  measure: avg_order_amount {
    type: avg
    sql: ${TABLE}.avg_order_amount
  }
}

With this view in place, you can now create a dashboard or explore that displays the average order amount by month and customer.

Conclusion:

Derived tables are an essential part of the LookML modeling language in Looker, and provide a powerful tool for transforming and aggregating data. By using derived tables, you can simplify the

Take a look at the Looker Product, Also Reach out to us here if you are interested to evaluate if Looker is right for you.

_________________________________________________________________________________________

More Imp Topics

How Looker Writes SQL?

How to calculate percent of total in looker?

Table Calculations in Looker

What are Derived tables in Looker?

How to set conditional formatting in LOOKER?

Looker Interview Questions And Answers

New Looker Performance Recommendations Dashboard

Looker Git Version Control: How To Revert To A Specific Commit In Looker, No Git Commands Necessary

________________________________________________________________________________________

Reference :

About Me:-
I am Om Prakash Singh – Data Analytics Consultant, Looker Consultant, and Solution Architect.
I am a Highly analytical and process-oriented Data Analyst with in-depth knowledge of database types; research methodologies; and big data capture, manipulation, and visualization. Furnish insights, analytics, and business intelligence used to advance opportunity identification.

You’ve got data and lots of it. If you’re like most enterprises, you’re struggling to transform massive information into actionable insights for better decision-making and increased business results.
Reach out to us if you are interested to evaluate if Looker is right for you or any other BI solution.

This Post Has 12 Comments

  1. Payal Sharma

    Insightful article on derived tables in Looker. The benefits & best practices were well explained. Thanks!

    1. Om Prakash Singh

      Thanks a lot Payal, keep going with other articles. Hope you like it

  2. Sonu

    I really enjoyed reading this article. The information on derived tables in Looker was presented in a way that was both informative and engaging. I highly recommend it to anyone who is looking to improve their data analysis skills.

    1. Om Prakash Singh

      Thanks for your feedback Sonu, this is a great motivation for me.

  3. Amelia Grace

    Great resource for derived tables in Looker. Clear explanations & helpful examples.

    1. Om Prakash Singh

      Thanks Amelia , hope you like other contents too!

Leave a Reply