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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
cqueiroz2222
Helper I
Helper I

Power BI doing wrong calculations

Hello, I'm making a table that calculates the previous month and adds it to the current month's result.

Power bi makes the first line correctly, but then the calculations are wrong as you can see in the image below: 

cqueiroz2222_1-1730858770234.png

The error can be seen in abr/2023, if we calculate manually -29,813 + (-67,166) = -96,979 and not -96.229 as shown in the table. Help me see what could be wrong, please

 

DAX Measures Below:

C/C =
VAR vResult = [Result]
VAR vPreviousMonth = [Previous Month]

RETURN
    IF(vPreviousMonth < 0,
        CALCULATE(vResult + vPreviousMonth, vResult
    )
 
--------------------------------------------------------------------------------
Previous Month =
CALCULATE(
    [Result],
    PREVIOUSMONTH('dim_Calendario'[Data])
)
1 ACCEPTED SOLUTION

@cqueiroz2222 ,

To ensure the cumulative total is calculated correctly for each month without relying on intermediate or potentially blank values, we can use a SUMX approach. This modified measure iterates over all dates up to and including the current month and adds up the Result values.

Here's the modified measure to accumulate properly:

Cumulative Total = 
VAR CurrentDate = MAX('Calendar'[Date])
RETURN
    SUMX(
        FILTER(
            ALL('Calendar'),
            'Calendar'[Date] <= CurrentDate
        ),
        [Result]
    )

 

  • CurrentDate: This variable captures the current row's date in the context, which acts as the endpoint for our cumulative calculation.
  • FILTER(ALL('Calendar'), 'Calendar'[Date] <= CurrentDate): This filter retrieves all rows from the Calendar table up to the current date, removing any filters that might interfere.
  • SUMX(..., [Result]): By iterating over each date in the filtered range, this formula sums up all [Result] values up to the CurrentDate, ensuring an accurate cumulative calculation.

Best regards,

View solution in original post

7 REPLIES 7
DataNinja777
Super User
Super User

Hi @cqueiroz2222 ,

The DAX for your C/C measure has a couple of issues. It appears that there is a syntax error in how you’re adding vResult and vPreviousMonth. Additionally, the IF statement might not be necessary if we intend to always add the previous month's result to the current one, regardless of its sign.

C/C = 
VAR vResult = [Result]
VAR vPreviousMonth = [Previous Month]

RETURN
    IF(
        NOT ISBLANK(vPreviousMonth),
        vResult + vPreviousMonth,
        vResult
    )

Best regards,

Hello @DataNinja777 

The measure you sent did "work" to me, however PBI still makes the wrong calculation, as you can see in the image below:
Captura de tela 2024-11-06 110641.png
The first to lines the calculation is made correctly, but the error apperas when he should calculate:

(-29.813) + (-67.166) = -96.979 and not -96.229

Do you have any other idead in how could I calculate this?

@cqueiroz2222 ,

To ensure the cumulative total is calculated correctly for each month without relying on intermediate or potentially blank values, we can use a SUMX approach. This modified measure iterates over all dates up to and including the current month and adds up the Result values.

Here's the modified measure to accumulate properly:

Cumulative Total = 
VAR CurrentDate = MAX('Calendar'[Date])
RETURN
    SUMX(
        FILTER(
            ALL('Calendar'),
            'Calendar'[Date] <= CurrentDate
        ),
        [Result]
    )

 

  • CurrentDate: This variable captures the current row's date in the context, which acts as the endpoint for our cumulative calculation.
  • FILTER(ALL('Calendar'), 'Calendar'[Date] <= CurrentDate): This filter retrieves all rows from the Calendar table up to the current date, removing any filters that might interfere.
  • SUMX(..., [Result]): By iterating over each date in the filtered range, this formula sums up all [Result] values up to the CurrentDate, ensuring an accurate cumulative calculation.

Best regards,

Now it worked, thank you for your help!!! 😄

Hello, @DataNinja777 , me again! 😃

Can you please help me to add one more condition (if it would be possible)

cqueiroz2222_1-1730914632311.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]

cqueiroz2222
Helper I
Helper I

Hello @Anonymous , thank you for your help.

 

However, my dax measuares are in a separate table called 'Measures', does it change anythig? Because I tried to use and it didn't work.

C/C =
var _t = ADDCOLUMNS('Medidas',"Total",
    SUMX(FILTER(ALL('dim_Calendario'),[Month]<=EARLIER([Month])),[Result]))
RETURN
IF(MAX('Medidas'[Result])<0,MAXX(_t,[Total]),MAX('Medidas'[Result]))

Also this "EARLIER([Month]" didnt work
Anonymous
Not applicable

Hi @cqueiroz2222 ,

 

I made simple samples and you can check the results below:

vtianyichmsft_0-1730872667086.png

Measure = var _t = ADDCOLUMNS('Table',"Total",SUMX(FILTER(ALL('Table'),[ID]<=EARLIER([ID])),[Result]))
RETURN IF(MAX('Table'[Result])<0,MAXX(_t,[Total]),MAX('Table'[Result]))

 

Replacing the ID in the sample with your date should accomplish your goal.

 

An attachment for your reference. Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.