The Problem
I have two model files, and one datagroup
. The datagroup should be used in both model files. Currently I have to copy and paste the datagroup into each model file. The problem is that if I want to edit the datagroup, I have to do so in both files. Is there a way to define the datagroup just once?
A Solution
We can approach this problem by taking advantage of the include
parameter. We can make a separate file that contains only the datagroups, and include that in both model files.
Step 1: Create a Datagroups File
Create a separate .lkml
file to contain your datagroups. You can create a .lkml
datagroup file in the same way you can create a separate .lkml
Explore file. In this example, the datagroups file is named datagroups.lkml
:
datagroup: daily { max_cache_age: "24 hours" sql_trigger: SELECT CURRENT_DATE();; }
Step 2: Include the Datagroups File in Your Model Files
Now that you've created the datagroups file, you can include it in both of your models, and use persist_with
to apply the datagroup to individual Explores in your models, or to apply the datagroup to all Explores in a model. For example, the two model files below both include the datagroups.lkml
file. This file is named ecommerce.model.lkml
. The daily
datagroup is used at the explore
level so that it applies just to the orders
Explore:
include: "datagroups.model.lkml" connection: "database1" explore: orders { persist_with: daily }
This next file is named inventory.model.lkml
. The daily
datagroup is used at the model
level so that it applies to all of the Explores in the model file:
include: "datagroups.model.lkml" connection: "database2" persist_with: daily explore: items { } explore: products { }