I understand how to dynamically apply measures to ...
# gooddata-ui
I understand how to dynamically apply measures to an insight, but I am running into issues when I swap a measure for an insight that has a secondary measure such as a headline insight. Changing the measure seems to break the secondary measure and not allow the insight to be rendered. When looking at the secondary measure I am seeing a popMeasure but I dont see any clear way to update it. Does anyone have advice on how to tackle this issue?
Hello Justin, if I understand you correctly, you have some IInsightDefinition object, and you are using functions like insightSetBuckets to override measures in the insight. If you have a Headline insight with a primary measure and then an automatically generated secondary measure (like in the screenshot), then if you swap out the primary measure, the secondary measure will no longer make sense. The reason for that is that the secondary measure references the primary one using so-called localIdentifier. This localIdentifier changes when you swap the primary measure as it will have a different one. To fix this, you need to change both measures, first get whatever measure you want to use as the primary and then derive a new secondary measure for the new primary measure that points to it. Something along these lines:
Copy code
import {
} from "@gooddata/sdk-model";

const originalInsight: IInsight = {} as any; // skipped for brevity
// get both measures, we assume the insight is a Headline
const primaryMeasureBucket = insightBucket(originalInsight, "measures");
const secondaryMeasureBucket = insightBucket(originalInsight, "secondary_measures");
const primaryMeasure = bucketMeasure(primaryMeasureBucket);
const secondaryMeasure = bucketMeasure(secondaryMeasureBucket);

// use a different primary measure
const swapedPrimaryMeasure = primaryMeasure; // or some custom logic to get the new primary measure, skipped for brevity
// if the secondary measure is a PoP measure, make sure the new one points to the new primary measure
const adaptedSecondaryMeasure = isPoPMeasure(secondaryMeasure)
    ? modifyPopMeasure(secondaryMeasure, (m) => m.masterMeasure(swapedPrimaryMeasure))
    : secondaryMeasure;

// get new insight with the buckets updated
const newInsight = insightSetBuckets(originalInsight, [
    newBucket("measures", swapedPrimaryMeasure),
    newBucket("secondary_measures", adaptedSecondaryMeasure),
Is this something you were looking for or did I not understand you correctly?
@Dan Homola sorry for the late reply, this worked perfectly, thanks for your help.
No worries, I’m glad it helped 🙂