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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Can I set a column name as variable in DAX?

Hi there 🙂

 

I want to be able to change the column name referenced in a DAX formula - for example here I want to be able to change the column name where it says "YTD", so that this is based on a slicer selection

 

djalexr_0-1600355286864.png

Its easy enough to change the other values (e,.g. I made a formula that uses a slicer input instead of the hard coded "Turnover" value in the above formula...but I'm wondering if its possible to also make the column name in square brackets a variable? 

Is this possible?

 

Thank You!!

Alex

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

@Anonymous Not exactly within the square brackets, but you could try a SWITCH function to get the result you're after: 

 

AllisonKennedy_0-1600605935512.png

Here's an example I made up: 

 

Test Slicer Column =


SUMX(FILTER('Blank Values',
VAR _Column =
SWITCH(SELECTEDVALUE(Slicer[Column Select])
, "Year", 'Blank Values'[Year]
, "Key", 'Blank Values'[Key])
RETURN
CONTAINSSTRING(_Column ,"2018")), 'Blank Values'[Value])

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Thanks very much for this!  Yes I was able to work around this using the SWITCH function.

 

Thanks again

 

Alex

You're welcome!

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

AllisonKennedy
Super User
Super User

@Anonymous Not exactly within the square brackets, but you could try a SWITCH function to get the result you're after: 

 

AllisonKennedy_0-1600605935512.png

Here's an example I made up: 

 

Test Slicer Column =


SUMX(FILTER('Blank Values',
VAR _Column =
SWITCH(SELECTEDVALUE(Slicer[Column Select])
, "Year", 'Blank Values'[Year]
, "Key", 'Blank Values'[Key])
RETURN
CONTAINSSTRING(_Column ,"2018")), 'Blank Values'[Value])

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

I know I'm a Very Late to the party. But is it feasible to obtain the pbix example file that does this?

don't worry , I find it how to do it ? 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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