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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
JothiG
Helper II
Helper II

dax correction

4_Cur_yr_cost_amt_pur =
var SelectedYear = max('yarn data'[Stock_finyear])

var _month_max = CALCULATE(MAX('yarn data'[month_sorter]), FILTER(all('yarn data'),
'yarn data'[Stock_finyear] = SelectedYear))

var _month= SELECTEDVALUE('yarn data'[month_sorter])

var _month_calc = if (ISBLANK(_month),_month_max,_month)

var qty =
CALCULATE(
SUM('yarn data'[Qty]),
  'yarn data'[Stock_finyear] = SelectedYear,
   'yarn data'[month_sorter] = _month_calc
)
var rat =
CALCULATE(
 SUM('yarn data'[rate]),
'yarn data'[Stock_finyear] = SelectedYear,
'yarn data'[month_sorter] = _month_calc)
RETURN
qty * rat    --- it is not giving correct result .why? it returns correct qty and rat is also correct. multiplication results only gives wrong answer.
1 ACCEPTED SOLUTION
Poojara_D12
Super User
Super User

Hi @JothiG 

Replace your final multiplication with a SUMX that multiplies per row, inside the same filters:

4_Cur_yr_cost_amt_pur =
VAR SelectedYear = MAX('yarn data'[Stock_finyear])
VAR _month_max = CALCULATE(
    MAX('yarn data'[month_sorter]),
    ALL('yarn data'),
    'yarn data'[Stock_finyear] = SelectedYear
)
VAR _month = SELECTEDVALUE('yarn data'[month_sorter])
VAR _month_calc = IF( ISBLANK(_month), _month_max, _month )

RETURN
CALCULATE(
    SUMX(
        'yarn data',
        'yarn data'[Qty] * 'yarn data'[rate]
    ),
    'yarn data'[Stock_finyear] = SelectedYear,
    'yarn data'[month_sorter] = _month_calc
)

 

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos"

Kind Regards,
Poojara - Proud to be a Super User
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS

View solution in original post

5 REPLIES 5
Poojara_D12
Super User
Super User

Hi @JothiG 

Replace your final multiplication with a SUMX that multiplies per row, inside the same filters:

4_Cur_yr_cost_amt_pur =
VAR SelectedYear = MAX('yarn data'[Stock_finyear])
VAR _month_max = CALCULATE(
    MAX('yarn data'[month_sorter]),
    ALL('yarn data'),
    'yarn data'[Stock_finyear] = SelectedYear
)
VAR _month = SELECTEDVALUE('yarn data'[month_sorter])
VAR _month_calc = IF( ISBLANK(_month), _month_max, _month )

RETURN
CALCULATE(
    SUMX(
        'yarn data',
        'yarn data'[Qty] * 'yarn data'[rate]
    ),
    'yarn data'[Stock_finyear] = SelectedYear,
    'yarn data'[month_sorter] = _month_calc
)

 

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos"

Kind Regards,
Poojara - Proud to be a Super User
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS
rohit1991
Super User
Super User

Hi @JothiG 

 

The issue happens because you are multiplying two separate aggregates (SUM(Qty) × SUM(rate)), which gives inflated results. The correct way is to calculate row-by-row (Qty * rate) and then sum them up using SUMX.

4_Cur_yr_cost_amt_pur =
VAR SelectedYear =
    MAX ( 'yarn data'[Stock_finyear] )
VAR _month_max =
    CALCULATE (
        MAX ( 'yarn data'[month_sorter] ),
        FILTER ( ALL ( 'yarn data' ), 'yarn data'[Stock_finyear] = SelectedYear )
    )
VAR _month =
    SELECTEDVALUE ( 'yarn data'[month_sorter] )
VAR _month_calc =
    IF ( ISBLANK ( _month ), _month_max, _month )
RETURN
    CALCULATE (
        SUMX ( 'yarn data', 'yarn data'[Qty] * 'yarn data'[rate] ),
        'yarn data'[Stock_finyear] = SelectedYear,
        'yarn data'[month_sorter] = _month_calc
    )

This ensures each row’s (Qty × rate) is computed first, then summed -giving you the correct purchase cost.


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

this is also not giving correct result.

danextian
Super User
Super User

Hi @JothiG 

 

It seems to me that you are trying to get the product of  aggregations for each row and them sum them up. But that's just my guess. There's no enough information to go on with. Please provide a workable sample data (not an image), your expected result from the same sample data and your reasoning behind. You may post a link to Excel or a sanitized copy of your PBIX stored in the cloud. https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Praful_Potphode
Solution Sage
Solution Sage

Hi @JothiG 

can you share more information(Scenario\Data Model\PBIX) on this?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.