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
Newcolator
Helper II
Helper II

Putting a table and column name into a variable

Hi!

 

This is doing my head in, maybe because it's not possible. I've got this DAX:

 

Filter Test =
VAR AvailableValuesList = CALCULATETABLE(VALUES(DIM_GROUP[Channel]), REMOVEFILTERS(DIM_GROUP[Channel]))
VAR AvailableValuesCount = COUNTROWS(AvailableValuesList)
VAR SelectedValuesList = VALUES(DIM_GROUP[Channel])
VAR SelectedValuesCount = COUNTROWS(SelectedValuesList)
RETURN SelectedValuesCount & "/" & AvailableValuesCount
 
I am returning a string which shows the number of values selected in a slicer over the total number of values available. This works fine.
 
I need this DAX for many columns, so ideally I don't want to have to update the column reference many times in the expression. I'm trying to use a variable to store the column reference. Like this:
 
Filter Test =
VAR ColumnReference = DIM_GROUP[Channel]
VAR AvailableValuesList = CALCULATETABLE(VALUES(ColumnReference), REMOVEFILTERS(ColumnReference))
VAR AvailableValuesCount = COUNTROWS(AvailableValuesList)
VAR SelectedValuesList = VALUES(ColumnReference)
VAR SelectedValuesCount = COUNTROWS(SelectedValuesList)
RETURN SelectedValuesCount & "/" & AvailableValuesCount
 
But it's not happy: A single value for column 'Channel' in table 'DIM_GROUP' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single....
 
I've tried 
VAR ColumnReference = SELECTCOLUMNS('DIM_GROUP', [Channel])
but that doesn't work either. 
 
Is this even possible?
 
The ideal would be to have the multi-line dax expression held just once and then I can pass column references to it, but solving this first problem would be enough.
 
Any help? Many thanks.
1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@Newcolator 

just so I understand, you are hoping for a user defined function with column name as parametrs in dax. Something like

// Function to sum up values in a specified column
function sumColumn(data, column) {
    return data.reduce((sum, row) => sum + (row[column] || 0), 0);
}

// Example usage
const data = [{'colA': 1, 'colB': 2}, {'colA': 3, 'colB': 4}];
const sumColA = sumColumn(data, 'colA');
const sumColB = sumColumn(data, 'colB');

console.log(`Sum of colA: ${sumColA}`); // Output: Sum of colA: 4
console.log(`Sum of colB: ${sumColB}`); // Output: Sum of colB: 6

 

 But I don't think in the current capacity of dax, this is possible. However, there is a buzz  about udf coming to dax. Not sure what functionalities it will offer.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

3 REPLIES 3
v-sgandrathi
Community Support
Community Support

Hi @Newcolator,

Thank you @smpa01 for your response.

Has your issue been resolved?If the response provided by the community member addressed your query, could you please confirm? It helps us ensure that the solutions provided are effective and beneficial for everyone.

If yes, kindly accept the useful reply as a solution and give us Kudos. It would be appreciated.

Thank you for your understanding!

smpa01
Super User
Super User

@Newcolator 

just so I understand, you are hoping for a user defined function with column name as parametrs in dax. Something like

// Function to sum up values in a specified column
function sumColumn(data, column) {
    return data.reduce((sum, row) => sum + (row[column] || 0), 0);
}

// Example usage
const data = [{'colA': 1, 'colB': 2}, {'colA': 3, 'colB': 4}];
const sumColA = sumColumn(data, 'colA');
const sumColB = sumColumn(data, 'colB');

console.log(`Sum of colA: ${sumColA}`); // Output: Sum of colA: 4
console.log(`Sum of colB: ${sumColB}`); // Output: Sum of colB: 6

 

 But I don't think in the current capacity of dax, this is possible. However, there is a buzz  about udf coming to dax. Not sure what functionalities it will offer.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Yes, that would be very useful. If it's not possible at the moment that's fine. I will stop trying to make it work! 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.