Export (0) Print
Expand All
Expand Minimize

IIf (MDX)

Returns one of two values determined by a logical test.


IIf(Logical_Expression, Expression1, Expression2)

Logical_Expression

A valid Multidimensional Expressions (MDX) logical expression that evaluates to true or false.

Expression1

A valid Multidimensional Expressions (MDX) expression.

Expression2

A valid Multidimensional Expressions (MDX) expression.

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.

NoteNote

In Microsoft SQL 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 Server Analysis 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.

NoteNote

If either expression evaluates to NULL, the result set will be NULL when that condition is met.

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])

Lastly, this example shows how to use Plan Hints:

WITH MEMBER MEASURES.X AS

IIF(

[Measures].[Internet Sales Amount]=0

, NULL

, (1/[Measures].[Internet Sales Amount]) HINT EAGER)

SELECT {[Measures].x} ON 0,

[Customer].[Customer Geography].[Country].MEMBERS ON 1

FROM [Adventure Works]

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft