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

Dynamic field names in mesures

Let’s say I’ve below areas as a table value.

Area

USA

UK

AUS

CAN

 

I’ve created a measure for selected item from the Area table. (It’s always one selected in slicer).

SelectedArea = SelectedValue(Area)

Now, suppose my fact table has different sales columns for each area like below.

Sales

Sales_USA

Sales_UK

Sales_AUS

Sales_CAN

 

USA Sales := Sum(Sales_USA)

So, my question is here instead of creating Sales for each area, can we automate this something like below?
Sales := Sum(Sales_[SelectedArea])

Based on the selection in Area column the measure should update.

I can create measure of each area and use switch statement but the problem is I’ve more than 50 different measures.

Any help would be appreciated.

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

Sorry for our delay in response, but it does not support to calculate dynamic formula/field in measure, but we can use "unpivot" in power query editor to meet your requirement:

 

3.jpg4.jpg5.jpg6.jpg7.jpg8.jpg9.jpg

 

 

If you do not want to change the construction of data, we can use switch to calculate for each area:

 

 

Sales =
SUMX (
    DISTINCT ( Country[Country] ),
    SWITCH (
        [Country],
        "USA", SUM ( Sales[Sales_USA] ),
        "BRA", SUM ( Sales[Sales_BRA] ),
        "CAN", SUM ( Sales[Sales_CAN] ),
        "JPN", SUM ( Sales[Sales_JPN] )
    )
)

 


By the way, PBIX file as attached.


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

9 REPLIES 9
Ashish_Mathur
Super User
Super User

Hi,

Your data is not well arranged.  You should unpivot your data to have all Countries appear in 1 column only and in another column you have have the sales figures.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @Ashish_Mathur , I took the the above data just an example to explain the scenario but in reality the data and the usecase is different and we can't change the data foramt for various reasons. 

Anonymous
Not applicable

I've created a measure as below.

Sales =
var SelectedCountry = SELECTEDVALUE(Country[Country])
var Exp = "Sum(Sales[Sales_"&SelectedCountry&"])" // this is returning Sum(Sales_USA)
return CALCULATE(Exp) //here the return statement not evaluating the Exp variable instead it's just dislaying Sum(Sales_USA)
 
Is there anyway to evaluate the variable inside calculate?
Annotation 2020-04-02 105214.png

Hi @Anonymous ,

 

Sorry for our delay in response, but it does not support to calculate dynamic formula/field in measure, but we can use "unpivot" in power query editor to meet your requirement:

 

3.jpg4.jpg5.jpg6.jpg7.jpg8.jpg9.jpg

 

 

If you do not want to change the construction of data, we can use switch to calculate for each area:

 

 

Sales =
SUMX (
    DISTINCT ( Country[Country] ),
    SWITCH (
        [Country],
        "USA", SUM ( Sales[Sales_USA] ),
        "BRA", SUM ( Sales[Sales_BRA] ),
        "CAN", SUM ( Sales[Sales_CAN] ),
        "JPN", SUM ( Sales[Sales_JPN] )
    )
)

 


By the way, PBIX file as attached.


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
camargos88
Community Champion
Community Champion

Hi @Anonymous ,

 

I create a pbix file with an example.

I hope this file helps you: Download PBIX 

 

 

Ricardo



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

Proud to be a Super User!



Anonymous
Not applicable

@camargos88 Thanks for your quick response. I know this is one way of doing it. But I wanted to completly avoid creating bunch of measures or If conditions. Essentially I want to create a single measure and dynamically update the column name based on the selections in area slicer.

 

SelectedValue = SelectedValue(area)

Current: Sum(Sales_USA) (for each area)

Expected: Sales := Sum(Sales_[SelectedValue])

 

Suppose If I selected USA, my sales measure should become Sum(Sales_USA) and If i select UK it should become Sum(Sales_UK). Simply I want to control [SelectedValue] based on selection in area slicer. So this way I can avoid creating so many measures or if conditions. 
Is it possible to use varible or measure in the column names? 

@Anonymous ,

 

I don't know how to do it without if.

I sent you an example that you can apply for n number of columns in 1 measure. You don't need to create a lot of measures.

 

I hope it helps,

 

Ricardo

 



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

Proud to be a Super User!



Anonymous
Not applicable

I understand but the probelm is i'm using direct query with 250millions records. writing more if conditions severely affects the performance. I'm looking for more elegant solution.

@Anonymous ,

 

I don't see a problem using if, once you are gonna filter it and measures work only on the fly.

Does your database have 1 column per country or are you pivoting on your query ?

 

Ricardo



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

Proud to be a Super User!



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.