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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
JL0101
Helper I
Helper I

Calculating the difference using Max Value

I have the code below that is a measure, where I want find the difference in weight based on the latest value and the previsous value however when I use the code below the coumn is blank, any help on this? Thanks 

 

 

 

a2 = 
VAR MaxRevision = MAX(Full_Rebar[Revision Number])
VAR PreviousRevision = MAX(Full_Rebar[Revision Number]) - 1
RETURN
     CALCULATE(SUM(Full_Rebar[weight_calculated_BS8666]),(Full_Rebar[Revision Number]=MaxRevision)-SUM(Full_Rebar[weight_calculated_BS8666]),(Full_Rebar[Revision Number]=PreviousRevision))

 

 

 
1 ACCEPTED SOLUTION
AnalyticsWizard
Solution Supplier
Solution Supplier

@JL0101 

 

The issue with your measure is likely due to the way you're trying to subtract the SUM functions directly within the `CALCULATE` function, which isn't valid syntax in DAX. You need to compute the sums separately and then subtract them. Here’s how you can revise your measure to correctly calculate the difference in weight between the latest revision and the previous revision:

 

Weight Difference =
VAR MaxRevision = MAX(Full_Rebar[Revision Number])
VAR PreviousRevision = MaxRevision - 1
VAR WeightCurrent = CALCULATE(
SUM(Full_Rebar[weight_calculated_BS8666]),
Full_Rebar[Revision Number] = MaxRevision
)
VAR WeightPrevious = CALCULATE(
SUM(Full_Rebar[weight_calculated_BS8666]),
Full_Rebar[Revision Number] = PreviousRevision
)
RETURN
WeightCurrent - WeightPrevious


1. MaxRevision and PreviousRevision: These variables store the maximum revision number and the previous revision number respectively.

 

2. WeightCurrent and WeightPrevious: These variables calculate the total weight for the current and previous revisions using the `CALCULATE` function, which changes the context of the calculation to match the specified revision numbers.

 

3. Return Statement: Subtracts the previous revision's weight from the current revision's weight to find the difference.

 

This measure will return the difference in weight between the two specified revisions. Make sure that both revisions exist in your dataset; if the previous revision does not exist for some entries, the measure might return blank for those cases. This approach assumes that `Revision Number` is numeric and sequential.

 

If you encounter any more blanks, verify that `Revision Number` values are correctly inputted and that there are indeed records for both the maximum and the previous revisions in your dataset.

 

If this post helps, please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

View solution in original post

6 REPLIES 6
AnalyticsWizard
Solution Supplier
Solution Supplier

@JL0101 

 

The issue with your measure is likely due to the way you're trying to subtract the SUM functions directly within the `CALCULATE` function, which isn't valid syntax in DAX. You need to compute the sums separately and then subtract them. Here’s how you can revise your measure to correctly calculate the difference in weight between the latest revision and the previous revision:

 

Weight Difference =
VAR MaxRevision = MAX(Full_Rebar[Revision Number])
VAR PreviousRevision = MaxRevision - 1
VAR WeightCurrent = CALCULATE(
SUM(Full_Rebar[weight_calculated_BS8666]),
Full_Rebar[Revision Number] = MaxRevision
)
VAR WeightPrevious = CALCULATE(
SUM(Full_Rebar[weight_calculated_BS8666]),
Full_Rebar[Revision Number] = PreviousRevision
)
RETURN
WeightCurrent - WeightPrevious


1. MaxRevision and PreviousRevision: These variables store the maximum revision number and the previous revision number respectively.

 

2. WeightCurrent and WeightPrevious: These variables calculate the total weight for the current and previous revisions using the `CALCULATE` function, which changes the context of the calculation to match the specified revision numbers.

 

3. Return Statement: Subtracts the previous revision's weight from the current revision's weight to find the difference.

 

This measure will return the difference in weight between the two specified revisions. Make sure that both revisions exist in your dataset; if the previous revision does not exist for some entries, the measure might return blank for those cases. This approach assumes that `Revision Number` is numeric and sequential.

 

If you encounter any more blanks, verify that `Revision Number` values are correctly inputted and that there are indeed records for both the maximum and the previous revisions in your dataset.

 

If this post helps, please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

Looks like the issue is that the previous revision is not

VAR PreviousRevision = MaxRevision - 1


Rather it is a the previous list (python term) item of 

Full_Rebar[Revision Number]


How would I get the previous list item of Full_Rebar[Revision Number]? 

I had a closer look and for some reason WeightPrevious is coming up with no values i.e. blank ?

Thank you!

amitchandak
Super User
Super User

@JL0101 , try like

 

a2 =
VAR MaxRevision = MAX(Full_Rebar[Revision Number])
VAR PreviousRevision = MAX(Full_Rebar[Revision Number]) - 1
RETURN
CALCULATE(SUM(Full_Rebar[weight_calculated_BS8666]),Full_Rebar[Revision Number]=MaxRevision)-calculate(SUM(Full_Rebar[weight_calculated_BS8666]),Full_Rebar[Revision Number]=PreviousRevision)

I get the following error

 

A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.