Power Automate Desktop (PAD) Best Practices: Part 1 - Date Variables
Michael Annis
Fractional Chief Automation Officer (fCAO) | Digital Transformation Consultant | CPA | Geneva Investor Accounting SME | Private Equity/Hedge Fund Automation |
While it might be nice if PAD had a Get DateTime Action that could pull any date, it unfortunately, does not; therefore, we have to calculate our own dates as needed based off the %CurrentDateTime%.
What this looks like is using the "Get current date and time" action, and applying calculations to get the other dates that we need.
In the case of BDTM (beginning day this month), we use "Convert datetime to text" to break %CurrentDateTime% down into 'MM' and 'yyyy' to %ThisMonth_mm% and %ThisYear_yyyy%, respectively. Then, we piece them back together into a date variable stringing the text together %ThisMonth_mm%/01/%ThisYear_yyyy%.
(Note when pulling 'MM' from date to text; 'mm' stands for minutes, so to get month we must use 'MM'.)
Once we have BDTM, you can get EDLM (ending day last month) by subtracting 1 day using the "Add to datetime" action.
In the "Add to datetime" action, we can also subtract 1 Month using the "Time unit" dropdown. This will give us BDLM (beginning day last month).
领英推荐
And, just like before, we can get EDPM (ending day previous month; previous meaning 'the one before the last one') from BDLM the same way we got EDLM from BDTM.
Once we have the dates as date variables, we can convert them into any text format we wish using the "Convert datetime to text" action to be used as variables later in our PAD flow.
For the text variables, put the date code (ex. EDLM) followed by the text order used, and then the divider that is used (Dash, Dot, or Slash). If there is no divider we can leave off the third identifier in the variable name (ex. EDLM_yyyymmdd).
Here is a complete 'GetDates' subflow that can be copied and pasted into a PAD subflow:
DateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateOnly CurrentDateTime=> CurrentDateTime
Text.FromCustomDateTime DateTime: CurrentDateTime CustomFormat: $'''MM''' Result=> ThisMonth_mm
Text.FromCustomDateTime DateTime: CurrentDateTime CustomFormat: $'''yyyy''' Result=> ThisYear_yyyy
Text.ToDateTime Text: $'''%ThisMonth_mm%/01/%ThisYear_yyyy%''' DateTime=> BDTM
DateTime.Add DateTime: CurrentDateTime TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> YEST
Text.FromCustomDateTime DateTime: CurrentDateTime CustomFormat: $'''ddMMyyyy''' Result=> TODA_ddmmyyyy
Text.FromCustomDateTime DateTime: CurrentDateTime CustomFormat: $'''yyyyMMdd''' Result=> TODA_yyyymmdd
# Beginning Day This Month - 1 day = Ending Day Last Month
DateTime.Add DateTime: BDTM TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> EDLM
# Beginning Day This Month - 1 month = Beginning Day Last Month
DateTime.Add DateTime: BDTM TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Months ResultedDate=> BDLM
# Beginning Day Last Month - 1 day = Ending Day Previous Month
DateTime.Add DateTime: BDLM TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> EDPM
Text.FromCustomDateTime DateTime: EDPM CustomFormat: $'''yyyy-MM-dd''' Result=> EDPM_yyyymmdd_Dash
Text.FromCustomDateTime DateTime: EDLM CustomFormat: $'''yyyy-MM-dd''' Result=> EDLM_yyyymmdd_Dash
Text.FromCustomDateTime DateTime: EDLM CustomFormat: $'''MMddyy''' Result=> EDLM_mmddyy
Text.FromCustomDateTime DateTime: EDLM CustomFormat: $'''MM/dd/yyyy''' Result=> EDLM_mmddyyyy_slash
Michael Annis has no affiliation with Microsoft or its subsidiaries.? The “Best Practices” are purely compilations of his opinion and observations of conversations on the Power Automate Desktop User Community; an individual’s use of them should be taken as such.? Michael Annis has been using Robotic Process Automation (RPA) for over 2 years and maintains a “Top Solution Author” position on the Microsoft Power Automate Desktop User Community standings.?The best place for users to get their questions answered is through the User Community located here: https://powerusers.microsoft.com/t5/Power-Automate-Desktop/bd-p/MPADesktop.