> ## 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.

# Adding new cube measures

This page provides a description of how we can add a measure. The techniques employed are generic examples that can be
extended, adapted, and repeated for any use case that we need.

To avoid forking library code, all extensions are made by subclassing the relevant configuration class from the `cvarc-starter` library module and wiring the subclass into your project's `ApplicationConfig` in the `cvarc-application` module.

## Step 1 - extend the cube selection

If your new measure depends on a field that is not yet reachable through the cube's selection, extend the selection so the field can be found.

The selection is built in `createBASelection()` / `createSASchemaSelectionDescription()`, which live in `BACubeConfig` / `SACubeConfig` (in the `cvarc-starter` library module). Use the subclass pattern described in [Adding a Hierarchy — Step 2](./adding-a-hierarchy#step-2---extending-the-cubes-selection): create a `CustomBACubeConfig extends BACubeConfig` (or `CustomSACubeConfig extends SACubeConfig`) in `cvarc-application/src/main/java/com/activeviam/cvarc/application/cfg/extensions/`, annotated with `@Configuration` and `@Primary`, and override the selection method to add the reference to your new field's store.

<Note>
  Make sure that any new field added to the selection does not exist in the selection already, and that it points to a corresponding field in a datastore.
</Note>

## Step 2 - define the measure

Extend `BAMeasureBuilder` (or `SAMeasureBuilder`) to add new measures. Place the new class in `cvarc-application/src/main/java/com/activeviam/cvarc/application/cfg/extensions/`.

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
public class ExtendedBAMeasureBuilder extends BAMeasureBuilder {

    @Override
    public void measures(ICopperContext context) {
        super.measures(context);
        Copper.sum("TestField").publish(context);
    }
}
```

To read more about creating measures with Copper,
see [Creating and publishing measures.](https://docs.activeviam.com/products/atoti/server/6.0.9/docs/copper/copper_measures/)

## Step 3 - wire the extended measure builder into the cube config

To swap `BAMeasureBuilder::measures` for `ExtendedBAMeasureBuilder::measures`, extend the cube config rather than editing it in place. If you already created `CustomBACubeConfig` for Step 1, override `configureCubeBuilder()` in the same class; otherwise create it now in `cvarc-application/src/main/java/com/activeviam/cvarc/application/cfg/extensions/`.

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@Configuration
@Primary
public class CustomBACubeConfig extends BACubeConfig {

    @Override
    public ICanBuildCubeDescription<IActivePivotInstanceDescription> configureCubeBuilder(
            final INamedCubeDescriptionBuilder builder) {
        return builder
                .withMeasures(ExtendedBAMeasureBuilder::measures) // use the extended measure builder
                // ... copy the rest of the parent's chain, or call super.configureCubeBuilder(builder)
                //     and post-process the result if you only need a localized change.
                ;
    }
}
```

Then add `CustomBACubeConfig` to the `@Import` list of `ApplicationConfig` in `cvarc-application/src/main/java/com/activeviam/cvarc/application/cfg/ApplicationConfig.java`.
