calendar_definition

用法

view: retail_custom_calendar {
  calendar_definition: {
    reference_date: ref_date
    timeframe_mapping: {
      custom_date: custom_date
      custom_period: custom_period
      custom_quarter: custom_quarter
      custom_season: custom_season
      custom_week: custom_week
      custom_year: custom_year
    }
    timeframe_ordinal_mapping: {
      custom_date: custom_date
      custom_period: custom_period
      custom_quarter: custom_quarter
      custom_season: custom_season
      custom_week: custom_week
      custom_year: custom_year
    }
    previous_ordinal_mapping: {
      custom_date: custom_date
      custom_week: custom_week
    }
  }
层次结构
calendar_definition

定义

在 Looker 中使用 自定义日历 时,您可以使用 calendar_definition 参数将 Looker 自定义日历时间范围映射到您在数据库中创建的 自定义日历表的列

如需了解如何设置自定义日历视图文件,请参阅在 Looker 中使用自定义日历文档页面。

在自定义日历的视图文件中,添加 calendar_definition 参数块。例如:

  calendar_definition: {
    reference_date: ref_date
    timeframe_mapping: {
      custom_date: custom_date
      custom_period: custom_period
      custom_quarter: custom_quarter
      custom_season: custom_season
      custom_week: custom_week
      custom_year: custom_year
    }
    timeframe_ordinal_mapping: {
      custom_date: ordinal_custom_date
      custom_period: ordinal_custom_period
      custom_quarter: ordinal_custom_quarter
      custom_season: ordinal_custom_season
      custom_week: ordinal_custom_week
      custom_year: ordinal_custom_year
    }
    previous_ordinal_mapping: {
      custom_date: prev_day_num
      custom_week: prev_week_num
    }
  }

请注意关于 calendar_definition 参数块的以下事项:

请参阅本页面的示例部分。

在 LookML 中对自定义日历表进行建模后,您可以创建一个基于自定义日历视图的 维度组type: custom_calendar。然后,您的最终用户可以使用自定义日历维度时间范围创建探索查询。

reference_date

reference_date 参数用于指定自定义日历视图中的一个维度,该维度会映射到自定义日历数据库表中的列,该列提供标准日历日期,例如 2026-01-01

请注意关于 reference_date 参数的以下事项:

  • reference_date 是必需的,因为 Looker 会使用它来计算从数据表到自定义日历表的联接,以提供使用自定义时间范围的查询结果。
  • reference_date 必须是唯一的或主键。

请参阅本页面的示例部分,了解 reference_date 如何指向对自定义日历表中的列进行建模的维度。

timeframe_mapping

timeframe_mapping 组成块会将每个 Looker 自定义时段映射到对自定义日历数据库表的等效列进行建模的维度。

timeframe_mapping: {
  custom_date: field-ref
  custom_period: field-ref
  custom_quarter: field-ref
  custom_season: field-ref
  custom_week: field-ref
  custom_year: field-ref
}

请参阅本页面的 示例 部分,了解每个 timeframe_mapping 子参数如何指定对自定义日历表中的列进行建模的维度。

timeframe_ordinal_mapping

timeframe_ordinal_mapping 组成块会将每个 Looker 自定义时段映射到对自定义日历数据库表的相应序号列进行建模的维度。

timeframe_ordinal_mapping: {
  custom_date: field-ref
  custom_period: field-ref
  custom_quarter: field-ref
  custom_season: field-ref
  custom_week: field-ref
  custom_year: field-ref
}

请参阅本页面的示例部分,了解每个timeframe_ordinal_mapping子参数如何指定对自定义日历表中的列进行建模的维度。

previous_ordinal_mapping

如果您想要创建同比 (PoP) 指标,还必须向 calendar_definition 添加 previous_ordinal_mapping 块。

previous_ordinal_mapping 组成块会将每个 Looker 自定义时间范围映射到对自定义日历数据库表的相应列进行建模的维度。

previous_ordinal_mapping: {
  custom_date: field-ref
  custom_week: field-ref
}

请参阅本页面的示例部分,了解每个previous_ordinal_mapping子参数如何指定对自定义日历表中的列进行建模的维度。

必需的时间范围

timeframe_mappingtimeframe_ordinal_mapping 的时间范围子参数是必需的。如果特定时间范围(例如 custom_season)与您的自定义日历无关,您仍必须通过在日历视图中定义占位符维度来添加该时间范围。例如:

 dimension: season {
    type: string
    sql: 'N/A' ;;
    hidden: yes
  }
  dimension: season_num {
    type: number
    sql: 0 ;;
    hidden: yes
  }

示例

以下是一个名为 fiscal_calendar.view.lkml 的示例视图文件,该文件对名为 fiscal_calendar_table 的自定义日历表进行建模:

view: fiscal_calendar {
  sql_table_name: fiscal_calendar_table ;;

  calendar_definition: {
    reference_date: reference_date

    timeframe_mapping: {
      custom_year: fiscal_year
      custom_quarter: fiscal_quarter_of_year
      custom_date: fiscal_date_of_month
      custom_week: fiscal_week_of_year
      custom_period: fiscal_period_of_year
      custom_season: season
    }
    timeframe_ordinal_mapping: {
      custom_year: fiscal_year_num
      custom_quarter: fiscal_quarter_of_year_num
      custom_date: fiscal_date_of_month_num
      custom_week: fiscal_week_of_year_num
      custom_period: fiscal_period_of_year_num
      custom_season: season_num
    }
    previous_ordinal_mapping: {
          custom_date: prev_custom_date
          custom_week: prev_custom_week
    }
  }

  dimension: reference_date {
    type: date
    primary_key: yes
    sql: ${TABLE}.reference_date ;; # Name of the column in your database table that shows the standard date, such as `2026-01-01`
  }

  dimension: fiscal_year {
    type: string
    sql: ${TABLE}.fiscal_year ;;
  }

  dimension: fiscal_year_num {
    type: number
    sql: ${TABLE}.fiscal_year_num ;;
  }

  dimension: prev_custom_week {
          type: number
          sql: ${TABLE}.prev_custom_week ;;
        }

  # ... other dimensions for quarters, weeks, periods, seasons, etc. ...

  # Example placeholder dimensions for unused timeframes
  dimension: season {
    type: string
    sql: 'N/A' ;;
    hidden: yes
  }
  dimension: season_num {
    type: number
    sql: 0 ;;
    hidden: yes
  }
}