Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
rwalker
New Member

Selecting a column from a selected value

Hi, I am completely tearing my hair out here. I have no idea why this doesn't work. 

 

Basically, I am trying to select a column based on a selected value (which is in turn, chosen via slicer) using a switch statement. Here is my DAX code that DOES NOT WORK (Table & column names edited for anonymity):

SelectedColumn =
var quarter = SELECTEDVALUE('Date Look-Up'[Quarter])

RETURN
    SWITCH(
    quarter,
    "Q1",'MyTable'[Column Q1],
    "Q2",'MyTable'[Column Q2],
    "Q3",'MyTable'[Column Q3],
    "Q4",'MyTable'[Column Q4]
    )

Here some of the changes I made to troubleshoot:

1) Changing the variable "quarter" to one of the values used in the SWITCH statement:

SelectedColumn =
var quarter = "Q4"

RETURN
    SWITCH(
    quarter,
    "Q1",'MyTable'[Column Q1],
    "Q2",'MyTable'[Column Q2],
    "Q3",'MyTable'[Column Q3],
    "Q4",'MyTable'[Column Q4]
    )

Works! But, I don't want to just select 1 value, I want the selected value to change. So, no good. It proves that the switch statement CAN work.

2) Converting the SELECTEDVALUE to a string:

SelectedColumn =
var quarter = CONVERT(SELECTEDVALUE('Date Look-Up'[Quarter]),STRING)

RETURN
    SWITCH(
    quarter,
    "Q1",'MyTable'[Column Q1],
    "Q2",'MyTable'[Column Q2],
    "Q3",'MyTable'[Column Q3],
    "Q4",'MyTable'[Column Q4]
    )

DOESN'T WORK

3) Making a test measure where it outputs a number depending on the SELECTEDVALUE:

TestMeasure =
var quarter = SELECTEDVALUE('Date Look-Up'[Quarter])

RETURN
    SWITCH(
    quarter,
    "Q1",1,
    "Q2",2,
    "Q3",3,
    "Q4",4
    )

WORKS! But I don't need numbers, I need it to select the column. (Note, this measure also works if I replace each letter with an aggregation, for example SUM('MyTable'[Column Q1]), but I don't want an aggregation, I want the whole column.)

4) Using the TestMeasure to return a number which is then fed into the switch statement for selecting a column

TestMeasure =
var quarter = SELECTEDVALUE('Date Look-Up'[Quarter])

RETURN
    SWITCH(
    quarter,
    "Q1",1,
    "Q2",2,
    "Q3",3,
    "Q4",4
    )
 
And then,

SelectedColumn =
var quarter = MyTable[TestMeasure]

RETURN
    SWITCH(
    quarter,
    1,'MyTable'[Column Q1],
    2,'MyTable'[Column Q2],
    3,'MyTable'[Column Q3],
    4,'MyTable'[Column Q4]
    )
 
DOESN'T WORK

SO, selecting a column based on a switch statement works IF the variable is set to a constant.
AND, I can output different values based on the SELECTEDVALUE using a switch statement (in a measure)
BUT I can't combine these two in order to select a column with a SWITCH statement!!!

What is happening here??? I feel like I'm going completely insane!!! Help!!!

4 REPLIES 4
RameezM1984
New Member

Hello 

 

Did u get the solution for the above?

I am also facing the same problem..if you have a solution ..kindly share 

DataInsights
Super User
Super User

@rwalker,

 

Are you trying to dynamically select a column to display in a visual? If so, field parameters are a great option:

 

https://learn.microsoft.com/en-us/power-bi/create-reports/power-bi-field-parameters 

 

Or, are you trying to dynamically select a column's value for further use in your measure? It's worth noting that a calculated column can't recognize a slicer selection, so a measure is the only option.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




I am trying to dynamically select a column to display in a visual. I clicked your link but for me there is no "Preview Features" in the options menu, so I can't do it.

 

Is there a way to do this with a measure instead of a calculated column? I want all the rows to display in a visual, not an aggregation.

@rwalker,

 

What version of Power BI Desktop do you have? If you don't see "Field parameters" below, upgrade Power BI Desktop:

 

DataInsights_0-1722454793934.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.