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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
cqueiroz2222
Helper I
Helper I

Dax measure with condition if

Hello, can you help me on how do I transfor an excel formula into a DAX Measure?

 

cqueiroz2222_0-1730983415948.png


when my last month's result be positive (dec/23 =64.228)
next (jan/24 = 138.023) C/C measure must repeat the Result

In excel it would be:
=SE(E20<0;(D21+E20);D21)
E20 = previous month (superior line)
D21 = [Result]

1 ACCEPTED SOLUTION

I get it now @cqueiroz2222 

cc = 

var currentValue = TOTALMTD([valuemeasure], 'calendar'[Date])

var togetherValue = 
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date]) //&& DATEDIFF('calendar'[Date], MAX('calendar'[Date]), MONTH)
)
var togetherValuePreviousMonth = 
CALCULATE(
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date]) //&& DATEDIFF('calendar'[Date], MAX('calendar'[Date]), MONTH)
),
PARALLELPERIOD('calendar'[Date],-1,MONTH)
)

var stopValue = 
CALCULATE(
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date])),
 DATESMTD('calendar'[Date])
)

var cumulative = 
CALCULATE(
    IF( togetherValuePreviousMonth >= 0, currentValue, togetherValue),
    DATESINPERIOD('calendar'[Date], MAX('Table'[date]), -1, MONTH)
)

return cumulative

vojtechsima_0-1730996053538.png

 

View solution in original post

16 REPLIES 16
vojtechsima
Resident Rockstar
Resident Rockstar

@cqueiroz2222 
is this something you want (you need calendar for this).

vojtechsima_0-1730985000071.png

 

cc = 
var previousMonthValue = 
CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH)
)
var check = IF( previousMonthValue >= 0, [valuemeasure], previousMonthValue + [valuemeasure])

return check

 

Hello @vojtechsima 

This dax measurement worked, but the previous calculation is going wrong

 

The central idea of ​​the measure is:

Perform a quantitative sum of the results

  • When negative = add to the previous result
  • When positive = repeat the [Result]

I share some of the data to help

 

Note that the account that is being wrong is: (-29.813) + (-67.166) = -96.979 and not -96.229 as show in the table below:

cqueiroz2222_0-1730986463694.png

 



DataNinja777
Super User
Super User

Hi again, @cqueiroz2222 ,

 

To achieve this behavior in DAX, where the cumulative measure repeats the Result for the current month if the previous month's result is positive, we need to account for DAX's non-recursive nature. While we can't directly reference the result of the previous row (as in Excel), we can work around it with some conditional logic. Here's an approach that can approximate your requirement:

Cumulative Total with Conditional Repeat = 
VAR CurrentDate = MAX('Calendar'[Date])
VAR PreviousMonth = PREVIOUSMONTH(CurrentDate)
VAR PreviousMonthResult = 
    CALCULATE(
        [Result],
        FILTER(
            ALL('Calendar'),
            'Calendar'[Date] = PreviousMonth
        )
    )
RETURN
    IF(
        PreviousMonthResult >= 0,
        [Result],
        SUMX(
            FILTER(
                ALL('Calendar'),
                'Calendar'[Date] <= CurrentDate
            ),
            [Result]
        )
    )

This DAX formula provides a way to "repeat" the Result value when the previous month’s result is positive. However, remember that DAX doesn’t natively support recursive calculations that rely on the outcome of the previous calculation within the same measure. Instead, this approach checks the previous month’s Result conditionally and applies the cumulative calculation if necessary.

 

Best regards,

 

rajendraongole1
Super User
Super User

Hi @cqueiroz2222 - as per above excel formuale if the previous month’s result is positive, you can use a measure that checks the value of the previous month. 

 

C_C_Measure =
VAR PreviousMonthResult =
CALCULATE([Result],
DATEADD('YourDateTable'[Date], -1, MONTH)
)

RETURN
IF(
PreviousMonthResult < 0,
[Result] + PreviousMonthResult,
[Result]
)

 

you can replace Result with actual measure that you created for monthly result value.

still issue exist, please share some sample data for reference , will check at our end.





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

Proud to be a Super User!





This dax measurement worked, but the previous calculation is going wrong

 

The central idea of ​​the measure is:

Perform a quantitative sum of the results

  • When negative = add to the previous result
  • When positive = repeat the [Result]

I share some of the data to help

 

Note that the account that is being wrong is: (-29.813) + (-67.166) = -96.979 and not -96.229 as show in the table below:

cqueiroz2222_1-1730986582517.png

 

 

Hello, @cqueiroz2222 ,
I refactored my case:

cc = 
var previousMonthValue = 
CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH)
)

var currentValue = TOTALMTD([valuemeasure], 'calendar'[Date])

var togetherValue = 
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date]) && DATEDIFF('calendar'[Date], MAX('calendar'[Date]), MONTH)<=1
)
var cumulative = 
CALCULATE(
    IF( previousMonthValue >= 0, [valuemeasure], togetherValue),
    DATESINPERIOD('calendar'[Date], MAX('Table'[date]), -1, MONTH)
)
var check = IF( previousMonthValue >= 0, [valuemeasure], previousMonthValue + [valuemeasure])

return cumulative

vojtechsima_0-1730989015768.png

It would help me if you could show me how the result should look like in your data that you shared.

@vojtechsima 

Apparently the measure “works”, but why is the result wrong?

Please see below:

cqueiroz2222_1-1730990445771.png

(-96.979)  + (-64.354) = -161.333 and not -16.583, can you verify it?


I'am trying to reply this:

cqueiroz2222_2-1730990631683.png

 

 

Hey @cqueiroz2222 

vojtechsima_0-1730992747980.png

I would be interested how the JUly or August look really, after the + value.

cc = 
var previousMonthValue = 
CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH)
)

var currentValue = TOTALMTD([valuemeasure], 'calendar'[Date])

var togetherValue = 
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date]) && DATEDIFF('calendar'[Date], MAX('calendar'[Date]), MONTH)<=3
)
var cumulative = 
CALCULATE(
    IF( previousMonthValue >= 0, [valuemeasure], togetherValue),
    DATESINPERIOD('calendar'[Date], MAX('Table'[date]), -1, MONTH)
)
var check = IF( previousMonthValue >= 0, [valuemeasure], previousMonthValue + [valuemeasure])

return cumulative

I am playing with the 17th row condition the <=3

 

Hello, @vojtechsima 

Thank you for the adjustments.
Now it starts to get wrong in jul/2023

cqueiroz2222_0-1730993260057.png



Please see the original data until the end:

 

cqueiroz2222_1-1730993277158.png

 

@cqueiroz2222 

 

cc = 
var previousMonthValue = 
CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH)
)

var currentValue = TOTALMTD([valuemeasure], 'calendar'[Date])

var togetherValue = 
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date]) //&& DATEDIFF('calendar'[Date], MAX('calendar'[Date]), MONTH)
)

var stopValue = 
CALCULATE(
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date])),
 DATESMTD('calendar'[Date])
)

var cumulative = 
CALCULATE(
    IF( previousMonthValue >= 0, stopValue, togetherValue),
    DATESINPERIOD('calendar'[Date], MAX('Table'[date]), -1, MONTH)
)
var check = IF( previousMonthValue >= 0, [valuemeasure], previousMonthValue + [valuemeasure])

return cumulative

 

vojtechsima_0-1730993862824.png

At this point, I am just adding layers, I suggest refactoring it later for optimisation.

@vojtechsima 

cqueiroz2222_0-1730994273102.png

At a certain point the measurement starts to miscalculate, is there a limiter?

Is there any way to not put an "end" as I will have the results for the next few months?

I want to maintain only the logic, regardless of the period

cqueiroz2222_1-1730994389957.png

 

@cqueiroz2222 do you reset it every year? because it looks like so, based on the calculation it should go further.
because Why do you repeat value in January24 but not in Nov23 or Set23?

The idea is to calculate based on the [Result] of each month, regardless of the year

The rule is:

When last month is < 0,

calculate the previous month + [Result] of the current month

 

In other words, it will only repeat the month's result [Result] when the previous result is positive

I get it now @cqueiroz2222 

cc = 

var currentValue = TOTALMTD([valuemeasure], 'calendar'[Date])

var togetherValue = 
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date]) //&& DATEDIFF('calendar'[Date], MAX('calendar'[Date]), MONTH)
)
var togetherValuePreviousMonth = 
CALCULATE(
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date]) //&& DATEDIFF('calendar'[Date], MAX('calendar'[Date]), MONTH)
),
PARALLELPERIOD('calendar'[Date],-1,MONTH)
)

var stopValue = 
CALCULATE(
CALCULATE(
    CALCULATE(
   [valuemeasure],
    PARALLELPERIOD('calendar'[Date], -1, MONTH))
    +
    TOTALMTD([valuemeasure], 'calendar'[Date]),
    'calendar'[Date] < MAX('calendar'[Date])),
 DATESMTD('calendar'[Date])
)

var cumulative = 
CALCULATE(
    IF( togetherValuePreviousMonth >= 0, currentValue, togetherValue),
    DATESINPERIOD('calendar'[Date], MAX('Table'[date]), -1, MONTH)
)

return cumulative

vojtechsima_0-1730996053538.png

 

Hello @vojtechsima 

Thank you for your patience and help. I believe it will not be possible to replicate the excel rule, as it continues to make mistakes at a certain point

AnkitKukreja
Super User
Super User

Hi! @cqueiroz2222 

 

Can you please explain your question again and best way would be to share your requirement and what is the outcome you are expecting.

 

 

 

For Power BI trainings or support dm or reach out to me on LinkedIn.
If my response has successfully addressed your question or concern, I kindly request that you mark this post as resolved. Additionally, if you found my assistance helpful, a thumbs-up would be greatly appreciated.

Thanks,
Ankit Kukreja
www.linkedin.com/in/ankit-kukreja1904
https://topmate.io/ankit_kukreja

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.