Calculated measure: deleting an re-creating
Hi,
There seems to be a a problem (bug??) when I delete a calculated measure and add another one again with the same name, but a different formula / script function. I have the following code to create the calculated measure, which uses a user defined multiplication factor.
So far it works fine. In my application, the user is allowed to change the factor var CustomFactor : double; to another value as they wish, then click a "recalc" button, which deletes the calculated measure and adds it again, using the following code:
The loop works fine by itself - it deletes the calculated measure. But when it gets added again with the same field name, caption etc. but a slightly different formula (the variable CustomFactor has changed), it calculates exactly the same values as before, not the updated values with the new CustomFactor.
I could use a different measure name each time or have some other work-around, but the root problem remains. Please could you help me
EDIT: I tried changing the fcxSlice.MeasuresContainer.Measures[].ScriptFunction at run-time, but then the measure becomes completely blank. How can I change the formula of a measure at run-time so it recalculates? I would greatly appreciate any help, thank you.
There seems to be a a problem (bug??) when I delete a calculated measure and add another one again with the same name, but a different formula / script function. I have the following code to create the calculated measure, which uses a user defined multiplication factor.
with fcxSlice.MeasuresContainer do
  AddCalcMeasure ('CustomA', 'CustomA', af_Formula, 'CustomFactorA', 'Result := Measures[''TotalFieldName''].CurrentValue * ' + CustomFactor.ToString);
So far it works fine. In my application, the user is allowed to change the factor var CustomFactor : double; to another value as they wish, then click a "recalc" button, which deletes the calculated measure and adds it again, using the following code:
fcxSlice.BeginUpdate;
 Â
// delete calculated measures
for i := fcxSlice.MeasuresContainer.Count - 1 downto 0 do
  if fcxSlice.MeasuresContainer.Measures[i].AgrFunc = af_Formula then
    fcxSlice.MeasuresContainer.DeleteMeasure(i);
with fcxSlice.MeasuresContainer do
  AddCalcMeasure ('CustomA', 'CustomA', af_Formula, 'CustomFactorA', 'Result := Measures[''TotalFieldName''].CurrentValue * ' + CustomFactor.ToString);
fcxSlice.EndUpdate;
The loop works fine by itself - it deletes the calculated measure. But when it gets added again with the same field name, caption etc. but a slightly different formula (the variable CustomFactor has changed), it calculates exactly the same values as before, not the updated values with the new CustomFactor.
I could use a different measure name each time or have some other work-around, but the root problem remains. Please could you help me
EDIT: I tried changing the fcxSlice.MeasuresContainer.Measures[].ScriptFunction at run-time, but then the measure becomes completely blank. How can I change the formula of a measure at run-time so it recalculates? I would greatly appreciate any help, thank you.