> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cube.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Hierarchies

> Hierarchies group dimensions into levels of granularity, allowing users to drill down or roll up for analysis.

You can use the `hierarchies` parameter within [cubes][ref-ref-cubes] to define hierarchies.
You can think about a hierarchy as a means to group [dimensions][ref-ref-dimensions] together and organize
them into levels of granularity, allowing users to drill down or roll up for analysis.

> [!NOTE]
>
> Hierarchies display is subject to support in \[visualization tools]\[ref-viz-tools].
> Check \[APIs & Integrations]\[ref-apis-support] for details.
> You can also preview hierarchies in \[Playground]\[ref-playground].

Any hierarchy should have the following parameters: [`name`](#name) and [`levels`](#levels).

> [!NOTE]
>
> Just like other members, hierarchies can be exposed to data consumers through
> \[views]\[ref-ref-views]. List them in the `includes` parameter of a view's
> `cubes` block alongside measures, dimensions, and segments.

[YouTube video](https://www.youtube-nocookie.com/embed/Y5E6DmH_Hz4)

## Parameters

### `name`

The `name` parameter serves as the identifier of a hierarchy. It must be unique
among all members within a cube and follow the [naming conventions][ref-naming].

```yaml title="YAML" theme={"dark"}
cubes:
  - name: users
    sql_table: users

    # ...

    hierarchies:
      - name: location
        title: User Location
        levels:
          - state
          - city
```

```javascript title="JavaScript" theme={"dark"}
cube(`users`, {
  sql_table: `users`,

  // ...

  hierarchies: {
    location: {
      title: `User Location`,
      levels: [
        state,
        city
      ]
    }
  }
})
```

### `title`

You can use the `title` parameter to set the human-readable name of a hierarchy:

```yaml title="YAML" theme={"dark"}
cubes:
  - name: users
    sql_table: users

    # ...

    hierarchies:
      - name: location
        title: User Location
        levels:
          - state
          - city
```

```javascript title="JavaScript" theme={"dark"}
cube(`users`, {
  sql_table: `users`,

  // ...

  hierarchies: {
    location: {
      title: `User Location`,
      levels: [
        state,
        city
      ]
    }
  }
})
```

### `levels`

The `levels` parameter is used to define the levels of the hierarchy. You can do so
by listing the dimensions included in the hierarchy, from less granular to more
granular ones:

```yaml title="YAML" theme={"dark"}
cubes:
  - name: users
    sql_table: users

    dimensions:
      - name: state
        sql: state
        type: string

      - name: city
        sql: city
        type: string

    hierarchies:
      - name: location
        title: User Location
        levels:
          - state
          - city
```

```javascript title="JavaScript" theme={"dark"}
cube(`users`, {
  sql_table: `users`,

  dimensions: {
    state: {
      sql: `state`,
      type: `string`
    },

    city: {
      sql: `city`,
      type: `string`
    }
  },

  hierarchies: {
    location: {
      title: `User Location`,
      levels: [
        state,
        city
      ]
    }
  }
})
```

You can include the same dimension in multiple hierarchies. It is also possible to
include a dimension from a joined cube into a hierarchy:

```yaml title="YAML" theme={"dark"}
cubes:
  - name: users
    sql_table: users

    joins:
      - name: orders
        sql: "{CUBE.id} = {orders.user_id}"
        relationship: one_to_many

    dimensions:
      - name: state
        sql: state
        type: string

      - name: city
        sql: city
        type: string

      - name: status
        sql: status
        type: string

    hierarchies:
      - name: details
        title: User Details
        levels:
          - status
          - state
          - city

      - name: statuses
        title: User & Order Statuses
        levels:
          - status
          - orders.status
```

```javascript title="JavaScript" theme={"dark"}
cube(`users`, {
  sql_table: `users`,

  joins: {
    orders: {
      sql: `${CUBE.id} = ${orders.user_id}`,
      relationship: `one_to_many`
    }
  },

  dimensions: {
    state: {
      sql: `state`,
      type: `string`
    },

    city: {
      sql: `city`,
      type: `string`
    },

    status: {
      sql: `status`,
      type: `string`
    }
  },

  hierarchies: {
    location: {
      title: `User Location`,
      levels: [
        state,
        city
      ]
    },

    statuses: {
      title: `User & Order Statuses`,
      levels: [
        status,
        orders.status
      ]
    }
  }
})
```

### `public`

The `public` parameter is used to manage the visibility of a hierarchy. Valid
values for `public` are `true` and `false`. When set to `false`, this hierarchy
**cannot** be queried through the API. Defaults to `true`.

```yaml title="YAML" theme={"dark"}
cubes:
  - name: users
    sql_table: users

    # ...

    hierarchies:
      - name: location
        title: User Location
        levels:
          - state
          - city
        public: false
```

```javascript title="JavaScript" theme={"dark"}
cube(`users`, {
  sql_table: `users`,

  // ...

  hierarchies: {
    location: {
      title: `User Location`,
      levels: [
        state,
        city
      ],
      public: false
    }
  }
})
```

[ref-ref-cubes]: https://docs.cube.dev/reference/data-modeling/cube.md

[ref-ref-dimensions]: https://docs.cube.dev/reference/data-modeling/dimensions.md

[ref-ref-views]: https://docs.cube.dev/reference/data-modeling/view.md

[ref-naming]: https://docs.cube.dev/docs/data-modeling/concepts/syntax.md#naming

[ref-apis-support]: https://docs.cube.dev/reference#data-modeling

[ref-playground]: https://docs.cube.dev/docs/explore-analyze/playground#viewing-the-data-model

[ref-viz-tools]: https://docs.cube.dev/admin/connect-to-data/visualization-tools


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.