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
hewgreen
New Member

Removing duplicates with merging logic

I'm struggling to combine functions correctly (such as FILTER and DISTINCT) in a CALCULATETABLE query. I used the following to generate a slice of a larger input table based on the Product string:
Slice = CALCULATETABLE(FILTER('Input Table', 'Input Table'[Product] = "x"))

This is an annonymised version of what it produced:

InstanceProductQuantityCustomer
0001x-1Bob
0002x-1Bob
0003x4Foo
0004x7Bar
0005x3Do

 

I need unique rows based on the Customer column. Note 'Bob' is duplicated. I'm not sure how to use DISTINCT in the context of a FILTER. As you see I'm falling early but my full desire is to also handle the merging logic in regard to two aspects.

 

1. The Instance is a unique value so I'd like this to be handled by concatination, blank or a new string entered 'Multiple'. Whichever is easier.

 

2. Quantity can also differ between instances. I'd like these to be SUM if they do not equal -1. If any equal -1 then -1 should be entered in the new row.

 

I've struggled with other answers on this topic. Explinations and pointers to how functions work together would be appreciated. I'm enjoying the early learning phase of DAX.

 

 

1 ACCEPTED SOLUTION
Deku
Super User
Super User

Addcolumns(

Distinct( tbl[customer]),

"Instances",

calculate( concatenatex( values(tbl[instance]), tbl[instance], ", ")),

"Quantity",

Var sumQuanity = CALCULATE( sum(tbl[quantity] ))

Var minQuantity = calculate( min( tbl[quantity] ))

Return

If( minQuanity = -1, minQuanity, sumQuantity)

)


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

6 REPLIES 6
Dangar332
Super User
Super User

Hi, @hewgreen 

From screenshot you provided it don't seems like duplicate, because it has two instance (0001,0002) and customer(Bob) has value for both instance.

Please provide some sample row data to understand better your problem

Best Regards,
Dangar

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

I've used the term duplicate incorrectly. Within the scope of Customer 'Bob' is duplicated. Therefore I'd like the merge these rows.

Deku
Super User
Super User

Addcolumns(

Distinct( tbl[customer]),

"Instances",

calculate( concatenatex( values(tbl[instance]), tbl[instance], ", ")),

"Quantity",

Var sumQuanity = CALCULATE( sum(tbl[quantity] ))

Var minQuantity = calculate( min( tbl[quantity] ))

Return

If( minQuanity = -1, minQuanity, sumQuantity)

)


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

I noticed that. the sum and min operate over the whole column not grouped. So for my data min is always -1

The sum and mine are calculated within addcolumns. Addcolumns is a iterator, and we are iterating over each customer. The sum and min are wrapped in calculate so context transition applies the current row context of customer into the calculation


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

This worked well. Thanks so much.

The variables just had some typos. Otherwise flawless.

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.