Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
I need to calculate the sum of all amounts for a column in a table avoiding duplicate IDs in another column in same table.
At last I finished with two measures:
Max Order Gross Sales Amount =
MAX(tBrand[ORDER_TOTAL_AMOUNT])
Brand_Gross_Sales_Amount =
SUMX ( DISTINCT ( tBrand[ORDER_ID] ); [Max Order Gross Sales Amount] )
Using these two different measures all is fine, but when I tried to use variables in same measure like this:
Brand_Gross_Sales_Amount =
VAR vMax_Order_Gross_Sales_Amount =
MAX ( tBrand[ORDER_TOTAL_AMOUNT] )
RETURN
SUMX ( DISTINCT ( tBrand[ORDER_ID] ); vMax_Order_Gross_Sales_Amount )
In thi case returned amount is wrong (it is a lot bigger than first one). I have checked and I cannot find the problem. May you help me?
Thanks a lot.
Solved! Go to Solution.
I think the way you've set it up is best. You could have it all in one measure by simply using the code of the first one on the second one (note you need the CALCULATE to trigger context transition):
Brand_Gross_Sales_Amount = SUMX ( DISTINCT ( tBrand[ORDER_ID] ); CALCULATE ( MAX ( tBrand[ORDER_TOTAL_AMOUNT] ) ) )
but having two measures as you had is more versatile and the best practice.
Please mark as solved when we get to the solution so that others can see it too. If you find the posts useful please consider kudoing.
Cheers
Hi @cmalopez
Variables in DAX are immutable once they are assigned a value at declaration. In your example,
VAR vMax_Order_Gross_Sales_Amount = MAX ( tBrand[ORDER_TOTAL_AMOUNT] )
the MAX() is evaluated before the SUMX and then the value obtained stored in vMax_Order_Gross_Sales_Amount.
vMax_Order_Gross_Sales_Amount is used when the SUMX is iterating. Note that value is always the same. THe measure, on the contrary, is calculated anew for each row as the SUMx iterates.
Thank you so much for your response. So, is possible to calculate sum for column values dependig on distinct ID values in other column in a more easy way? Do I need to use two different measures?
Thank you so much again.
I think the way you've set it up is best. You could have it all in one measure by simply using the code of the first one on the second one (note you need the CALCULATE to trigger context transition):
Brand_Gross_Sales_Amount = SUMX ( DISTINCT ( tBrand[ORDER_ID] ); CALCULATE ( MAX ( tBrand[ORDER_TOTAL_AMOUNT] ) ) )
but having two measures as you had is more versatile and the best practice.
Please mark as solved when we get to the solution so that others can see it too. If you find the posts useful please consider kudoing.
Cheers
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
47 | |
25 | |
19 | |
14 | |
10 |
User | Count |
---|---|
57 | |
49 | |
44 | |
18 | |
18 |