Power Apps: Calculating various dates with date functions

Mariano GomezDyn365GP4 years ago9 Views

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

UpdateContext({cdt:Today()});

Note: you could also use the Now() function, but be aware that this function also includes the timestamp.

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

Original Post http://dynamicsgpblogster.blogspot.com/2020/06/powerapps-dates.html

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...