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
Anonymous
Not applicable

Sum values by ID

Hello!

 

I've following situation:

power bi issue.PNG

 

What I want to achieve is to aggregate values by QuantityID. 2 in 1st row and 3rd row is the same, there are no spaces or anything (I've putted it via Enter Data option)

so I would see something like:

Quantity    Unit Price   Column2

2                £3                4

5                £7                5

2                £2                4

 

I want to do it in power bi desktop (I know I could do it via grouping in power query, but this is not an option). I know I could do it with measure in dax but this is not an option either.

 

I also know it will work if I remove Unit Price column, but I don't want to do it.

 

I want a calculate column with as stated in screen shot.

 

Any ideas?

 

Cheers!

Michal

1 ACCEPTED SOLUTION
Anonymous
Not applicable

[Column 2] =
var __quantity = T[Quantity]
var __sameQuantityCount =
	COUNTROWS (
		FILTER (
			T,
			T[Quantity] = __quantity
		)
	)
var __sameQuantitySum =
	__sameQuantityCount * __quantity
RETURN
	__sameQuantitySum

Best

Darek

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

[Column 2] =
var __quantity = T[Quantity]
var __sameQuantityCount =
	COUNTROWS (
		FILTER (
			T,
			T[Quantity] = __quantity
		)
	)
var __sameQuantitySum =
	__sameQuantityCount * __quantity
RETURN
	__sameQuantitySum

Best

Darek

Anonymous
Not applicable

Thx Darek for quick answer, but I don't think this is what I was asking for.

 

What if I've let say 10000 rows with Quantity:

1

2

3

3

2

1

and so on...and I want to sum the quantity by each row. Not doing count though.

 

Cheers!

Michal

Anonymous
Not applicable

Mate,

 

Have a look at this:

 

Quantity    Unit Price   Column2

2                £3                4

5                £7                5

2                £2                4

 

My code does exactly what you were asking for. It does this: Get the current Quantity (I assume it's the QuantityID you were talking about but never showed it). Then, find all the rows where Quantity is the same as the current one and count the number of rows with it. Then multiply the Quantity by the number of rows and return the number. This is exactly what you wanted the code to do. Is it not?

 

You should be maximally clear about your requirements, otherwise you'll get what people think you wanted.

 

Best

Darek

Anonymous
Not applicable

Hi!

 

Actually...it does, but instead of counting I need sum, so I changed a little your code and it works perfect!

Column 3 =
var __quantity = 'Table from records'[Quantity]
var __sameQuantityCount =
    CALCULATE(SUM('Table from records'[Quantity]),
        FILTER (
            'Table from records',
            'Table from records'[Quantity] = __quantity
        )
    )

RETURN
    __sameQuantityCount
 
Thx for help 🙂
 
My apologies if I was not clear about what I need 🙂
 
Cheers!
Michal
Anonymous
Not applicable

Well, I'm happy it works for you but the logic in your formula is exactly the same as mine as much as I'm able to say. What's worse, your formula is slower than mine. You should almost NEVER put a whole table as a filter in CALCULATE. This slows calculation down big time and if you do this in regular measures, sometimes you'll be scratching your head not knowing why the formula returns seemingly nonsensical results.

 

Best

Darek

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.