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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
cqueiroz2222
Helper I
Helper I

Adding an IF condition to a Measure

Hello, I am trying to reply an Excel Rule into DAX Measure

The idea is:

When Result < 0, sum Result + Last Month C/C (superior line), otherwise, just reply the result
So just reply the result when C/C last month be positive


DAX MEASURE USED:

C/C  =
VAR CurrentDate = MAX('dim_Calendario'[Data])
RETURN
    SUMX(
        FILTER(
            ALL('dim_Calendario'),
            'dim_Calendario'[Data] <= CurrentDate
        ),
        [Result]
    )

Expected Result in excel and power bi version below:
cqueiroz2222_0-1731003705167.pngcqueiroz2222_1-1731003728863.png

 

 

8 REPLIES 8
Anonymous
Not applicable

Hi @cqueiroz2222 ,

I create a table as you mentioned.

vyilongmsft_0-1731032949545.png

Then I think you can create a calculated column.

C/C = 
VAR _Current = 'Table'[Result]
VAR _Previous =
    CALCULATE (
        SUM ( 'Table'[Result] ),
        FILTER ( 'Table', 'Table'[Month] < EARLIER ( 'Table'[Month] ) )
    )
RETURN
    _Current + COALESCE ( _Previous, 0 )

vyilongmsft_1-1731033136775.png

I also think you can create two measures and here are the DAX codes.

Result_Color = IF(MAX('Table'[Result])<0,"Red","Black")
C/C_Color = IF(MAX('Table'[C/C])<0,"Red","Black")

Next make them into Conditional formatting.

vyilongmsft_2-1731033363032.png

vyilongmsft_3-1731033414842.png

Finally you will get what you want.

vyilongmsft_4-1731033453869.png

 

 

Best Regards

Yilong Zhou

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

cqueiroz2222_0-1731067215842.png

But this last line isnt respecting the rule: if the last month result's be positive, just reply current month result

You can try the below dax:

C/C = 
VAR _Current = 'Table'[Result]
VAR _MaxMonth = MAXX(ALL('Table'), 'Table'[Month])
VAR _Previous =
    CALCULATE (
        SUM ( 'Table'[Result] ),
        FILTER ( 'Table', 'Table'[Month] < EARLIER ( 'Table'[Month] ) )
    )
RETURN
    IF (
        'Table'[Month] = _MaxMonth && _Current < 0,
        _Current,
        _Current + COALESCE ( _Previous, 0 )
    )

Hello @Angith_Nair 

 

Thank you but it didnt work

ryan_mayu
Super User
Super User

did you mean if the value is <0, then the output is this month's value + last month's value?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




yes

if value < 0, then result + last month

if > 0, just reply the result

FreemanZ
Super User
Super User

hi @cqueiroz2222 ,

 

try like:

measure =

VAR _lm = EDATE(MAX('dim_Calendario'[Data]), -1)

VAR _lmcc =

SUMX(

        FILTER(

            ALL('dim_Calendario'),

            'dim_Calendario'[Data] <= _lm

        ),

        [Result]

    )

RETURN

IF([Result]<0, [Result] +_lm, [Result])

Hello @FreemanZ 

 

Thank you but it didnt work

cqueiroz2222_0-1731005040482.png

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

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.