0
votes

I have the expression in MDX

IIF
(
  IsEmpty
  (
        Filter
        (
          [Measures].[Actual  Hours]
         ,
          [WORK ACTUAL].[MAINT D].[Maintenance Code] = 'PRO'
        )
      / 
        [Measures].[Actual  Hours]
    * 100
  )
 ,0
 ,
      Filter
      (
        [Measures].[Actual  Hours]
       ,
        [WORK ORDER ACTUAL].[MAINT D].[Maintenance Code] = 'PRO'
      )
    / 
      [Measures].[Actual  Hours]
  * 100
)

I got the following error :

MdxScript(test)the divide function expects a string or numeric expression for the 1 argument. A tuple set expression was used .

3
I'd like to help but rather than guessing could you please tell me the desired result of this calculation - can you explain in words please? Also the context - is this part of a bigger script, or does this logic go into another tool e.g. ssrs ?whytheq

3 Answers

1
votes

The Filter function returns a set hence your error message:
https://msdn.microsoft.com/en-us/library/ms146037.aspx

Returns the set that results from filtering a specified set based on a search condition.

So you are trying to divide a set of members by a number.

To resolve it you need a numeric expression instead of a set. Numeric expressions are either of the following:

  1. A number
  2. A numeric measure
  3. A numeric formatted member property
  4. The result of a tuple

Probably number 4 is the trickiest concept in mdx for someone new to the language to understand.

As @BillAnton has mentioned this is a tuple and hence a numeric expression:

([WORK ACTUAL].[MAINT D].&[PRO],[Measures].[Actual  Hours])

This is also a tuple and numeric expression:

([WORK ACTUAL].[MAINT D].&[PRO])

It will simply use the measure that is used in the context of the script or the cubes default measure.

Also in the definition of Filter you can see that it's signature is as follows:

Filter(Set_Expression, Logical_Expression )

In your script you have the following

Filter
    (
      [Measures].[Actual  Hours] //<<measures are numeric_expression
     ,
      [WORK ACTUAL].[MAINT D].[Maintenance Code] = 'PRO'
    )

In terms of getting rid of the error here is 1 resolution:

IIF
(
      //10 is a numeric placeholder (you can replace with something that returns a number)
      10 / [Measures].[Actual  Hours]
    * 100
  = 0
 ,0
 ,
    //20 is a numeric placeholder (you can replace with something that returns a number)
    20 / [Measures].[Actual  Hours]
  * 100
)
1
votes

You're not using the Filter function correctly. Filter-function is typically used to filter a set of tuples by a measure or some other calculated logic. In order to filter a measure by a specific member (which is what I think you're trying to do), you can simply create a tuple...

([WORK ACTUAL].[MAINT D].&[PRO],[Measures].[Actual  Hours])

This calculates actual hours (using the defined aggregation function which is most likely SUM) using fact records associated to the [PRO] member of the [Maint D] attribute in the [Work Actual] dimension. Depending on how your dimension/attribute/etc is setup, this may not be the precise syntax...but hopefully you get the idea.

MEMBER [Measures].[<calc-measure-stage1>]
    IIF(
        [Measures].[Actual  Hours] = 0
        ,NULL
        ,(
            (
                 [WORK ACTUAL].[MAINT D].&[PRO]
                ,[Measures].[Actual  Hours]
            )
            /
            [Measures].[Actual  Hours]
         ) * 100
    )
MEMBER [Measures].[<calc-measure-stage2>]
    IIF(
        [Measures].[Actual  Hours] = 0
        ,NULL
        ,(
            (
                 [WORK ORDER ACTUAL].[MAINT D].&[PRO]
                ,[Measures].[Actual  Hours]
            )
            /
            [Measures].[Actual  Hours]
         ) * 100
    )
MEMBER [Measures].[<calc-measure>]
    IIF(
         IsEmpty([Measures].[<calc-measure-stage1>]
        ,[Measures].[<calc-measure-stage2>]
        ,[Measures].[<calc-measure-stage1>]
    )
0
votes

Use CoalesceEmpty(Expression, NumaricValue) for numeric measures

Also use DIVIDE(Numerator, Denominator, 0) which automatically handles devided by 0 exception