DAX SelectedValue function

With the new Power BI desktop release (July 2017) comes a new DAX function called “SelectedValue”, this DAX function is a convenience feature to help you with working with selections. Today when you want to use the value of a selection you always have to be careful, to get the value of the selection you can use the VALUES function, this works great when you always have a single value but when more than one value is selected you get an error message like this:

MdxScript(Model) (171, 30) Calculation error in measure ‘Invoice'[Measure]: A table of multiple values was supplied where a single value was expected.

To solve this you need to write you measure in combination with HasOneValue to check if you only have a single value:

If (HasOneValue(<column reference>), Values(<column reference>), <default value>)

With this months Power BI release you can write it much simpler using this:

SelectedValue(<column reference>,<default value>)

When multiple values are returned the function will return BLANK but what is great is that you now also easily add a default value yourself so can do something like this:

PS as you can see there is a squiggly under year, there is currently a bug in the intellisence that causes this. You can just use any column in the function.