多次统计回头客 BigQuery 和 Data Studio

Repeat Customers being counted more than once BigQuery & Data Studio

我正在使用 BigQuery 和 Google Data Studio。我想统计所选日期范围内回头客的数量和重复购买率(重复customer/totalcustomers)。日期范围是我的数据工作室仪表板上的过滤器。

示例数据集

选择的日期范围过滤器:2020 年 1 月 1 日至 2020 年 9 月 30 日

我想要的输出

我得到的输出

这是我目前的代码

    with ct as(
    select order_id, customer_id, order_date, first_order_date, 
    case when datediff(order_date, first_order_date,Day)!=0 
    then 1 else 0 end as ret_flag 
    from xx
    )
    
    select order_date, 
count(distinct(case when return_flag=0 then customer_id end)) as new_customer
    count(distinct(case when return_flag=1 then customer_id end)) as return_Customer
        
    from ct 
    group by order_date

由于日期范围过滤器是由用户动态选择的,因此该逻辑不应在查询中,而应在仪表板本身中。使用您的示例数据集作为仪表板的数据源,因为在仪表板中 is.Then 单击 Resource -> Manage added datasourcesEDIT 您的数据源。单击 ADD A FIELD 并应用您的逻辑 COUNT_DISTINCT(CASE ... END)