Hi guys, I am new to dbt semantic layer and metricflow. I am currently creating a project to leverage the usage of dbt semantic layer in dbt core, to calculate the Recency, Frequency, and Monetary metrics of the customers. This metrics later will be used as a features to perform a clustering.
I found difficulties in writing semantic model to calculate the Recency metric of each customer. Here is my SQL (Bigquery) query to calculate the metric:
with recency as (
select
customer_id,
transaction_date,
MAX(DATE_ADD(transaction_date, INTERVAL 1 DAY)) OVER() AS snapshot_date,
ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY transaction_date DESC) as rank
from `marketing_analytics_dwh.fct_transactions`
)
select
r.customer_id,
c.customer_name_hashed,
transaction_date as latest_transaction_date,
DATE_DIFF(snapshot_date, transaction_date, day) AS customer_recency
from recency r
join `marketing_analytics_dwh.dim_customers` c
on r.customer_id = c.customer_id
where rank = 1
order by c.customer_id, r.transaction_date
Basically I calculate the number of days + 1 that have passed from the latest transaction date performed by each customer to the latest transaction date + 1 day.
Could you guys help me to convert this to semantic model?