---
title: Implementing custom time dimension granularities
description: "This recipe shows examples of commonly used custom granularities."
---

## Use case

Sometimes, you might need to group the result set by units of time that are
different from [default granularities][ref-default-granularities] such as `week`
(starting on Monday) or `year` (starting on January 1).

Below, we explore the following examples of custom granularities:
* *Week starting on Sunday*, commonly used in the US and some other countries. A
*fiscal week* that starts on any other day is defined the same way.
* *[Fiscal year][wiki-fiscal-year]* and *fiscal quarter*, commonly used in
accounting and financial reporting.

## Data modeling

Consider the following data model. `interval` and `offset` parameters are used to
configure each custom granularity in `granularities`.

`interval` is the only way to define a granularity under a name of your own, and it
places the granularity in the `day` → `week` → `month` → `quarter` → `year` hierarchy,
so Cube derives it arithmetically and rolls it up with the units around it. In exchange,
the period has to be a fixed length. A period that varies in length, such as a month of
a 4-5-4 retail calendar, is read from a pre-calculated column instead — see [overriding
granularities][ref-calendar-cubes] and the [custom calendar
recipe][ref-recipe-custom-calendar].

Note that each custom granularity is also exposed via a [proxy
dimension][ref-proxy-granularity]. This is not just a convenience: while the
[REST API][ref-rest-api] can query custom granularities by name, the [SQL
API][ref-sql-api] and the [GraphQL API][ref-graphql-api] can only address the
default granularities, so a proxy dimension is the way to query a custom
granularity from those APIs or from a BI tool. Proxy dimensions can also be used
in further calculations, like rendering `fiscal_quarter_label` below.

<CodeGroup>

```yaml title="YAML"
cubes:
  - name: custom_granularities
    sql: |
      SELECT '2024-01-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-02-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-03-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-04-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-05-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-06-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-07-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-08-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-09-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-10-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-11-15'::TIMESTAMP AS timestamp UNION ALL
      SELECT '2024-12-15'::TIMESTAMP AS timestamp

    dimensions:
      - name: timestamp
        sql: timestamp
        type: time

        granularities:
          - name: sunday_week
            interval: 1 week
            offset: -1 day

          - name: fiscal_year
            title: Federal fiscal year in the United States
            interval: 1 year
            offset: -3 months

          - name: fiscal_quarter
            title: Federal fiscal quarter in the United States
            interval: 1 quarter
            offset: -3 months
      
      - name: sunday_week
        sql: "{timestamp.sunday_week}"
        type: time
      
      - name: fiscal_year
        sql: "{timestamp.fiscal_year}"
        type: time
      
      - name: fiscal_quarter
        sql: "{timestamp.fiscal_quarter}"
        type: time

      - name: fiscal_quarter_label
        sql: |
          'FY' || (EXTRACT(YEAR FROM {timestamp.fiscal_year}) + 1) ||
          '-Q' || EXTRACT(QUARTER FROM {timestamp.fiscal_quarter} + INTERVAL '3 MONTHS')
        type: string
```

```javascript title="JavaScript"
cube(`custom_granularities`, {
  sql: `
    SELECT '2024-01-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-02-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-03-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-04-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-05-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-06-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-07-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-08-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-09-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-10-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-11-15'::TIMESTAMP AS timestamp UNION ALL
    SELECT '2024-12-15'::TIMESTAMP AS timestamp
  `,

  dimensions: {
    timestamp: {
      sql: `timestamp`,
      type: `time`,

      granularities: {
        sunday_week: {
          interval: `1 week`,
          offset: `-1 day`
        },

        fiscal_year: {
          title: `Federal fiscal year in the United States`,
          interval: `1 year`,
          offset: `-3 months`
        },

        fiscal_quarter: {
          title: `Federal fiscal quarter in the United States`,
          interval: `1 quarter`,
          offset: `-3 months`
        }
      }
    },
    
    sunday_week: {
      sql: `${timestamp.sunday_week}`,
      type: `time`
    },

    fiscal_year: {
      sql: `${timestamp.fiscal_year}`,
      type: `time`
    },
      
    fiscal_quarter: {
      sql: `${timestamp.fiscal_quarter}`,
      type: `time`
    },

    fiscal_quarter_label: {
      sql: `
          'FY' || (EXTRACT(YEAR FROM {timestamp.fiscal_year}) + 1) ||
          '-Q' || EXTRACT(QUARTER FROM {timestamp.fiscal_quarter} + INTERVAL '3 MONTHS')
      `,
      type: `string`
    }
  }
})
```

</CodeGroup>

## Result

Querying this data modal would yield the following result:

<Frame>
  <img src="https://ucarecdn.com/c385c29d-e145-47da-8551-2be5efb1c268/" />
</Frame>


[ref-custom-granularities]: /reference/data-modeling/dimensions#granularities
[ref-default-granularities]: /docs/data-modeling/dimensions#time-dimensions
[wiki-fiscal-year]: https://en.wikipedia.org/wiki/Fiscal_year
[ref-sql-api]: /reference/core-data-apis/sql-api
[ref-rest-api]: /reference/core-data-apis/rest-api
[ref-graphql-api]: /reference/core-data-apis/graphql-api
[ref-proxy-granularity]: /docs/data-modeling/dimensions#time-dimension-granularity-references
[ref-calendar-cubes]: /docs/data-modeling/concepts/calendar-cubes#overriding-granularities
[ref-recipe-custom-calendar]: /recipes/data-modeling/custom-calendar
