You are on page 1of 13

(HTTPS://WWW.SQLBI.COM/)  TRAINING (HTTPS://WWW.SQLBI.

COM/TRAINING

 ALL ARTICLES  https://sql.bi/53917 (https://sql.bi/53917)  72    


Using the
SELECTEDVALUE
function in DAX
(https://www.sqlbi.com/articles/using-the-selectedvalue-function-in-dax/)

This article describes how the SELECTEDVALUE DAX


function simpli es the syntax required in many scenarios
where you need to read a single value selected in the lter
context.

The DAX language is growing thanks


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:

IF ( HASONEVALUE ( Table[column] ), VALUES ( Table[column]


IF ( HASONEVALUE ( Table[column] ), VALUES ( Table[column]
IF ( HASONEVALUE ( Table[column] ), VALUES ( Table[column]

 DAX  CODE FORMAT CODE WITH GET YOUR SEAT


 COPY
CONVENTIONS #2

(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.

It is important to remember that:

The SELECTEDVALUE (https://dax.guide/selectedvalue/) and


VALUES (https://dax.guide/values/) functions read the current
lter context, not the row context;

When you have a row context, just use a column reference


(within RELATED (https://dax.guide/related/) in case it is in a
lookup table);
If the lter context returns zero rows for the referenced column,
then SELECTEDVALUE (https://dax.guide/selectedvalue/) returns
the second argument – do not assume that the second argument
is returned only when two or more values are selected;

If you use a version of DAX that does not have SELECTEDVALUE


(https://dax.guide/selectedvalue/), you can use the same pattern
as that described in this article, replacing SELECTEDVALUE
(https://dax.guide/selectedvalue/) with the corresponding syntax
using HASONEVALUE (https://dax.guide/hasonevalue/) /
VALUES.

Sample Uses
The Power BI le you can download contains all the examples
described in this section.

Retrieving a column from the


same table
You can retrieve the attribute of an entity (e.g. the current class of a
product) displayed in a matrix, making sure you are not going to
duplicate the rows in case there are more attribute values available for
that same selection.

Product Class := SELECTEDVALUE ( 'Product'[Class] )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #3
(https://www.sqlbi.com/wp-content/uploads/SelectedValue-01.png)

This measure is useful when you navigate using a Matrix or a


PivotTable. However, the measure displays data even for products that
have no sales. This does not happen when you use a Table visual in
Power BI.

(https://www.sqlbi.com/wp-content/uploads/SelectedValue-02.png)

If you want to show the class of a product only when there is a


measure available, you should add a condition as in the two following
examples:
1 Product Class v1 :=
2 IF (
3     NOT ISBLANK ( [Sales Amount] ),
4     SELECTEDVALUE ( 'Product'[Class] )
5 )
6  
7 Product Class v2 :=
8 IF (
9     NOT ISEMPTY ( Sales ),
10     SELECTEDVALUE ( 'Product'[Class] )
11 )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #4

(https://www.sqlbi.com/wp-content/uploads/SelectedValue-03.png)

Retrieving a column from a


lookup table
If you want to retrieve the corresponding attribute in a lookup table,
you probably need to modify the propagation of the lter context
through relationships. When you have a row context you would use a
single RELATED (https://dax.guide/related/) function to retrieve the
corresponding row context in a lookup table, regardless of the number
of relationships to travel. For example, consider the case of a
snow ake dimension featuring the Product, Product Subcategory, and
Product Category tables. If you want to get the current category of a
product in a Matrix or in a PivotTable, you need the following code:
1 Product Category :=
2 CALCULATE (
3     SELECTEDVALUE ( 'Product Category'[Category] ),
4     CROSSFILTER (
5         'Product'[ProductSubcategoryKey],
6         'Product Subcategory'[ProductSubcategoryKey]
7         Both
8     ),
9     CROSSFILTER (
10         'Product Subcategory'[ProductCategoryKey],
11         'Product Category'[ProductCategoryKey],
12         Both
13     )
14 )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #5

In this case, a simpler syntax could be the one provided by the


expanded table:

1 Product Category exp.table :=


2 CALCULATE (
3     SELECTEDVALUE ( 'Product Category'[Category] ),
4     'Product'
5 )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #6

However, remember that the expanded table might have undesired


side effects in complex data models, so the CROSSFILTER
(https://dax.guide/cross lter/) solution should be preferred. Both
measures return the same result.

(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 )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #7

(https://www.sqlbi.com/wp-content/uploads/SelectedValue-05.png)

Parameter table pattern


If you consider the parameter table pattern
(http://www.daxpatterns.com/parameter-table/), you can implement a
measure using a parameter by using SELECTEDVALUE
(https://dax.guide/selectedvalue/) writing this:
1 Sales by Scale :=
2 DIVIDE (
3     [Sales Amount],
4     SELECTEDVALUE ( 'Scale'[Scale], 1 )
5 )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #8

Instead of that:

1 Sales by Scale :=
2 DIVIDE (
3     [Sales Amount],
4     IF ( HASONEVALUE ( Scale[Scale] ), VALUES ( Scale
5 )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #9

The result is the same, you just simplify your code.

(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).

1 Sales by Scale Checked :=


2 DIVIDE (
3     [Sales Amount],
4     SELECTEDVALUE ( 'Scale'[Scale], ERROR ( "Single s
5 )

 DAX  CODE FORMAT CODE WITH


 COPY
CONVENTIONS #10
(https://www.sqlbi.com/wp-content/uploads/SelectedValue-07.png)

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

 SUBSCRIBE TO RSS Marco is a business intelligence consultant and me


(HTTPS://FEEDS.FEEDBURNER.COM/SQLBI_BLOG) Analysis Services in 1998, back when Analysis Ser
VIEW FULL PROFILE (HTTPS://WWW.SQLBI.COM/AUTHOR/MARCO-RUSS

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/)  

 ARTICLE – DAX 101

Year-to-date ltering weekdays in


(https://www.sqlbi.com/articles/y
date- ltering-weekdays-in-dax/)
Time intelligence functions oftentimes hide an auto
statement meant to make time intelligence calculat
This article describes this behavior and what to do
ends up breaking your calculation.
 READ MORE (HTTPS://WWW.SQLBI.COM/ARTICLES/YEAR-TO-DATE-FILTERI
(https://www.sqlbi.com/p/mastering-dax-video-course/)
 VIDEO COURSE

Mastering DAX Video Course


(https://www.sqlbi.com/p/mastering-
dax-video-course/)
TRY FOR FREE
(HTTPS://WWW.SQLBI.COM/P/MASTERING-DAX-
VIDEO-COURSE/)

(/p/power-bi-dashboard-design-course/)

72 Comments SQLBI 
1 Login

 Recommend t Tweet f Share Sort by Best


๕ Enclose code in comments with <PRE></PRE> to preserve indentation

Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Sergey Kompaniets
− ⚑
2 years ago
EXPAND COMMENTS

Newsletter What is next


Mastering DAX
Your email address SUBSCRIBE (https://www.sqlbi.com/p/mastering
dax-orange-county-costa-mesa-
 By pressing the Subscribe button you are agree to our Privacy Policy. may-2019/) – May 15
(https://www.sqlbi.com/privacy/) , Orange County / Costa Mesa  
(https://www.sqlbi.com/ics/?
id=79022)

Get BI news and original content in your Mastering DAX

65.8K inbox every 2 weeks!


(https://www.sqlbi.com/p/mastering
dax-workshop-helsinki-2019/) –
 subscribers NEWSLETTER ARCHIVE (HTTPS://WWW.SQLBI.COM/NEWSLETTER/) May 21, Helsinki  
(https://www.sqlbi.com/ics/?
id=75963)
Mastering DAX
(https://www.sqlbi.com/p/mastering
dax-workshop-cincinnati-may-
2019/) – May 21, Cincinnati  
(https://www.sqlbi.com/ics/?
id=76139)
Data Modeling for Power BI
(https://www.sqlbi.com/p/data-
modeling-for-power-bi-calgary-
june-2019/) – Jun 3, Calgary  
(https://www.sqlbi.com/ics/?
id=78967)
Microsoft Business Applications
Summit 2019
(https://www.microsoft.com/en-
us/businessapplicationssummit) –
Jun 10, Atlanta  
(https://www.sqlbi.com/ics/?
id=104410)
Mastering DAX
(https://www.sqlbi.com/p/mastering
dax-workshop-new-york-june-
2019/) – Jun 12, New York  
(https://www.sqlbi.com/ics/?
id=78437)
Mastering DAX
(https://www.sqlbi.com/p/mastering
dax-workshop-aarhus-june-2019/)
– Jun 18, Aarhus  
(https://www.sqlbi.com/ics/?
id=74035)
(https://www.sqlbi.com/) (https://dax.guide/)

(https://www.daxpatterns.com/) (https://www.daxformatter.com/)

(https://okviz.com/) (https://synoptic.design/)

You might also like