Daily DAX : Day 318 SELECTEDVALUE
DAX SELECTEDVALUE Function
Description
The SELECTEDVALUE
function in Power BI DAX (Data Analysis Expressions) returns the value of a column when the context has been filtered down to a single distinct value. If no single value is selected or multiple values are present, it can return a default value or a blank.
Syntax
SELECTEDVALUE(<ColumnName> [, <AlternateResult>])
- ColumnName: The column from which to retrieve the selected value.
- AlternateResult (optional): The value to return if no single distinct value is found. If not specified, returns BLANK().
Return Value
The single selected value from the specified column, or the alternate result (if provided) when no single value exists.
Use Cases
- Dynamic Measures: Use in measures to return a value based on a slicer or filter selection.
- Conditional Logic: Simplify DAX formulas by checking for a single selected value instead of complex filtering.
- Dynamic Titles: Create dynamic titles or text in visuals based on user selections.
Example
Suppose you have a table Sales
with a column Region
. You want to display the selected region in a card visual.
SelectedRegion = SELECTEDVALUE(Sales[Region], "No Region Selected")
Behavior:
- If a single region (e.g., "North") is selected in a slicer, it returns "North".
- If no region or multiple regions are selected, it returns "No Region Selected".
Notes
SELECTEDVALUE
is a shorthand for common scenarios where you might otherwise useHASONEVALUE
andVALUES
.- It works within the current filter context, typically set by slicers, filters, or visuals.
- If multiple values are present and no alternate result is specified, it returns BLANK().
Example in a Measure
DynamicSales =
VAR SelectedRegion = SELECTEDVALUE(Sales[Region], "All Regions")
RETURN
CALCULATE(SUM(Sales[Amount]), Sales[Region] = SelectedRegion)
This measure calculates the total sales for the selected region or all regions if no single region is selected.
Comments
Post a Comment