Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Summing up a column and setting to 0 if positive, keep if negative

Hi All - hope everyone is safe and doing ok out there!!!

 

I'm trying to sum up all the revenue for a state that is negative.  If the value ends up being positive then I want it set to 0 - the point here to add up just the money we are losing.  To add a level of complexity to it, for a given month there are various values that go into the overall value.  So for example - a revenue value for March for Washington in my example may be -2500.  In order to get that it had to add up the values for that state for that month so there were some positives and negative numbers in the calc to finally arrive at -2500.  Lets say there was -5000 negative revenue and positive 2500 so the end value when put in a pbi table is -2500.  My current calc is ignoring the positives and just adding up the negatives which is not what I want.  So in that past example my dax is returning -5000 and not -2500.  Here is an example of what I want:

 Sum1.PNG

I put my desired result from the dax i'm asking for help on - it would return that value.  To give an example of the outputs that go into that value - it would look like this:

sum2.PNG

These are all the values that go on in the backend to come up with the -2500 value for WA.  I tried a query and it ignored the positives and just returns a value of -5000.  I think I need to sum up the group first.  Here is the dax I tried:

 

SUMX('Table_Revenue',IF('Table_Revenue'[Revenue]<0,'Table_Revenue'[Revenue],0))

 

Would it be a sum inside of a sum?

 

Any suggestions are greatly appreciated!!

YB

12 REPLIES 12
BA_Pete
Super User
Super User

 @Anonymous ,

 

Try this and see if it works for you:

sum_revenue.PNG

 



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Anonymous
Not applicable

Hi there - thanks for the reply - it works but needs a little tweaking.  It does set the value to 0 but as you see - its still summing up the overall value with the positive.  In your visual below - the desired revenue should be -3500, not -3000.  In the last sum - its still counting the positive 500. 

Anonymous
Not applicable

bump....

LOL, sorry @Anonymous ,

 

Please try the following measure:

desiredRevenue = 
    IF(
        HASONEVALUE(revenue[state]),
        IF(
            SUM(revenue[revenue]) > 0,
            0,
            SUM(revenue[revenue])
        ),
        SUMX(
            FILTER(
                SUMMARIZE(
                    revenue,
                    revenue[state],
                    "sumRev", SUM(revenue[revenue])
                ),
                [sumRev] < 0
            ),
            [sumRev]
        )
    )

 

I get the following result:

sumLTzero.PNG

 



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Anonymous
Not applicable

The measure given by @BA_Pete  can be written much simpler:

your revenue = 
    SUMX(
        values( revenue[state] ),
        var __rev = CALCULATE( SUM( revenue[revenue] ) )
        return
        	( __rev < 0 ) * __rev
    )

Best

Hi @Anonymous ,

 

That's a really elegant solution, thanks for improving mine massively!

 

Could you help my understanding of the following part of your code please?

( __rev < 0 ) * __rev

What exactly is happening here and how, or what terms should I search to learn about this technique please?

 

Many thanks.



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Anonymous
Not applicable

@Anonymous , @BA_Pete ,

 

thanks for the replies guys - i'm using darlove's method yet its still not summing the group right.  When I put it into practice I show that its ignoring the positive values in the overall number and just adding up the negatives.  So for one of my columns for example its showing -2500 for the actual value but with this calc - it shows -2800 because its ignoring the +300 that went into the overall number.  Thoughts?

Anonymous
Not applicable

@Anonymous @BA_Pete - All,
 
I think it is the values function in your code that is making it not calculate right.  It is going through and looking at all the values again and discarding the positives and adding up all the negatives.  In your code - if I run it, it gives me inflated negative numbers.  So if I had say +5000 and -3500 - my general column now sets it to +1500.  With the code you gave - instead of setting the result to 0 it just gives me the -3500 completely ignoring the positive #'s. 
So I took out the values and tried this **** here is my actual code w/o the fake columns I gave before:
 
test 7 =
VAR _version = SELECTEDVALUE(Perspective[Version Actual], "actual | actual")
return
CALCULATE(
VAR _Rev = CALCULATE(SUM('Attributes'[Value USD]))
return
        (_Rev < 0 ) * _Rev,
        'Attributes'[Version] = _version,
        'Attributes'[KPI] = "normalized revenue"
       
)
 
This works in that it takes the value given for each month, i'm putting this into graphs, etc so evaluating it by month.  If the value is negative it shows it, if its positive it sets it to 0.  The only remaining issue I have is if I have 3 values - -500, +500, -500 for example - this would set the value of +500 to 0.  It does this but my Sum still ends up being -500.  It should be -1000.  So its ignoring the setting of +'s to 0 and just summing uAny idea as to why?
 
Thanks - YB.
Anonymous
Not applicable

@BA_Pete , @Anonymous , All.....

 

Any thoughts on this?  Still unable to figure a way to sum up the grouping once I have set it to zero.  Following your code it still sums up the overall group and ignores how we set some to zero for being positive numbers.  Any thoughts would be greatly appreciated!

 

YB

Anonymous
Not applicable

What you're trying to do is hard. This is because it looks like you want to sum up numbers differently depending on which granularity and which dimension have been selected. This requires a lot of code and the formula will be tightly tied to the model. I mean A LOT OF CODE. One time you want to sum up over states, other times you want to sum up over months. Probably there are other dims you'd like to sum over. This will make the formula gigantic and you'll need to account for almost each and every attribute in many dimensions. Without knowing which dimensions and attributes you want to sum over, there is no way to build a formula. And as I said - it'll be gigantic.

Best
D
Anonymous
Not applicable

Hi there. It's no magic. In DAX bools are ints in disguise (as they are in Python, by the way). ( __rev < 0 ) is true or false. If multiplied by a number it returns 1 or 0. So, 1 * __rev = __rev when __rev is < 0, and 0 * __rev = 0 when __rev is >= 0.

Best
D

Very cool. Thanks for taking the time @Anonymous


Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors