Professional Documents
Culture Documents
COM/TRAINING
Using the
SELECTEDVALUE
function in DAX
(https://www.sqlbi.com/articles/using-the-selectedvalue-function-in-dax/)
to the monthly updates of Power BI,
which gradually introduce new
features later made available also in
Analysis Services and Power Pivot.
Learning DAX
The July 2017 release of Power BI
from scratch?
Desktop includes a new tool function called SELECTEDVALUE. This Read our DAX
function does not introduce anything new – it just simpli es the syntax learning guide!
required in common coding patterns, as you will see in a few of the
START HERE
possible uses presented after the description of the SELECTEDVALUE (HTTPS://WWW.SQLBI.COM/GU
(https://dax.guide/selectedvalue/) syntax.
SELECTEDVALUE
(https://dax.guide/selectedvalue/)
Syntax
The function SELECTEDVALUE (https://dax.guide/selectedvalue/)
returns the value of the column reference passed as rst argument if it
is the only value available in the lter context, otherwise it returns
blank or the default value passed as second argument. Here are a few
examples of possible syntax.
SELECTEDVALUE ( Table[column] )
SELECTEDVALUE ( Table[column], "default value" )
SELECTEDVALUE ( Table[column], 0 )
May 15-17, 2019
DAX CODE FORMAT CODE WITH
COPY
CONVENTIONS #1 Orange County / Cos
LAST WEEK TO
Internally, SELECTEDVALUE (https://dax.guide/selectedvalue/) is just REGISTER
syntax sugar generating the following corresponding syntaxes:
(http://www.sqlbi.com/p/mastering
You should use SELECTEDVALUE (https://dax.guide/selectedvalue/) in dax-orange-county-costa-
all those cases when you need to read a single value selected in the mesa-may-2019/)
lter context, obtaining blank or another default value in all other
cases.
Sample Uses
The Power BI le you can download contains all the examples
described in this section.
(https://www.sqlbi.com/wp-content/uploads/SelectedValue-02.png)
(https://www.sqlbi.com/wp-content/uploads/SelectedValue-03.png)
(https://www.sqlbi.com/wp-content/uploads/SelectedValue-04.png)
Using a numeric column in a
calculation
The SELECTEDVALUE (https://dax.guide/selectedvalue/) function
simpli es the syntax required when you use a numeric column of an
entity as a parameter in a calculation. For example, the following
measure calculates the quantity dividing the existing Sales Amount
measure by the Unit Price value of the selected product. In case more
products are selected, the blank result of SELECTEDVALUE
(https://dax.guide/selectedvalue/) automatically propagates into the
result of Calc Quantity.
1 Calc Quantity :=
2 DIVIDE (
3 [Sales Amount],
4 SELECTEDVALUE ( 'Product'[Unit Price] )
5 )
(https://www.sqlbi.com/wp-content/uploads/SelectedValue-05.png)
Instead of that:
1 Sales by Scale :=
2 DIVIDE (
3 [Sales Amount],
4 IF ( HASONEVALUE ( Scale[Scale] ), VALUES ( Scale
5 )
(https://www.sqlbi.com/wp-content/uploads/SelectedValue-06.png)
If you want to avoid a multiple selection, you could use the ERROR
(https://dax.guide/error/) function, so the visual displays a proper error
message (even if the reference to MdxScript seems like nonsense in a
DAX client such as Power BI Desktop).
Conclusion
The SELECTEDVALUE (https://dax.guide/selectedvalue/) function
simpli es the syntax of a common pattern involving two functions
(HASONEVALUE (https://dax.guide/hasonevalue/) and VALUES
(https://dax.guide/values/)) to retrieve a value from the lter context.
Just remember that this technique is not required when you have a
row context, because you can simply use a column reference and the
RELATED (https://dax.guide/related/) function when a lookup table is
involved.
Download
Insert your email address and press Download for access to the les used in this article.
DOWNLOAD
(HTTP: //WWW.SQLBI.COM/WP-
Your email ad
CONTENT/UPLOADS/SELECTEDVALUE-
IN-DAX.ZIP)
Keep me informed about BI news and upcoming articles with a bi-weekly
newsletter (uncheck if you prefer to proceed without signing up for the newsletter)
By pressing the Download button you are agree to our Privacy Policy.
(https://www.sqlbi.com/privacy/)
WRITTEN BY
Marco Russo
(https://www.s
russo/)
PUBLISHED ON JUL 11, 2017
RECOMMENDED
DAX(HTTPS://WWW.SQLBI.COM/TOPICS/DAX/)
FILTER CONTEXT(HTTPS://WWW.SQLBI.COM/TOPICS/FILTER-CONTEXT/)
POWER BI(HTTPS://WWW.SQLBI.COM/TOPICS/POWER-BI/)
(/p/power-bi-dashboard-design-course/)
72 Comments SQLBI
1 Login
Name
Sergey Kompaniets
− ⚑
2 years ago
EXPAND COMMENTS
(https://www.daxpatterns.com/) (https://www.daxformatter.com/)
(https://okviz.com/) (https://synoptic.design/)