Forum Discussion

taingw4's avatar
taingw4
Regular Visitor
8 years ago
Solved

Does HASONEVALUE() not work inside a Calculated Column?

I am trying to use HASONEVALUE() in a Calculated Column to show a column value if a certain field is selected, or otherwise show 0 if that field is not selected.   I tried writing the results as si...
  • v-yulgu-msft's avatar
    8 years ago

    Hi taingw4,

     

    Calculated column is initialized once it has been created regardless of slicer selection. To create a conditional column depend on slicer, you'd better use measure. 

     

    If you only need to show result in a card visual, you can create measure like:

    HASONEVALUE measure2 = IF(HASONEVALUE(Test_1[Test_1 Cat]),1,0)

     

    If you want to display measure value as a column in a table visual, please try this:

    HASONEVALUE measure = CALCULATE(IF(HASONEVALUE(Test_1[Test_1 Cat]),1,0),ALLSELECTED(Test_1))

     

    Best regards,
    Yuliana Gu

  • TomMartens's avatar
    TomMartens
    8 years ago

    Hey,

     

    if you are creating a measure you have to be always aware of the following

    • a measure returns a scalar value, meaning just one value, by using a certain expression
    • the expression is applied to the rows filtered down by slicers, column and row headers

    For this reason you have to specify an aggregate function, even if you are certain, that there is just one row, e.g. by testing using HASONEVALUE...

     

    IF the check HASONEVALUE('table1'[Column]) is true, and you know that this means that there is also just one value from 'table2'[column2] selected - from a logical point of view, w/o the necessity that there is some filter propagation happening, you have to, but also can safely use aggregate functions.

     

    If table2 is on the one-side of a relationship you also can use this formula

    RELATED('table2'[column2])

     

    Hopefully this helps

     

    Regards

    Tom