Looker Blocks are pre-built pieces of LookML code provided by Looker and the Looker community. They represent best practices for modeling, visualizing, and analyzing data in Looker. Not only do they serve as a starting point for your LookML projects, but they can also be easily customized to meet your specific requirements.
Looker Blocks fall into two main categories: Data Blocks and Analytic Blocks. Data Blocks are pre-modeled datasets, providing foundational building blocks for your analyses. Analytic Blocks, on the other hand, encompass analytical patterns and methodologies such as cohort, funnel, and retention analyses.
Each Looker Block is a set of LookML files, including model files, view files, and dashboard files. These blocks are readily available in Looker's Marketplace and GitHub repository. Adding a Looker Block to your project is a simple task. From the Marketplace, you just find a block that fits your needs and hit "Install." The LookML files associated with the block are then automatically added to your selected Looker project.
While Looker Blocks serve as a robust foundation, customization is often necessary to match your database schema and specific business needs. Let's assume you've installed the 'Sales Analytics' block from the Marketplace. To customize a field within a LookML view file, navigate to the file in the Looker IDE. For example, if you want to customize the total_sales measure in the sales view, you can simply modify the LookML code accordingly:
# sales.view.lkml
view: sales {
# ...
measure: total_sales {
type: sum
sql: ${TABLE}.amount ;;
}
}
This measure aggregates the amount field from the sales table. If your sales table uses a different field name for the amount of a sale, you can replace amount with the correct field name in your table.
But what if you find yourself repeating specific tasks or complex analyses? In such cases, creating your own Looker Blocks may be a practical solution. This approach involves crafting a set of LookML files that encapsulate a specific data model or analysis. For instance, let's consider a frequent retention analysis on your orders data. To avoid repeating this analysis in every project, you can create a Looker Block:
# retention.model.lkml
model: retention {
explore: monthly_retention {
view_name: monthly_retention
from: derived_table {
sql: SELECT DATE_TRUNC('month', order_date) as month,
COUNT(DISTINCT customer_id) as customer_count,
COUNT(DISTINCT IF(order_number > 1, customer_id, NULL)) as retained_customers
FROM orders
GROUP BY month ;;
}
}
}
In this block, a new LookML model file and an Explore for the retention analysis are created. A derived table in a LookML view file calculates the necessary retention metrics, and a LookML dashboard file visualizes these metrics.
To summarize, Looker Blocks offer a fast-track approach to constructing data models and performing complex analyses in Looker. They are not only flexible, reusable, and shareable, but they also embody best practices in LookML code. By harnessing Looker Blocks, you can significantly speed up your development process in Looker while ensuring high standards of quality and efficiency. Keep an eye out for the next article where we'll dive into more advanced topics, such as setting up data tests and configuring scheduled data deliveries in Looker.