Power Apps: Calculating various dates with date functions
In this article, I will show you how to leverage the DateDiff(), DateAdd(), Weekday(), DateValue(), and Day(), and Today() functions to calculate all sort of dates that are typical to business applications: first and last day of previous month, first and last day of previous quarter, first and last day of previous week, first and last day of current month, first and last day of current quarter, first and last day of current week, first and last day of next month, first and last day of next quarter, first and last day of next week, etc.
Let's begin:
Each calculation is self-contained for convenience sake, which means the only dependency is on the cdt context variable.
Current Date
Note: you could also use the Now() function, but be aware that this function also includes the timestamp.
First Day of Previous Month
Last Day of Previous Month
First Day of Current Month
Last Day of Current Month
First Day of Next Month
Last Day of Next Month
First Day of Previous Quarter
Last Day of Previous Quarter
First Day of Current Quarter
Last Day of Current Quarter
First Day of Next Quarter
Last Day of Next Quarter
First Day of Previous Week
Last Day of Previous Week
First Day of Current Week
Last Day of Current Week
First Day of Next Week
Last Day of Next Week
You can download the application on the Power Apps Community apps gallery
To check out my YouTube channel visit: https://youtube.com/user/mgomezb1
LIKE AND SUBSCRIBE!!!
Until next post!
MG.-
Mariano Gomez, MVP
Let's begin:
Each calculation is self-contained for convenience sake, which means the only dependency is on the cdt context variable.
Current Date
UpdateContext({cdt:Today()});
First Day of Previous Month
With( {rootdate: DateValue("01/01/1900")}, With( { mts: DateDiff( rootdate, cdt, Months ) - 1 }, DateAdd( rootdate, mts, Months ) ) )
Last Day of Previous Month
DateAdd(cdt, -Day(cdt), Days)
First Day of Current Month
With( {rootdate: DateValue("01/01/1900")}, With( { mts: DateDiff( rootdate, cdt, Months ) }, DateAdd( rootdate, mts, Months ) ) )
Last Day of Current Month
With( {rootdate: DateValue("01/01/1900")}, With( { mts: DateDiff( rootdate, cdt, Months ) + 1 }, With( { FirstNxtMonth: DateAdd( rootdate, mts, Months ) }, DateAdd( FirstNxtMonth, -1, Days ) ) ) )
First Day of Next Month
With( {rootdate: DateValue("01/01/1900")}, With( { mts: DateDiff( rootdate, cdt, Months ) + 1 }, DateAdd( rootdate, mts, Months ) ) )
Last Day of Next Month
With( {rootdate: DateValue("01/01/1900")}, With( { mts: DateDiff( rootdate, cdt, Months ) + 2 }, With( { FirstNxtMonth: DateAdd( rootdate, mts, Months ) }, DateAdd( FirstNxtMonth, -1, Days ) ) ) )
First Day of Previous Quarter
With( {rootdate: DateValue("01/01/1900")}, With( { qtrs: DateDiff( rootdate, cdt, Quarters ) }, DateAdd( rootdate, qtrs - 1, Quarters ) ) )
Last Day of Previous Quarter
With( {rootdate: DateValue("01/01/1900")}, With( { qrtr: DateDiff( rootdate, cdt, Quarters ) }, With( { NxtQrtr: DateAdd( rootdate, qrtr, Quarters ) }, DateAdd( NxtQrtr, -1, Days ) ) ) )
First Day of Current Quarter
With( {rootdate: DateValue("01/01/1900")}, With( { qtrs: DateDiff( rootdate, cdt, Quarters ) }, DateAdd( rootdate, qtrs, Quarters ) ) )
Last Day of Current Quarter
With( {rootdate: DateValue("01/01/1900")}, With( { qrtr: DateDiff( rootdate, cdt, Quarters ) + 1 }, With( { NxtQrtr: DateAdd( rootdate, qrtr, Quarters ) }, DateAdd( NxtQrtr, -1, Days ) ) ) )
First Day of Next Quarter
With( {rootdate: DateValue("01/01/1900")}, With( { qrtr: DateDiff( rootdate, cdt, Quarters ) + 1 }, With( { NxtQrtr: DateAdd( rootdate, qrtr, Quarters ) }, DateAdd( NxtQrtr, 0, Days ) ) ) )
Last Day of Next Quarter
With( {rootdate: DateValue("01/01/1900")}, With( { qrtr: DateDiff( rootdate, cdt, Quarters ) + 2 }, With( { NxtQrtr: DateAdd( rootdate, qrtr, Quarters ) }, DateAdd( NxtQrtr, -1, Days ) ) ) )
First Day of Previous Week
With( {rootdate: DateValue("01/01/1900")}, With( { diffval: DateDiff( rootdate, cdt, Days ), Offset: 1 - Weekday(cdt) }, DateAdd( cdt, Offset - 7, Days ) ) )
Last Day of Previous Week
With( {rootdate: DateValue("01/01/1900")}, With( { diffval: DateDiff( rootdate, cdt, Days ), Offset: 1 - Weekday(cdt) }, DateAdd( cdt, Offset - 1, Days ) ) )
First Day of Current Week
With( {rootdate: DateValue("01/01/1900")}, With( { diffval: DateDiff( rootdate, cdt, Days ), Offset: 1 - Weekday(cdt) }, DateAdd( cdt, Offset, Days ) ) )
Last Day of Current Week
With( {rootdate: DateValue("01/01/1900")}, With( { diffval: DateDiff( rootdate, cdt, Days ), Offset: 1 - Weekday(cdt) }, DateAdd( cdt, Offset + 6, Days ) ) )
First Day of Next Week
With( {rootdate: DateValue("01/01/1900")}, With( { diffval: DateDiff( rootdate, cdt, Days ), Offset: 1 - Weekday(cdt) }, DateAdd( cdt, Offset + 7, Days ) ) )
Last Day of Next Week
With( {rootdate: DateValue("01/01/1900")}, With( { diffval: DateDiff( rootdate, cdt, Days ), Offset: 1 - Weekday(cdt) }, DateAdd( cdt, Offset + 13, Days ) ) )
You can download the application on the Power Apps Community apps gallery
To check out my YouTube channel visit: https://youtube.com/user/mgomezb1
LIKE AND SUBSCRIBE!!!
Until next post!
MG.-
Mariano Gomez, MVP
Comments