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
PBI_newuser
Post Prodigy
Post Prodigy

How to sum the hours if the date and ID is same?

Hi, i have a set of data as shown below.

How to calculate if the same date exist in the next line for same ID, then sum it, if not just take the hours in Col C.

 

PBI_newuser_0-1617867786109.png

 

1 ACCEPTED SOLUTION
ERD
Community Champion
Community Champion

@PBI_newuser ,

If you are looking for a calculated column, you might try this code:

HoursCombiner = 
VAR currentIndex = T[Index]
VAR currentID = T[ID]
VAR nextDate = MINX(FILTER(T, T[Index] > currentIndex && T[ID] = currentID), T[Date])
RETURN
IF(T[Date] = nextDate, 
    SUMX(FILTER(T, T[Date] <= nextDate && T[ID] = currentID && T[Index] >= currentIndex), T[Hours]),
    T[Hours])

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

View solution in original post

6 REPLIES 6
ERD
Community Champion
Community Champion

@PBI_newuser ,

If you are looking for a calculated column, you might try this code:

HoursCombiner = 
VAR currentIndex = T[Index]
VAR currentID = T[ID]
VAR nextDate = MINX(FILTER(T, T[Index] > currentIndex && T[ID] = currentID), T[Date])
RETURN
IF(T[Date] = nextDate, 
    SUMX(FILTER(T, T[Date] <= nextDate && T[ID] = currentID && T[Index] >= currentIndex), T[Hours]),
    T[Hours])

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

amitchandak
Super User
Super User

@PBI_newuser , Try new column like

sumx(filter(Table, [ID] =earlier([ID]) && [Date] =earlier([Date]) ),[hours])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi @amitchandak , by using the column you have shared, the total hours for B-123 on 20/12/2019 is 35.0.

However, I would like to combine once only if the ID and date is the same, and add the second line (larger) value. For example, the HoursCombiner for B-123 on 20/12/2019 should be 8.0+9.5+9.5 = 27.0.

 

PBI_newuser_0-1617870942511.png

 

@PBI_newuser , You have to add an index column in the power query and try a new column - https://stackoverflow.com/questions/45715963/creating-an-index-column-for-power-bi

 

sumx(filter(Table, [ID] =earlier([ID]) && [Date] =earlier([Date]) && [Index] >earlier([index]) && [Hours] > earlier([hours]) ),[hours])

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi @amitchandak , I couldn't get the output I want.

Here is the sample. Thanks for your help! 

 

PBI_newuser_0-1617875815775.png

 

 

@PBI_newuser , Only need one small change to the formula 

 

HoursCombiner = 'Table'[Hours] + sumx(filter('Table', 'Table'[ID] =earlier('Table'[ID]) && [Date] =earlier([Date]) && 'Table'[Index] >earlier('Table'[Index]) && 'Table'[Hours] > earlier('Table'[Hours]) ),'Table'[Hours])
Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.

Top Solution Authors