cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
kevingeorge3
Regular Visitor

Geting Count of Days

How would I calculate the number of days in each month between two dates in powerBI?
Example: Given start date of 12/09/2010 and end date 07/11/2010 I would like to get back

09/2010 - 22 days
10/2010 - 31 days
11/2010 - 7 days

The start and end dates vary and could possibly be in the same month or across two months or even more.

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support
Community Support

Re: Geting Count of Days

Hi @kevingeorge3 

 

You can use the below measures:

201009 = IF(MIN('Table'[StartDate])>DATE(2010,9,30)||MAX('Table'[EndDate])<DATE(2010,9,1),0,IF(MIN('Table'[StartDate])<=DATE(2010,9,1)&&MAX('Table'[EndDate])>=DATE(2010,9,30),30,IF(MIN('Table'[StartDate])>DATE(2010,9,1)&&MAX('Table'[EndDate])>=DATE(2010,9,30),DATEDIFF(MIN('Table'[StartDate]),DATE(2010,9,30),DAY)+1,IF(MIN('Table'[StartDate])<=DATE(2010,9,1)&&MAX('Table'[EndDate])<DATE(2010,9,30),DATEDIFF(DATE(2010,9,1),MAX('Table'[EndDate]),DAY)+1,DATEDIFF(MIN('Table'[StartDate]),MAX('Table'[EndDate]),DAY)+1))))
201010 = IF(MIN('Table'[StartDate])>DATE(2010,10,31)||MAX('Table'[EndDate])<DATE(2010,10,1),0,IF(MIN('Table'[StartDate])<=DATE(2010,10,1)&&MAX('Table'[EndDate])>=DATE(2010,10,30),31,IF(MIN('Table'[StartDate])>DATE(2010,10,1)&&MAX('Table'[EndDate])>=DATE(2010,10,31),DATEDIFF(MIN('Table'[StartDate]),DATE(2010,10,31),DAY)+1,IF(MIN('Table'[StartDate])<=DATE(2010,10,1)&&MAX('Table'[EndDate])<DATE(2010,10,31),DATEDIFF(DATE(2010,10,1),MAX('Table'[EndDate]),DAY)+1,DATEDIFF(MIN('Table'[StartDate]),MAX('Table'[EndDate]),DAY)+1))))

 

201011 = IF(MIN('Table'[StartDate])>DATE(2010,11,30)||MAX('Table'[EndDate])<DATE(2010,11,1),0,IF(MIN('Table'[StartDate])<=DATE(2010,11,1)&&MAX('Table'[EndDate])>=DATE(2010,11,30),30,IF(MIN('Table'[StartDate])>DATE(2010,11,1)&&MAX('Table'[EndDate])>=DATE(2010,11,30),DATEDIFF(MIN('Table'[StartDate]),DATE(2010,11,30),DAY)+1,IF(MIN('Table'[StartDate])<=DATE(2010,11,1)&&MAX('Table'[EndDate])<DATE(2010,11,30),DATEDIFF(DATE(2010,11,1),MAX('Table'[EndDate]),DAY)+1,DATEDIFF(MIN('Table'[StartDate]),MAX('Table'[EndDate]),DAY)+1))))

 

5.PNG

You can also use M code to do this, but it would be a little bit complicated, please refer to the similar solved post:

https://community.powerbi.com/t5/Desktop/Days-in-Quarter-custom-colomn/td-p/736595 

 

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.

View solution in original post

5 REPLIES 5
Super User IV
Super User IV

Re: Geting Count of Days

from where these dates are coming from. Are they part of table or they are selected in Sclicer.





Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
In case it does not help, please provide additional information and mark me with @
My Recent Blogs -Decoding Direct Query - Time Intelligence, Winner Coloring on MAP,
HR Analytics, Power BI Working with Non-Standard TimeAnd Comparing Data Across Date Ranges
Proud to be a Super User! Linkedin


Highlighted
kevingeorge3
Regular Visitor

Re: Geting Count of Days

Hello,

 

they are coming from a table.

 

Super User IV
Super User IV

Re: Geting Count of Days

Try This a new column not measure

 

datediff = datediff(d1,d2,DAY)

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
https://community.powerbi.com/t5/Community-Blog/Winner-Topper-on-Map-How-to-Color-States-on-a-Map-wi...
https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...
https://community.powerbi.com/t5/Community-Blog/Power-BI-Working-with-Non-Standard-Time-Periods/ba-p...
https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601





Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
In case it does not help, please provide additional information and mark me with @
My Recent Blogs -Decoding Direct Query - Time Intelligence, Winner Coloring on MAP,
HR Analytics, Power BI Working with Non-Standard TimeAnd Comparing Data Across Date Ranges
Proud to be a Super User! Linkedin


kevingeorge3
Regular Visitor

Re: Geting Count of Days

That works to get the total days in the period, 

 

so that data reprsents days worked  so if I wanted a chart that shows the number of days worked by each year how would I do that if the start and stop dates have different years?

 

 

Community Support
Community Support

Re: Geting Count of Days

Hi @kevingeorge3 

 

You can use the below measures:

201009 = IF(MIN('Table'[StartDate])>DATE(2010,9,30)||MAX('Table'[EndDate])<DATE(2010,9,1),0,IF(MIN('Table'[StartDate])<=DATE(2010,9,1)&&MAX('Table'[EndDate])>=DATE(2010,9,30),30,IF(MIN('Table'[StartDate])>DATE(2010,9,1)&&MAX('Table'[EndDate])>=DATE(2010,9,30),DATEDIFF(MIN('Table'[StartDate]),DATE(2010,9,30),DAY)+1,IF(MIN('Table'[StartDate])<=DATE(2010,9,1)&&MAX('Table'[EndDate])<DATE(2010,9,30),DATEDIFF(DATE(2010,9,1),MAX('Table'[EndDate]),DAY)+1,DATEDIFF(MIN('Table'[StartDate]),MAX('Table'[EndDate]),DAY)+1))))
201010 = IF(MIN('Table'[StartDate])>DATE(2010,10,31)||MAX('Table'[EndDate])<DATE(2010,10,1),0,IF(MIN('Table'[StartDate])<=DATE(2010,10,1)&&MAX('Table'[EndDate])>=DATE(2010,10,30),31,IF(MIN('Table'[StartDate])>DATE(2010,10,1)&&MAX('Table'[EndDate])>=DATE(2010,10,31),DATEDIFF(MIN('Table'[StartDate]),DATE(2010,10,31),DAY)+1,IF(MIN('Table'[StartDate])<=DATE(2010,10,1)&&MAX('Table'[EndDate])<DATE(2010,10,31),DATEDIFF(DATE(2010,10,1),MAX('Table'[EndDate]),DAY)+1,DATEDIFF(MIN('Table'[StartDate]),MAX('Table'[EndDate]),DAY)+1))))

 

201011 = IF(MIN('Table'[StartDate])>DATE(2010,11,30)||MAX('Table'[EndDate])<DATE(2010,11,1),0,IF(MIN('Table'[StartDate])<=DATE(2010,11,1)&&MAX('Table'[EndDate])>=DATE(2010,11,30),30,IF(MIN('Table'[StartDate])>DATE(2010,11,1)&&MAX('Table'[EndDate])>=DATE(2010,11,30),DATEDIFF(MIN('Table'[StartDate]),DATE(2010,11,30),DAY)+1,IF(MIN('Table'[StartDate])<=DATE(2010,11,1)&&MAX('Table'[EndDate])<DATE(2010,11,30),DATEDIFF(DATE(2010,11,1),MAX('Table'[EndDate]),DAY)+1,DATEDIFF(MIN('Table'[StartDate]),MAX('Table'[EndDate]),DAY)+1))))

 

5.PNG

You can also use M code to do this, but it would be a little bit complicated, please refer to the similar solved post:

https://community.powerbi.com/t5/Desktop/Days-in-Quarter-custom-colomn/td-p/736595 

 

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.

View solution in original post

Helpful resources

Announcements
New Ranks Launched March 24th!

New Ranks Launched March 24th!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power BI Community!

‘Better Together’ Contest Finalists Announced!

‘Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Arun 'Triple A' Event Video, Q&A, and Slides

Arun 'Triple A' Event Video, Q&A, and Slides

Missed the Arun 'Triple A' event or want to revisit it? We've got you covered! Check out the video, Q&A, and slides now.

Top Solution Authors
Top Kudoed Authors