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
Bokchoy
Helper II
Helper II

Custom Sum based on matching column values

Hi All,

 

I have a single table where all sales records are kept. 

 

there are 2 types of sales records in this table. Primary sales record have a [Main] attribute of "M", and sub-records have a [Main] attribute of "S". Primary Sales records can contain many sub-records.

 

unfortunately all these records are kept on a single table and the [cost] of primary records are missing.

 

I need to manually calculate the [cost] of primary records by suming up all sub-records. sub-records have the same [date],[user_id] and [type] as the primary record. (see exmaple below)

 

How can i write this measure as a new column

 

Bokchoy_0-1651113206962.png

 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Bokchoy 

 

Use this to create a new column

Col Cost = CALCULATE(SUM([Cost]), FILTER('Table', 'Table'[Date] = EARLIER('Table'[Date])  && 'Table'[user_id] = EARLIER('Table'[user_id]) && 'Table'[Type] = EARLIER('Table'[Type])))

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

4 REPLIES 4
PhilipTreacy
Super User
Super User

Hi @Bokchoy 

 

Use this to create a new column

Col Cost = CALCULATE(SUM([Cost]), FILTER('Table', 'Table'[Date] = EARLIER('Table'[Date])  && 'Table'[user_id] = EARLIER('Table'[user_id]) && 'Table'[Type] = EARLIER('Table'[Type])))

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Thank you sir, works perfectly 🙂

PhilipTreacy
Super User
Super User

Hi @Bokchoy 

 

Download example PBIX file

 

Use this measure

 

MCost = CALCULATE(SUMX('Table', [Cost]), FILTER(ALL('Table'), 'Table'[Date] = MAX('Table'[Date]) && 'Table'[user_id] = MAX('Table'[user_id]) && 'Table'[Type] = MAX('Table'[Type])))

 

sxsum.png

regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Hi Philip, 
This works perfect as a measure, but it doesnt seem to work as a custom column. 
is there a way to achieve the same result while creating a new column?

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.