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.
I am trying to take the following data and calculate the values for each person.
person | sales | Average |
Jerry | 30 | 10 |
Jerry | 30 | 10 |
Jerry | 30 | 10 |
Tom | 25 | 12.5 |
Tom | 25 | 12.5 |
James | 20 | 10 |
James | 20 | 10 |
Joe | 25 | 25 |
I am looking to take the total sales for each person and create a new column (average) that will average the total sales and break it out for each line. Can someone explain how this can be done? Thank you in advance.
Solved! Go to Solution.
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 ) )
Hi @Anonymous,
Could you please post me your desired result?
Regards,
Daniel He
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 ) )
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.
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.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
113 | |
97 | |
84 | |
67 | |
60 |
User | Count |
---|---|
150 | |
120 | |
99 | |
87 | |
68 |