Compound metrics defined in an MDX cube that are based on a custom MDX expression may need to include set expressions to define the scope of the calculation. For instance, a metric may use a prompt to select months or quarters, and then evaluate the average over the day-level values belonging to the prompt selection.
In this example, "Attributes of the Date hierarchy" is a hierarchy prompt using the Date hierarchy of a Microsoft Analysis Services 2005 cube. Generate() is required because the user could choose more than one element; this function obtains the Day-level descendents of each user selection.

ApplySimple("Avg(Generate({ #0 }, Descendants(..CurrentMember, ..)), #1)", ?, Revenue)
This metric is used in a report such as the following, which generates the MDX statement below.

with set as '{...members}'
member . as 'Avg(Generate({ ...& }, Descendants(..CurrentMember, ..)), .)'
select {., .} on columns,
non empty on rows
from
where (...&)
cell properties CELL_ORDINAL, FORMATTED_VALUE, VALUE, FORMAT_STRING

A source of inefficiency in this query, particularly against Microsoft Analysis Services, is that . needs to be evaluated for every item on the row axis in the select clause, which may cause the Generate function to reevaluate for each cell. But in this formula, the Generate expression does not depend on the selected items in any way; its result will be the same for every iteration. It would be more efficient to evaluate the set once, and use a reference to it in .'s expression.
Beginning with Strategy Engine 9.0.0, this is accomplished by placing the delimiter %%SET%% before and after the portion of the expression to isolate as a named set.

ApplySimple("Avg(%%SET%%Generate({ #0 }, Descendants(..CurrentMember, ..))%%SET%%, #1)", ?, Revenue)
With this modification to the metric, the MDX now generates with an additional set declaration within the query:
with set as '{...members}'
set as 'Generate({ ...& }, Descendants(..CurrentMember, ..))'
member . as 'Avg(, .)'
select {., .} on columns,
non empty on rows
from
where (...&)
cell properties CELL_ORDINAL, FORMATTED_VALUE, VALUE, FORMAT_STRING
will evaluate once at the beginning of query execution.
Notes



