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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
P_Stavis
Regular Visitor

DAX - Remove duplicates and sum the values

Hello!

 

I'm trying to use DAX to remove the duplicates, based on a column and then sum the values of another column.

Manually removing the duplicates in PQ returns the correct values however my DAX formulas doesnt do the same without using PQ.


Heres the sample data

 

Product_IDPriceItem_weightSales_Dept
123 R$    2,003Front Desk
123 R$    2,003Backshop
456 R$    3,004Front Desk
456 R$    3,004Backshop
789 R$    5,006Front Desk
789 R$    5,006Backshop
321 R$    8,009Front Desk
321 R$    8,009Backshop
654 R$    7,008Front Desk
654 R$    7,008Backshop
987 R$    1,002Front Desk
987 R$    1,002Backshop
258 R$    6,007Front Desk
852 R$  10,0011Backshop

 

For the sample data, the expected sum would be:

 

Price

Front: 32

Back: 10


Weight

Front: 39

Back: 11

 

One of the DAX formulas I tried is:

 

sum_price = SUMX(
    VALUES('Sample Data'[Product_ID]),
    CALCULATE( SELECTEDVALUE('Sample Data'[Price]) )
)

However the sum returned is the following:
 
Price
Front: 32
Back: 36
 
Weight
Front: 39
Back: 43
 

If I manually remove the duplicates (either in Excel or PQ), it will always keep the first entry and remove the second (of course), so for the product ID 123 only the front desk will be kept (expected and desired).

 

How to reproduce this behavior consistently in DAX?

1 ACCEPTED SOLUTION
P_Stavis
Regular Visitor

Heres the formula that did the job:

 

For price:

 

Sum_Price_Ok =
SUMX(
    DISTINCT('Sample Data'[Product_ID]),
    CALCULATE(
        FIRSTNONBLANK('Sample Data'[Price], 0)
    )
)
 
For the weight:
 
Sum_Weight_Ok =
SUMX(
    DISTINCT('Sample Data'[Product_ID]),
    CALCULATE(
        FIRSTNONBLANK('Sample Data'[Item_weight], 0)
    )
)

View solution in original post

4 REPLIES 4
P_Stavis
Regular Visitor

Heres the formula that did the job:

 

For price:

 

Sum_Price_Ok =
SUMX(
    DISTINCT('Sample Data'[Product_ID]),
    CALCULATE(
        FIRSTNONBLANK('Sample Data'[Price], 0)
    )
)
 
For the weight:
 
Sum_Weight_Ok =
SUMX(
    DISTINCT('Sample Data'[Product_ID]),
    CALCULATE(
        FIRSTNONBLANK('Sample Data'[Item_weight], 0)
    )
)
xifeng_L
Super User
Super User

Hi @P_Stavis 

 

You could try adding a calculated column to identify the row numbers in the interval:

xifeng_L_0-1742800934114.png

 

Then just add the filter condition of row number equal to 1 in the measure:

 

xifeng_L_1-1742801012909.png

 

 

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

 

Thank you~

Hi @xifeng_L 

I did download your sample pbix and also tried to reproduce but unfortunately I could only reach this point:

P_Stavis_0-1742843283525.png

 

I did get your idea of having 1 for the first line and 2 for the repeated line. For the sample data it would work however for a larger database i don't think it would be the best solution.
Do you see another way to solve this problem only using dax measures?

Thanks alot for your reply!

 

P_Stavis
Regular Visitor

If I may add something based on my findings...

If I aggregate using groupby or summarize with only the price and product_id and show the data in a calculated table, the duplicates are removed:

P_Stavis_0-1742779532046.png

If I add the sales dept then the duplicates return:

 

P_Stavis_1-1742779651298.png

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.