Forum Discussion
Condition statement with group by
I have two columns in my table:
| Column 1 | ID |
| 34.52 | 1 |
| 50.20 | 1 |
| 24.21 | 1 |
| 31.53 | 1 |
| 27.78 | 1 |
| 20.00 | 2 |
| 31.64 | 2 |
| 16.00 | 2 |
| -29.41 | 2 |
| 26.74 | 2 |
| 21.23 | 3 |
| -13.17 | 3 |
| 12.29 | 3 |
| 5.26 | 3 |
| 13.26 | 3 |
| 21.02 | 4 |
| 2.90 | 4 |
| -24.20 | 4 |
| 8.85 | 4 |
| 7.59 | 4 |
My aim is to create a new measure that takes the same values as "Column 1" if the average of the values in "Column 1" are greater than 0 and takes a value of 0 otherwise. This condition should be grouped by the ID column.
How do I add the group by functionality in the statement below?
Table 2 = if(Average(Table1[Column 1]) < 0 , 0, Table1[Column 1]) )
You can use sum, average, min, max. Since there is only the one value they will all return the same value (as long as column 1 is in the table)
6 Replies
- MarkSResolver IV
Hi yadavu,
Here is a measure that will do what you requested:
Measure 2 = IF ( CALCULATE ( AVERAGE ( Table1[Column 1] ), ALL ( Table1[Column 1] ) ) > SUM ( Table1[Column 1] ), 0, SUM ( Table1[Column 1] ) )and here is a more useful measure that will show the value from column1 when it is greater than the average:
OverAverage = IF ( CALCULATE ( AVERAGE ( Table1[Column 1] ), ALL ( Table1[Column 1] ) ) > SUM ( Table1[Column 1] ), 0, SUM ( Table1[Column 1] ) )- yadavuNew Member
Hi MarkS,
Thank you for the response. This solution did not take into account the grouping of data by the ID column.
- MarkSResolver IV
Hi yadavu,
Sorry, it looks like I copied the same formula twice in the response, here is the corrected formula
Measure = IF ( CALCULATE ( AVERAGE ( Table1[Column 1] ), ALL ( Table1[Column 1] ) ) < 0, 0, AVERAGE(Table1[Column 1]) )although it does take into account the grouping by ID. In the example data there is no average by group id that is less than 0 so you only get back the original column.
- v-yulgu-msftMicrosoft Employee
Hi yadavu,
You could try this:
Condition value = IF ( CALCULATE ( AVERAGE ( Table11[Column 1] ), ALLEXCEPT ( Table11, Table11[ID] ) ) > 0, CALCULATE ( AVERAGE ( Table11[Column 1] ), ALLEXCEPT ( Table11, Table11[ID] ) ), 0 )Best regards,
Yuliana Gu