Forum Discussion
SUM value by category
- Anonymous8 years ago
I don't agree that this is the right way to do this, using measures you can calculate this information display it without needing to store the value on every single line in your data set (which is what a calculated column will do). Never the less, this is the calculated column code to do what you are asking:
This will produce your calculated column for salesTotal Sales = var thisPerson = [person] RETURN CALCULATE( SUM('YourTable'[sales]) ALL('YourTable'), 'YourTable'[person] = thisPerson )
This calculates the averageTotal Average = var thisPerson = [person] RETURN DIVIDE( [Total Sales], CALCULATE( COUNTROWS('YourTable') ALL('YourTable'), 'YourTable'[person] = thisPerson ) )
I don't agree that this is the right way to do this, using measures you can calculate this information display it without needing to store the value on every single line in your data set (which is what a calculated column will do). Never the less, this is the calculated column code to do what you are asking:
This will produce your calculated column for sales
Total Sales = var thisPerson = [person]
RETURN
CALCULATE(
SUM('YourTable'[sales])
ALL('YourTable'),
'YourTable'[person] = thisPerson
)
This calculates the average
Total Average = var thisPerson = [person]
RETURN
DIVIDE(
[Total Sales],
CALCULATE(
COUNTROWS('YourTable')
ALL('YourTable'),
'YourTable'[person] = thisPerson
)
)- rbrabec6 years agoRegular Visitor
Hi Ross
What would be your solution using a measure? I have difficulties finding a clear answer on how to use CALCULATE + any sort of function (SUM, AVERAGE etc.) for each different value in another column.
Example: different locations (cities) and I want the SUM of the electricity usage for each location.
- Anonymous6 years agoNot applicable
Hi rbrabec Measures are all about the context they are used. When you write a measure, you want to keep your mind on how you will be using the measure.
For example, in this problem, where we want to calculate total sales for the given person, we might expect that we will be using the measure on a table, with 1 row per person. In that case, its unlikely we would need to use a calculate statement, as the context would do all of the work.
When you want to use Calculate with Sum, average etc, the easiest way to do it is to create a measure that handles the Sum/Average/Whatever, then in your 2nd measure you call it using your calculate statement.
i.e:
Sum Measure = SUM('YourTable'[YourColumn])Calculate Measure = CALCULATE( [Sum Measure], 'YourTable'[OtherColumn] = "Other Value" )A Filter statement is often used in Calculate, but it doesn't have to be. The above is a fairly simplisitic use case, but hopefully gives you a starting point.