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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
nolos
New Member

Use variable as column name

I'm completely lost here. I have this table called "Hours"

1.png

 

and I am trying to create a measure in my visual that shows the hours depending on the location. Like this.

hrs.png

So, whatever location is showing the the "location" row in the visual, I want to sum that column in the Hours table. Something like this

 

var loc = Locations[Location]

return

sum of hours = Sum(Hours[loc])

 

Is there anyway to use a variable as the culumn name so that the column that is used is the name of whats in the location section of the visual?

 

Thanks a lot!

 

4 REPLIES 4
Greg_Deckler
Super User
Super User

@nolos You need to unpivot your location columns in Power Query Editor. Then this is trivial.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Thanks for the fast reply.  I hope i'm right in thinking you are referring to each of the location columns in the Hours Table.  If so, the problem there is that this is a table I created in power BI.  I used measures to add the columns and so that table doesn't show in the Transform Data section.  Or am I doing it wrong?

 

Thanks again!

@nolos Well, there is a DAX Unpivot but it isn't pretty. I would have to understand your situation better but in general it's not possibly or at least easy to do what you are trying to do with the data formatted that way. You would have to do something like:

 

Measure = 
  SWITCH( 'Table'[location],
    "Appleton", SUMX( SELECTCOLUMNS( 'Data', "__Value", [Appleton] ), [__Value] ),
    "Boonville", SUMX( SELECTCOLUMNS( 'Data', "__Value", [Boonville] ), [__Value] ),
    "Butler", SUMX( SELECTCOLUMNS( 'Data', "__Value", [Butler] ), [__Value] ),
   ...
  )

DAX Unpivot - Microsoft Fabric Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

This may seem silly but is there any way to do something like this...but actually working?

 

Location1 = SELECTEDVALUE(Locations[location])

hrs = SUMX(Hours,[Location1])

 

 

or

 

hrs =
var loc = SELECTEDVALUE(Locations[location])
var tbl = CONCATENATE("Hours", CONCATENATE("[",CONCATENATE(loc,"]")))
return tbl

 

then drop the below measure in the visual?

avg = SUMX(Hours, [hrs])

 

I know neither of these work but I was hoping it migh spark a viable solution.

 

Thanks.

Helpful resources

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