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

# Cube versioning and the Epoch Dimension

> Java API for cube versions in Atoti: `IActivePivotVersion` and how cube versions relate to Datastore epochs, and the Epoch Dimension for cross-epoch and cross-branch MDX What-If queries: configuration, per-user disabling, real-time behavior, and efficient queries.

> [!NOTE]
>
> For a conceptual overview of data versioning, branches, and epochs, see [How Atoti manages data versioning](https://docs.activeviam.com/concepts/versions/versions.md) on Atoti Hub.

This page documents cube versions and the Epoch Dimension. For epochs, epoch management, and branches in the Atoti Datastore, see [Data versioning](https://docs.activeviam.com/engine/java-sdk/6.2/database/data_versioning.md).

## What are versions?

Every committed transaction implies a new version of the Datastore. However, for each epoch, depending on the nature of
the transaction, other multi-versioned components may or may not hold a version associated to this epoch.

### Example

Consider the following example: within the Datastore, there are two stores, the Risk store and the Forex store.

No data from the Forex Store is ever used within the Cube.
The Forex Store exists purely to provide parameters for post-processing. It does not contribute to any pivot tables.

The Risk Store is the base store within the Datastore and there are no references between the Risk Store and the Forex
store.

When a transaction commits in the Forex Store, no new `IActivePivotVersion` will be created, because the cube is not
directly affected by anything in the Forex store.

So a new version of the Datastore will be created (with an incremented Epoch Counter as its Id),
but **NOT** a new `IActivePivotVersion`.

The `IActivePivotVersion` is associated with an Epoch, but not necessarily the same one as the latest version of the
Datastore.

The Epoch of any version can be found by using the `getEpoch()` method of the `IVersion` interface.

> Note about `IActivePivotVersion`s: each time an `IActivePivotVersion` is created, a new instance of the Aggregates
> Cache and Post-processors defined in the cube are associated with the new version.

The illustration below shows transactions committing on either the Risk Store or the Forex store for the presented
example:

![Transactions](https://mintcdn.com/activeviam/tQvlpZIn_EhELfJK/engine/java-sdk/6.2/assets/core/transactions.png?fit=max\&auto=format\&n=tQvlpZIn_EhELfJK\&q=85\&s=5b97bdef13e1521905d561869f3226ef)

## What is the Epoch Dimension?

The Epoch Dimension retrieves values from previous versions of the cube and enables indicators based on time or branch comparisons.

Cross-epoch and cross-branch analysis in MDX queries is performed through the Epoch Dimension.

> The Epoch Dimension can only be used in MDX queries but **NOT** inside post processors.
> The MDX function `Aggregate` cannot be used on the members of the Epoch Dimension.

The Epoch Dimension is by default composed of a hierarchy named after the dimension,
with two levels `Branch` and `Epoch`:

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
Dimension: Epoch
Hierarchy: Epoch
Levels: Branch > Level

- master
    +- Epoch 1
    +- Epoch 3
- Branch Scenario 1
    +- Epoch 2
```

The Branch level is populated by branches on which a transaction was committed.

### How is the Epoch Dimension configured?

In the cube description, the following line must be entered directly after the other dimensions:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
.withEpochDimension()
```

By default, the Epoch Dimension will create a dimension with a single `Branch` level available on the `Epoch` hierarchy.
The above call returns a builder that allows further customization of the Epoch Dimension and its two levels:
`Branch` and `Epoch`:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
.withEpochDimension()
.withinFolder("f")
.withMeasureGroups("mg")
.withEpochLevel()
.withFormatter("FFF")
.end()
```

Note that the Epoch Dimension must be defined entirely in one call of the builder, or an error message will be prompted.

### How is the Epoch Dimension disabled for specific users?

It is possible to restrict the usage of the Epoch Dimension. The dimension can be disabled for some users while the
dimension is enabled for others by attaching an MdxContext to their roles, using the following method:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
context.setDisableEpochDimension(true);
```

### How do real-time What-If queries behave?

A real-time query including the Epoch Dimension such as:

```
SELECT NON EMPTY [Epoch].[Epoch].[Branch].Members ON ROWS
  FROM cube
```

respects the following contract regarding updates on the Epoch Dimension:

* On first registration (i.e. activating real-time), all visible branches should be in the result
* Upon creating a branch without changes, the query won't reflect the newly created branch
  (No transaction on the branch means no update)
* A commit on any visible branch will update the query and reflect the new branch

### How to manage the Epoch Dimension

* Calculated members which use the Epoch Dimension can be defined:

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
 WITH MEMBER [Measures].[pnl.SUM AVG] AS
    AVG([Epoch].[Epoch].CurrentMember.Lag(5):[Epoch].[Epoch].CurrentMember.Lead(5), [Measures].[pnl.SUM])
SELECT {[Measures].[pnl.SUM], [Measures].[pnl.SUM AVG]} ON COLUMNS,
[Epoch].[Epoch].[Epoch].Members ON ROWS
FROM cube
```

The previous snippet defines a Calculated Member corresponding to the rolling average of the pnl.SUM aggregated measure,
with a centered 10 epoch-wide window:

![PNL Sum Lisse](https://mintcdn.com/activeviam/tQvlpZIn_EhELfJK/engine/java-sdk/6.2/assets/core/pnlsumLisse.png?fit=max\&auto=format\&n=tQvlpZIn_EhELfJK\&q=85\&s=9bf2fde4fc74d88add46859c9c7e1ab1)

* The members that are available in the dimension depend on the Epoch Management Policy. The released epochs are not
  available in the dimension.

* The name of the Epoch Dimension can be changed to "Branch" (useful if the epoch level is disabled) by setting the
  following property:

```properties theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
activeviam.mdx.epoch.dimension.legacyName=false
```

This affects the final MDX sent to the server. To change the name of this hierarchy and its levels to match domain-specific names, use the [Internationalization feature](https://docs.activeviam.com/engine/java-sdk/6.2/configuration/internationalization.md).

### Efficient queries and Examples

#### How are efficient queries structured?

The filters on the Epoch Dimension must be performed inside sub-selects.

The 2 following queries will return the same results (i.e. the values of pnl.SUM for 5 epochs):

**Inefficient Query:**

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
SELECT [Measures].[pnl.SUM] ON COLUMNS,
Subset([Epoch].[Epoch].[Epoch].Members, 0, 5) ON ROWS
FROM cube
```

**Efficient Query:**

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
SELECT [Measures].[pnl.SUM] ON COLUMNS,
[Epoch].[Epoch].[Epoch].Members ON ROWS
    FROM (SELECT Subset([Epoch].[Epoch].[Epoch].Members, 0, 5) ON 0
        FROM cube)
```

The MDX engine evaluates the cells before building the axes. Because of this behavior, the MDX engine will, for the
first query, retrieve the values of pnl.SUM on **all the available versions of the cube** and then only display 5 of
them.

The second query creates a restriction on the Epoch Dimension due to a sub-select. As the sub-selects are computed
before the cells of the pivot table, the MDX engine will only retrieve the values of pnl.SUM for the 5 selected epochs.

#### How do queries behave with real-time updates?

With the Epoch Dimension, it is possible to look into past data.
Some queries receive real-time updates if the `AllMember` member is not filtered out by the sub-select
filtering the Epochs.

Here are some examples:

<table>
  <tbody>
    <tr>
      <td>
        The following query will receive real time updates.\

        One new row will appear for each new version:
        \
        &#x20; `SELECT [Measures].[pnl.SUM] ON COLUMNS,`
        \
        &#x20; `[Epoch].[Epoch].[Epoch].Members ON ROWS`
        \
        &#x20; `FROM cube`
      </td>

      <td>
        ![](https://mintcdn.com/activeviam/tQvlpZIn_EhELfJK/engine/java-sdk/6.2/assets/core/subset_5.png?fit=max\&auto=format\&n=tQvlpZIn_EhELfJK\&q=85\&s=9dd6dfc68b927a760868c1def816ff21)
      </td>
    </tr>

    <tr>
      <td>
        As this query aims to display the value of Epoch 20 from the branch master, \

        this query will not receive real time updates.
        \
        &#x20; `SELECT [Measures].[pnl.SUM] ON COLUMNS,`
        \
        &#x20; `[Epoch].[Epoch].[Epoch].Members ON ROWS`
        \
        &#x20;`FROM (SELECT [Epoch].[Epoch].[Branch].[master].[20] ON 0`
        \
        &#x20; `FROM cube`
      </td>

      <td>
        ![](https://mintcdn.com/activeviam/tQvlpZIn_EhELfJK/engine/java-sdk/6.2/assets/core/one_epoch.png?fit=max\&auto=format\&n=tQvlpZIn_EhELfJK\&q=85\&s=27552a88f88284f34be4569c86eaa821)
      </td>
    </tr>

    <tr>
      <td>
        This query is like the previous one but uses a subselect to choose 2 epochs.
        \
        &#x20; `SELECT [Measures].[pnl.SUM] ON COLUMNS,`
        \
        &#x20; `[Epoch].[Epoch].[Epoch].Members ON ROWS`
        \
        &#x20;`FROM (SELECT {[Epoch].[Epoch].[Epoch].[10],`
        \
        &#x20;`[Epoch].[Epoch].[Epoch].[12]} ON 0`
        \
        &#x20; `FROM cube)`
      </td>

      <td>
        ![](https://mintcdn.com/activeviam/tQvlpZIn_EhELfJK/engine/java-sdk/6.2/assets/core/two_epochs.png?fit=max\&auto=format\&n=tQvlpZIn_EhELfJK\&q=85\&s=dd83aa5f1be9b5a66efbf86e9f84306b)
      </td>
    </tr>

    <tr>
      <td>
        This query that displays the five oldest epochs will never be updated because the MDX engine cannot register continuous queries on past Epochs.
        \
        &#x20; `SELECT [Measures].[pnl.SUM] ON COLUMNS,`
        \
        &#x20; `[Epoch].[Epoch].[Epoch].Members ON ROWS`
        \
        &#x20; `FROM (SELECT Tail([Epoch].[Epoch].[Epoch].Members, 5) ON 0`
        \
        &#x20; `FROM cube)`
      </td>

      <td>
        ![](https://mintcdn.com/activeviam/tQvlpZIn_EhELfJK/engine/java-sdk/6.2/assets/core/tail.png?fit=max\&auto=format\&n=tQvlpZIn_EhELfJK\&q=85\&s=3b7ac156771643dea467067a30fe8383)
      </td>
    </tr>

    <tr>
      <td>
        This query will always display the five most recent epochs.
        \
        &#x20; `SELECT [Measures].[pnl.SUM] ON COLUMNS,`
        \
        &#x20; `[Epoch].[Epoch].[Epoch].Members ON ROWS`
        \
        &#x20; `FROM ( SELECT Head([Epoch].[Epoch].[Epoch].Members, 5) ON 0`
        \
        &#x20; `FROM cube)`
      </td>

      <td>
        ![](https://mintcdn.com/activeviam/tQvlpZIn_EhELfJK/engine/java-sdk/6.2/assets/core/head.png?fit=max\&auto=format\&n=tQvlpZIn_EhELfJK\&q=85\&s=fba5fba9064b0346360446e3c1d8a29c)
      </td>
    </tr>
  </tbody>
</table>

### How is the Epoch Dimension used in a distributed environment?

In a distributed environment, the Query Cube does not have an Epoch Dimension by default. In order to support the
Epoch Dimension in a query node, it must be specified when building the cube definition. To learn more about what-if in
a distributed environment, see [What-if](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_what_ifs.md).
