Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Use If() function as measure or calculated column with different results

I am trying to use IF() function to create a measure and put it into a matrix, but the following two screenshots showed me no column selection when I am trying to give a condition statement to IF() function. The column I am going to refer to is "S/T" (which shows in the third graph, it is a column created by PowerQuery Editor). 

The complete measure that I would like to make is as below:

Turnaround Time / Box = 
    IF('new vessel'[S/T]="S", 
       CALCULATE(AVERAGEX('new vessel', 
                 'new vessel'[New Off Chassis Time]-'new vessel'[New Hot Time]),
                 ALLEXCEPT('new vessel','new vessel'[Tractor Cycle Id])
                ), 
       CALCULATE(AVERAGEX('new vessel', 
                 'new vessel'[New Off Chassis Time]-'new vessel'[New Hot Time]),
                 ALLEXCEPT('new vessel', 'new vessel'[Tractor Cycle Id])
                )
      )

 

 

To handle this situation, I tried to turn into using calculated column instead of using measure. And turns out it works.

 

Can someone explain a little bit why the IF() function is so interesting that it works only in calculated column? Or how can I do to make it work in measure?

 

 

 

  • Because in the measures there is no row context, but only filter context.

     

    This is why you need a column as first parameter of the IF function if you want to create a calculated column, instead if you want to create a measure, the first parameter does not accept a column.

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • Hi Anonymous ,

    try to use in the measure

    IF(SELECTEDVALUE('new vessel'[S/T]) = "S"

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Nice, it works. But I am wondering why it works, and why my original approach doesn't work? 

  • Because in the measures there is no row context, but only filter context.

     

    This is why you need a column as first parameter of the IF function if you want to create a calculated column, instead if you want to create a measure, the first parameter does not accept a column.

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.