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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
vicky_
Super User
Super User

Need help calculating totals which keep duplicating values

Hello, 

not sure if it's just forgetting how to do DAX after a couple of weeks of break or what, but I have a problem that I can't quite seem to solve. 
I have a sales, returns and order item-cost table. Please note - some of the tables are connected via DirectQuery, so I can't add columns. I'm having trouble calculating the total of the returned items-costs (which is the sum of each items (returns / ordered qty) * item's cost)

 

vicky__0-1736402924112.png

(using concat for illustration purposes, it is supposed to be a sumx)
my issue is that the totals values for when there are more than one items is duplicated. For the returned item costs of OrderA, I expect 5 + 10 = 15, but i'm getting 30 + 30 = 60 instead. 

 

I've attached a pbix below. 

Thank you in advance.

1 ACCEPTED SOLUTION
xifeng_L
Super User
Super User

Hi @vicky_

 

The problem is in the costadj variable. When subtotaling rows, VALUES(Orders[ItemID]) gets all ItemIDs under the current OrderID, not the ItemID of the current row.

 

Here is the corrected Measure:

 

returned item costs = 
CONCATENATEX(
    VALUES(Orders[ItemID]), 
        var ratio = CALCULATE(DIVIDE([Return Qty (measure)], SUM(Orders[OrderQty]), 0))
        var costadj = CALCULATE(SUM('Order Items Cost'[Cost]) * ratio, TREATAS({Orders[ItemID]}, 'Order Items Cost'[Item])) 
        RETURN costadj
    , ", ")

 

xifeng_L_0-1736414903476.png

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

View solution in original post

2 REPLIES 2
xifeng_L
Super User
Super User

Hi @vicky_

 

The problem is in the costadj variable. When subtotaling rows, VALUES(Orders[ItemID]) gets all ItemIDs under the current OrderID, not the ItemID of the current row.

 

Here is the corrected Measure:

 

returned item costs = 
CONCATENATEX(
    VALUES(Orders[ItemID]), 
        var ratio = CALCULATE(DIVIDE([Return Qty (measure)], SUM(Orders[OrderQty]), 0))
        var costadj = CALCULATE(SUM('Order Items Cost'[Cost]) * ratio, TREATAS({Orders[ItemID]}, 'Order Items Cost'[Item])) 
        RETURN costadj
    , ", ")

 

xifeng_L_0-1736414903476.png

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

That worked like a charm. Thanks so much!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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