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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
TrevLc
Helper III
Helper III

Totals incorrect for visual level calculation

I created a visual level calculation, which is quite simple -> versus previous, which works on a line by line level, but the total was incorrect.

So I changed the DAX to use SUMX and Values, but that still did not work.

Here is my formula

Versus previous =
var prev =  [Actual] - PREVIOUS([Actual])
return
SUMX(VALUES([Date]),
CALCULATE(prev)
)
Here is my data, and as you can see, the total for "Versus previous" is the same as the total for "Actual"
 
Please help, I want the total to be the sum of the line items.
Note, this is specific to the visual level calculation. "Versus previous" is not a measure
TrevLc_0-1718948697527.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi,@TrevLc 
Thank you for your replay
If you have more than one product and are calculating the difference between the current day's and previous day's quantities for each product on a row-by-row basis, and ultimately returning the correct sum of the differences, you can refer to the test I've done below
Here is my test result: 

vjtianmsft_0-1719400179525.png

Versus previous = 
VAR prev=IF([Product]=PREVIOUS([Product]),
[M_Amount_value] - PREVIOUS([M_Amount_value]),
[M_Amount_value])
VAR differ=
SUMX(VALUES([Date]),
CALCULATE(prev)
)
return
IF(ISINSCOPE([Date]),
differ,
[Sum of C_Difference_3]
)
C_Difference_3 = 
VAR CurrentValue=[Amount]
VAR PreviousValue = CALCULATE(
    MAX('Table2'[Amount]),
    FILTER(
        ALL('Table2'),
        'Table2'[Product] = EARLIER('Table2'[Product]) &&
        'Table2'[Date] < EARLIER('Table2'[Date])
    )
)
RETURN

CurrentValue -PreviousValue

Here is  my test data:

vjtianmsft_1-1719400262387.png

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Hi,@TrevLc,I am glad to help you.
Hello, @amitchandak .Your answer is excellent!
And I would like to share some additional solutions below

 

Based on your description, you want to calculate the difference between each row and the previous row, and aggregate those differences at the position of total to calculate the sum of the differences, not the sum of all quantities

The problem with your totals displaying incorrectly stems from the way totals are calculated in Power BI, which often doesn't match the expected result when using row-level logic for each item in the visual object

Here is my test result.

vjtianmsft_0-1719206409914.png

I created the calculate column to accomplish your needs:
here is the DAX code:

 

C_Difference_ = 
VAR CurrentValue = [Actual]
VAR PreviousValue = CALCULATE(
    MAX([Actual]),
    FILTER(
        ALL('Table'),
        'Table'[Date] < EARLIER('Table'[Date])
    )
)
RETURN
CurrentValue- PreviousValue

 

vjtianmsft_1-1719206453099.png

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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

 

Thank you... this will work. I was just hoping I could do it using visual level calculations

Anonymous
Not applicable

Hi,@TrevLc ,thank you for your reply.
You can try the following test

 

 

Versus previous = 
var prev =  [M_value] - PREVIOUS([M_value])
VAR differ=
SUMX(VALUES([Date]),
CALCULATE(prev)
)
return
IF(ISINSCOPE([Date]),
differ,
[Sum of C_Difference_]
)

 

 

I forced the value of Total to be changed via ISINCOPE, but it still used the value of the previously created calculated column itself, I tried to use [M_value] but the result was wrong, in the end I chose to use [Sum of C_Difference]

vjtianmsft_0-1719211188450.png

 

 

C_Difference_ = 
VAR CurrentValue = [Actual]
VAR PreviousValue = CALCULATE(
    MAX([Actual]),
    FILTER(
        ALL('Table'),
        'Table'[Date] < EARLIER('Table'[Date])
    )
)
RETURN
CurrentValue- PreviousValue
// IF(ISBLANK(PreviousValue),0, CurrentValue - PreviousValue)
// IF(ISBLANK(PreviousValue),'Table'[Actual], CurrentValue - PreviousValue)
M_value = SUM('Table'[Actual])

 

 

I noticed that your [Actual] has to be measure instead of columns

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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



Thanks so much.

How would I adjust the formula if I had multiple products? So each product has an amount per date... for example, here could be some data

TrevLc_0-1719291552495.png

 

Anonymous
Not applicable

Hi,@TrevLc 
Thank you for your replay
If you have more than one product and are calculating the difference between the current day's and previous day's quantities for each product on a row-by-row basis, and ultimately returning the correct sum of the differences, you can refer to the test I've done below
Here is my test result: 

vjtianmsft_0-1719400179525.png

Versus previous = 
VAR prev=IF([Product]=PREVIOUS([Product]),
[M_Amount_value] - PREVIOUS([M_Amount_value]),
[M_Amount_value])
VAR differ=
SUMX(VALUES([Date]),
CALCULATE(prev)
)
return
IF(ISINSCOPE([Date]),
differ,
[Sum of C_Difference_3]
)
C_Difference_3 = 
VAR CurrentValue=[Amount]
VAR PreviousValue = CALCULATE(
    MAX('Table2'[Amount]),
    FILTER(
        ALL('Table2'),
        'Table2'[Product] = EARLIER('Table2'[Product]) &&
        'Table2'[Date] < EARLIER('Table2'[Date])
    )
)
RETURN

CurrentValue -PreviousValue

Here is  my test data:

vjtianmsft_1-1719400262387.png

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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

amitchandak
Super User
Super User

@TrevLc , I doubt as of now you can use previous values in GT.

You can log an Idea of Issue

https://community.powerbi.com/t5/Issues/idb-p/Issues

 

https://ideas.powerbi.com/ideas/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks for your response.

Do you know a way I can sum the line items for the visual level calculation?

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Solution Authors