Stop 09 · Advanced DAX PatternsTopics · 5/10

SELECTEDVALUE and HASONEVALUE

Part of the Power BI Roadmap

In short

A report title that says "Sales for Pune" when Pune is picked, and "Sales for All Cities" otherwise, feels smart. These two functions let measures ask "has the user picked exactly one thing?"

SELECTEDVALUE returns the value of a column when exactly one value is in the filter context, or an alternate result otherwise. HASONEVALUE returns TRUE when only one value is filtered.

The jaadu, dressed as a waiter, points at a single bowl on a big round thali plate while holding a small notepad.

Think of it like this

The thali order

The waiter asks what you'd like. If you point at exactly one dish, he writes it down. If you say "everything" or point at three dishes, he writes "full thali". HASONEVALUE is the waiter checking "just one?", and SELECTEDVALUE is him writing either the dish name or the fallback.

Watch it happen

Two ways to ask the same question

HASONEVALUE

  • Returns TRUE or FALSE
  • TRUE only when one value is in the filter context
  • Used as the test inside IF
  • Great for hiding numbers on total rows

SELECTEDVALUE

  • Returns the value itself
  • Gives the alternate result when zero or many values
  • Shortcut for IF ( HASONEVALUE ( c ), VALUES ( c ), alt )
  • Great for titles and parameters

Use SELECTEDVALUE when you want the value, HASONEVALUE when you only need to know whether one is selected.

The big ideas

What you need to know

Dynamic titles

Combine text with SELECTEDVALUE and bind the measure to a visual title using the fx button.

Always give a fallback

Without the second argument SELECTEDVALUE returns BLANK when many values are selected, which makes empty titles.

Guard total rows

Totals have many values in context. IF ( HASONEVALUE ( ... ), ... ) keeps a per-item calculation from showing nonsense there.

Read slicer choices

SELECTEDVALUE on a disconnected table column turns a slicer pick into a number a measure can use.

See it in code

Walk through the code

dax
1Sales Title =2"Sales for " & SELECTEDVALUE ( Customer[City], "All Cities" )3 4Selected Discount =5SELECTEDVALUE ( 'Discount Options'[Discount %], 0 )6 7Sales After Discount =8[Total Sales] * ( 1 - [Selected Discount] )9 10Avg Sale per Store =11IF (12    HASONEVALUE ( Store[Store Name] ),13    DIVIDE ( [Total Sales], [Order Count] ),14    BLANK ()15)

1/4One city picked gives "Sales for Pune". None or several gives "Sales for All Cities".

A dynamic title, a slicer-driven discount and a per-store figure hidden on totals.

Save yourself the pain

Common mistakes

Oops

Using VALUES ( Customer[City] ) in a title measure.

Do this instead

VALUES returns a table, and errors when more than one city is selected. SELECTEDVALUE handles that safely.

Oops

Leaving out the alternate result, then wondering why the title vanishes.

Do this instead

Always pass a sensible fallback like "All Cities".

Your mission

A title that listens

~15 min

Quick check

Two cities are selected in a slicer. What does SELECTEDVALUE ( Customer[City], "Many" ) return?

Go deeper

  1. 1Build a dynamic title like "Sales for " & SELECTEDVALUE ( Store[City], "All Cities" ).
  2. 2Use HASONEVALUE to hide a measure on total rows.
  3. 3Read a what-if parameter's value with SELECTEDVALUE.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge