cancel
Showing results for
Did you mean:
Helper I

## How to find days between two dates for each month of a year?

Hello,

I have data that currently exist in this format:

 Project Start Date End Date 1 15-Jan-2022 20-Feb-2022 2 10-Jan-2022 25-Mar-2022 3 15-Feb-2022 20-Jun-2022

My user is interested in looking at Number of Days between Start and End Date by Month for each Year. How can we achieve the below  output using formulas or any other approch:

 S.No Start Date End Date Jan-22 Feb-22 Mar-22 Apr-22 May-22 Jun-22 1 15-Jan-2022 20-Feb-2022 16 20 2 10-Jan-2022 25-Mar-2022 21 29 25 3 15-Feb-2022 20-Jun-2022 14 31 30 31 20

Any ideas on how to do this on a Sharepoint List, Power App or eventually Power BI?

C

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
``````ClearCollect(
RangeBreakdownTotals,
GroupBy(
Ungroup(
ForAll(

DateRangeRequest,

With({startDate:ThisRecord.Start,
sequenceDays: Sequence(DateDiff(ThisRecord.Start,
ThisRecord.End) + 1)
},
"Date",
)

)),"Value"

///),"Month","MonthDates"
),"MonthYear","MonthYearDates"
),"DaysThisMonth",CountRows(ThisRecord.MonthYearDates)
))

``````
_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
6 REPLIES 6
Super User

You can do this in Power BI using the solution here. You end up with this table, which I added a '+1' to the formula to include EndDate in the count:

``````CountInDateRange =
VAR StartDate = VALUE( SELECTEDVALUE( ProjectData[Start Date ] ) )
VAR EndDate = VALUE( SELECTEDVALUE( ProjectData[End Date] ) )
VAR MinDateInContext = VALUE( MIN( Dates[Date] ) )
VAR MaxDateInContext = VALUE( MAX( Dates[Date] ) )

RETURN
IF( AND( StartDate < MinDateInContext, EndDate > MinDateInContext ),
MIN( EndDate, MaxDateInContext ) - MinDateInContext + 1,
IF( AND( AND( StartDate > MinDateInContext, StartDate < MaxDateInContext ), EndDate > MinDateInContext ),
MIN( EndDate, MaxDateInContext + 1 ) - StartDate ,
Blank() ) )
``````

To set this up you need the data similar to that you supplied above in a table called ProjectData and a date table called Dates with a column named [Date] with all dates in the range that you wish to count against. If you are not familiar with setting up a date table there are plenty of examples via Google, with code you can copy and paste. You don't need to setup a relationship between the Date table and the ProjectData table.

In PowerApps, you could solve this using the Sequence() function, however, the dynamic nature of your Year/Month columns would prove difficult to achieve - not impossible though

Thank you!
Super User

Ok so the following code, placed anywhere it can be triggered to run (In my case I placed it in the OnSelect of a button), will cycle through a set of start and end dates (In my example it is going through a collection that currently has 3 sets of start and end dates, the third of which starts in July and ends in September just to see if 3 months would break it)

This formula returns a collection with the following columns

Month: This is the number of the month, can change it in formula to be text value of month if needed, or just anywhere you display it, convert on the control it's being displayed on

DaysThisMonth: Total count of days requested off in that month

MonthDates: Nested table that includes every day selected for the request in that specific month.

``````ClearCollect(
RangeBreakdownTotals,
GroupBy(
Ungroup(
ForAll(

DateRangeRequest,

With({startDate:ThisRecord.Start,
sequenceDays: Sequence(DateDiff(ThisRecord.Start,
ThisRecord.End) + 1)
},
"Date",
)

)),"Value"

),"Month","MonthDates"
),"DaysThisMonth",CountRows(ThisRecord.MonthDates)
))``````

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
Helper I

Hello The RobRush,

Amazing formula !

The formula works well thank you, however I will need to have the Years as well  because for the moment if I have some days in march 2022 and some days in march 2023, it sum up all March days.

The idea is to be able to multiply the number of days with a specific number of hours so I got the workload in hours per months of the year per project.

Best regards,

Capucine

Super User
``````ClearCollect(
RangeBreakdownTotals,
GroupBy(
Ungroup(
ForAll(

DateRangeRequest,

With({startDate:ThisRecord.Start,
sequenceDays: Sequence(DateDiff(ThisRecord.Start,
ThisRecord.End) + 1)
},
"Date",
)

)),"Value"

///),"Month","MonthDates"
),"MonthYear","MonthYearDates"
),"DaysThisMonth",CountRows(ThisRecord.MonthYearDates)
))

``````
_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
Helper I

@TheRobRush  Many many thanks for your help!

Helper I

Hi,

can you give the crux of the solution please,

Announcements

#### A Celebration of What We've Achieved--And Announcing Our Winners

As the sun sets on the #SummerofSolutions Challenge, it's time to reflect and celebrate! The journey we embarked upon together was not just about providing answers – it was about fostering a sense of community, encouraging collaboration, and unlocking the true potential of the Power Platform tools.   From the initial announcement to the final week's push, the Summer of Solutions Challenge has been a whirlwind of engagement and growth. It was a call to action for every member of our Power Platform community, urging them to contribute their expertise, engage in discussions, and elevate collective knowledge across the community as part of the low-code revolution.   Reflecting on the Impact As the challenge ends, it's essential to reflect on the impact it’s had across our Power Platform communities: Community Resilience: The challenge demonstrated the resilience of our community. Despite geographical distances and diverse backgrounds, we came together to contribute, learn, and collaborate. This resilience is the cornerstone of our collective strength.Diverse Expertise: The solutions shared during the challenge underscore the incredible expertise within our community. From intricate technical insights to creative problem-solving, our members showcased their diverse skill sets, enhancing our community's depth.Shared Learning: Solutions spurred shared learning. They provided opportunities for members to grasp new concepts, expand their horizons, and uncover the Power Platform tools' untapped potential. This learning ripple effect will continue to shape our growth. Empowerment: Solutions empowered community members. They validated their knowledge, boosted their confidence, and highlighted their contributions. Each solution shared was a step towards personal and communal empowerment. We are proud and thankful as we conclude the Summer of Solutions Challenge. The challenge showed the potential of teamwork, the benefit of knowledge-sharing, and the resilience of our Power Platform community. The solutions offered by each member are more than just answers; they are the expression of our shared commitment to innovation, growth, and progress!   Drum roll, Please... And now, without further ado, it's time to announce the winners who have risen above the rest in the Summer of Solutions Challenge!   These are the top community users and Super Users who have not only earned recognition but have become beacons of inspiration for us all.   Power Apps Community:  Community User Winner: @SpongYe Super User Winner: Pending Acceptance Power Automate Community:  Community User Winner: @trice602 Super User Winner: @Expiscornovus  Power Virtual Agents Community: Community User Winner: Pending AcceptanceSuper User: Pending Acceptance Power Pages Community: Community User Winner: @OOlashyn Super User Winner: @ChristianAbata   We are also pleased to announced two additional tickets that we are awarding to the Overall Top Solution providers in the following communities:    Power Apps: @LaurensM   Power Automate: @ManishSolanki    Thank you for making this challenge a resounding success. Your participation has reaffirmed the strength of our community and the boundless potential that lies within each of us. Let's keep the spirit of collaboration alive as we continue on this incredible journey in Power Platform together.Winners, we will see you in Vegas! Every other amazing solutions superstar, we will see you in the Community!Congratulations, everyone!