IIf (MDX)
Returns one of two values determined by a logical test.
Syntax
IIf(Logical_Expression, Expression1 [HINT <hints>], Expression2 [HINT <hints>]) [HINT <hints>]
<hints> ::= <hint> [<hints>]
<hint> ::= EAGER | STRICT | LAZY
Arguments
Logical_Expression
A valid Multidimensional Expressions (MDX) logical expression that evaluates to true or false.Expression1 [HINT <hints>]
A valid Multidimensional Expressions (MDX) expression. HINT <hints> is optional modifier that determines how and when the expression is evaluated. See the Remarks section for more information.Expression2[HINT <hints>]
A valid Multidimensional Expressions (MDX) expression. HINT <hints> is optional modifier that determines how and when the expression is evaluated. See the Remarks section for more information.
Remarks
The expression specified by the logical expression evaluates to false only if the value of this expression is zero. Any other value evaluates to true.
If the specified logical expression evaluates to true, the IIf function returns the first expression. Otherwise, the function returns the second expression.
The specified expressions can return values or MDX objects. Furthermore, the specified expressions need not match in type.
Note
In MicrosoftSQL Server 2000, Analysis Services supported only numeric and string return types, and the types of specified expressions had to be the same. These restrictions do not apply to SQL ServerAnalysis Services.
The IIf function is not recommended for creating a set of members based on search criteria. Instead, use the Filter function to evaluate each member in a specified set against a logical expression and return a subset of members.
Note
If either expression evaluates to NULL, the result set will be NULL when that condition is met.
Plan hints are an extension to the MDX language to indicate to the engine how to evaluate expressions.
EAGER causes the expression to be evaluated over the entire IIF subspace.
STRICT causes the expression to be evaluated only in the resulting subspace according to the results of the condition expression.
LAZY causes the expression to be evaluated in a cell-by-cell mode.
EAGER and STRICT are mutually exclusive in the hint; they can be used in the same IIF(,,) over different expressions.
See Performance Improvements for MDX in SQL Server 2008 Analysis Services for an extended explanation.
Examples
The following query shows a simple use of IIF inside a calculated measure to return one of two different string values when the measure Internet Sales Amount is greater or less than $10000:
WITH MEMBER MEASURES.IIFDEMO AS
IIF([Measures].[Internet Sales Amount]>10000
, "Sales Are High", "Sales Are Low")
SELECT {[Measures].[Internet Sales Amount],MEASURES.IIFDEMO} ON 0,
[Date].[Date].[Date].MEMBERS ON 1
FROM [Adventure Works]
A very common use of IIF is to handle 'division by zero' errors within calculated measures, as in the following example:
WITH
//Returns 1.#INF when the previous period contains no value
//but the current period does
MEMBER MEASURES.[Previous Period Growth With Errors] AS
([Measures].[Internet Sales Amount]-([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER))
/
([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER)
,FORMAT_STRING='PERCENT'
//Traps division by zero and returns null when the previous period contains
//no value but the current period does
MEMBER MEASURES.[Previous Period Growth] AS
IIF(([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER)=0,
NULL,
([Measures].[Internet Sales Amount]-([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER))
/
([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER)
),FORMAT_STRING='PERCENT'
SELECT {[Measures].[Internet Sales Amount],MEASURES.[Previous Period Growth With Errors], MEASURES.[Previous Period Growth]} ON 0,
DESCENDANTS(
[Date].[Calendar].[Calendar Year].&[2004],
[Date].[Calendar].[Date])
ON 1
FROM [Adventure Works]
WHERE([Product].[Product Categories].[Subcategory].&[26])
The following is an example of IIF returning one of two sets inside the Generate function to create a complex set of tuples on Rows:
SELECT {[Measures].[Internet Sales Amount]} ON 0,
//If Internet Sales Amount is zero or null
//returns the current year and the All Customers member
//else returns the current year broken down by Country
GENERATE(
[Date].[Calendar Year].[Calendar Year].MEMBERS
, IIF([Measures].[Internet Sales Amount]=0,
{([Date].[Calendar Year].CURRENTMEMBER, [Customer].[Country].[All Customers])}
, {{[Date].[Calendar Year].CURRENTMEMBER} * [Customer].[Country].[Country].MEMBERS}
))
ON 1
FROM [Adventure Works]
WHERE([Product].[Product Categories].[Subcategory].&[26])