Forum Discussion
Conditional custom column with some math
- Anonymous6 years ago
Hi cpdanielmc21 ,
Thanks for explaining, I made this, hope that's what you want:
Measure 14 = VAR sumcategory = CALCULATE(SUM('Table (4)'[Amount]),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] in {1,2})) VAR sumcategory1 = CALCULATE(SUM('Table (4)'[Amount]),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] in {1})) VAR countrow2 = CALCULATE(COUNTROWS('Table (4)'),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] = 2)) RETURN IF(MAX('Table (4)'[Category]) = 3,SUM('Table (4)'[Amount]), IF(sumcategory < 0, IF(MAX('Table (4)'[Category]) = 1,0,sumcategory1 / countrow2 + SUM('Table (4)'[Amount])), 0))Aiolos Zhao
- 6 years ago
Hi cpdanielmc21 ,
You can modify Anonymous 's code and create a calculated column directly.
Column = VAR sumcategory = CALCULATE( SUM('Table (4)'[Amount]), FILTER( ALL( 'Table (4)'), 'Table (4)'[Category] in {1,2} && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] ) ) ) VAR sumcategory1 = CALCULATE( SUM('Table (4)'[Amount]), FILTER( ALL('Table (4)'), 'Table (4)'[Category] = 1 && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] ) ) ) VAR countrow2 = CALCULATE( COUNTROWS('Table (4)'), FILTER( ALL('Table (4)'), 'Table (4)'[Category] = 2 && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] ) ) ) RETURN IF( [Category] = 3, 'Table (4)'[Amount], IF( sumcategory > 0, 0, SWITCH( [Category], 1, 0, 2, (sumcategory1/countrow2) + 'Table (4)'[Amount] ) ) )Best regards,
Lionel ChenIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous Thanks!
One question, is there a way to do this directly as a custom column in the Data / step in query editor instead of measure?
Also, the measure works very well except when I start to filter:
So the thing is, later on I will create a summary with the total values per month of the categories, and this is what I get (Category 1 is zero, and 3 is 100 that's ok, but total category 2 should be -21)
I think it's another logic in your screenshot.
Do you want to show the amount column by sum or not sum?
In your screenshot, the amount is a sum, so every category will only have one amount.
But in your post, you split the category 1 amount to 3 rows, that's different. which one is you wanted?
Aiolos Zhao