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 September 15. Request your voucher.

Reply
D_PBI
Post Partisan
Post Partisan

How to count days between two dates grouped by two attributes?

Hi.
Please view the below example table, which also includes the desired result to the right.
You will notice each ID has mutiple Statuses and each have their own Start Date and End Date. I need to calculate the number of Days for each ID, for each Status with a unique starting number (ignore the accompanying letter).

For example, ID 1 has the statues 1a, 1b, and 1c. I need to calculate the number of Days between the earliest date for Status starting with '1' and the latest date for Status starting with '1'. This would be 1a  01/04/2021 through to (including) 1c  10/08/2022. This is a total of 497 Days.
Note, the starting Status doesn't always start with the same Status value. Look at ID 1 Status 3 it starts with 3c, but for ID 3 the Status 3 starts with 3d. Point being, we don't want to look for a particular Status value exact (i.e. 3c) but we want to group by the Status starting number (i.e. 1b would be series 1, 2a would be the series 2, etc..). The desired results, to the right of the exmaple table, will confirm the wanted result.

D_PBI_0-1688578478111.png

 

I plan to drop these values into a horizontal bar-graph, so Series would be on the X-axis, ID on the Y-axis and the count will be the Days. I'm not sure if I should have this calculated in the measure of a column for my needs.
Please can someone help me with this calculation?
Thanks.

1 ACCEPTED SOLUTION
Nathaniel_C
Community Champion
Community Champion

Hi @D_PBI ,

If I understand your issue, please try this.
My example:

Nathaniel_C_0-1688583055217.png

Period = 

Var _id = MAX('Status'[ID])
Var _series = MAX('Status'[Series])

var _MinDate = CALCULATE(min('Status'[Start Date]),FILTER(All('Status'),'Status'[ID]= _id && 'Status'[Series]=_series))
var _MaxEndDate = CALCULATE(MAX('Status'[End Date]),FILTER(All('Status'),'Status'[ID]= _id && 'Status'[Series]=_series))
var _dateDif= DATEDIFF(_MinDate,_MaxEndDate,DAY)

Return _dateDif

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
D_PBI
Post Partisan
Post Partisan

@Nathaniel_C  - Perfect. Thank you very much.

@D_PBI ,
You are very welcome!
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Nathaniel_C
Community Champion
Community Champion

Hi @D_PBI ,

If I understand your issue, please try this.
My example:

Nathaniel_C_0-1688583055217.png

Period = 

Var _id = MAX('Status'[ID])
Var _series = MAX('Status'[Series])

var _MinDate = CALCULATE(min('Status'[Start Date]),FILTER(All('Status'),'Status'[ID]= _id && 'Status'[Series]=_series))
var _MaxEndDate = CALCULATE(MAX('Status'[End Date]),FILTER(All('Status'),'Status'[ID]= _id && 'Status'[Series]=_series))
var _dateDif= DATEDIFF(_MinDate,_MaxEndDate,DAY)

Return _dateDif

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Kudoed Authors