web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How can I retrieve eac...
Power Automate
Suggested Answer

How can I retrieve each date of the previous month

(0) ShareShare
ReportReport
Posted on by 203
How can I retrieve each date of the previous month, starting from the first day to the last day? For example, from March 1, 2025, to March 31, 2025, for the last month's dates.
 
Ex
 01.03.2025
02.03.2025
03.03.2025
 
Best Regards 
 
I have the same question (0)
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at
     
    Please find the below solution. Use the loop.
     
     
     
    Script: Please copy paste the below code to your flow in PAD
    Scripting.RunVBScript.RunVBScript VBScriptCode: $'''Dim currentDate, firstDayOfCurrentMonth, lastDayOfPreviousMonth, firstDayOfPreviousMonth
    Dim dateIterator, output
    
    \' Get today\'s date
    currentDate = Date
    
    \' First day of current month
    firstDayOfCurrentMonth = DateSerial(Year(currentDate), Month(currentDate), 1)
    
    \' Last day of previous month = day before first of current month
    lastDayOfPreviousMonth = DateAdd(\"d\", -1, firstDayOfCurrentMonth)
    
    \' First day of previous month
    firstDayOfPreviousMonth = DateSerial(Year(lastDayOfPreviousMonth), Month(lastDayOfPreviousMonth), 1)
    
    \' Initialize output
    output = \"\"
    
    \' Iterate from first to last day of previous month
    dateIterator = firstDayOfPreviousMonth
    Do While dateIterator <= lastDayOfPreviousMonth
        output = output & FormatDateTimeCustom(dateIterator) & vbCrLf
        dateIterator = DateAdd(\"d\", 1, dateIterator)
    Loop
    
    \' Output the result so PAD can capture it
    WScript.Echo output
    
    \' --- Custom format function ---
    Function FormatDateTimeCustom(d)
        FormatDateTimeCustom = Right(\"0\" & Day(d), 2) & \".\" & Right(\"0\" & Month(d), 2) & \".\" & Year(d)
    End Function
    ''' ScriptOutput=> PreviousMonthDates ScriptError=> ScriptError4
    Text.SplitText.Split Text: PreviousMonthDates StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> DatesList
    
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
    Thanks & Regards
    Vishnu Reddy
     
  • Suggested answer
    eetuRobo Profile Picture
    4,204 Super User 2025 Season 2 on at
    You can get the last day of the last month by decreasing current dates day + 1 from the current date. So if today is 07/04/2025 then you can use "Add to datetime" -action and decrease 8 days from todays date (time unit days)


    And you can get that like this: 


    You can get the first day of last month like so:


    Then just loop from first day to the last and add them to a list:



    Here is the actions you can paste to your flow:
    Variables.CreateNewList List=> DateList
    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateOnly CurrentDateTime=> CurrentDateTime
    SET CurrentDay TO CurrentDateTime.Day
    SET CurrentDayPlus1 TO CurrentDateTime.Day + 1
    SET DaysToSubstract TO 0 - CurrentDayPlus1
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: DaysToSubstract TimeUnit: DateTime.TimeUnit.Days ResultedDate=> LastDayOfLastMonth
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Months ResultedDate=> LastMonth
    SET LastMonth TO $'''01/%LastMonth.Month%/%LastMonth.Year%'''
    Text.ConvertTextToDateTime.ToDateTime Text: LastMonth DateTime=> LastMonth
    SET CurrentLoopDay TO LastDayOfLastMonth.Day
    LOOP LoopIndex FROM 1 TO CurrentLoopDay STEP 1
        Variables.AddItemToList Item: LastMonth List: DateList
        DateTime.Add DateTime: LastMonth TimeToAdd: 1 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> LastMonth
    END
    
     
  • DJ_Jamba Profile Picture
    2,837 Super User 2025 Season 2 on at
    Try this:

    Compose 1:
    {
      "Day1ThisMonth": @{formatDateTime(utcNow(),'yyyy-MM-01')},
      "Day1LastMonth": @{subtractFromTime(utcNow(), 1, 'Month','yyyy-MM-01')}
    }
     
    Compose 2:
    int(
    	first(
    		split(
    			dateDifference(
    			outputs('Current_and_Last_Month_Dates')?['Day1LastMonth'],
    			outputs('Current_and_Last_Month_Dates')?['Day1ThisMonth']
    			),
    		'.'
    		)
    	)
    )
     
    Select:
    From:
    range(0,outputs('DateDiff_Dates'))
    
    Map:
    {
      "Date": @{addToTime(
    	outputs('Current_and_Last_Month_Dates')?['Day1LastMonth'],
    	item(),
    	'Day',
    	'dd MMM yyyy' // Or 'dd.MM.yyyy'
    )},
      "DayofWeek": @{addToTime(
    	outputs('Current_and_Last_Month_Dates')?['Day1LastMonth'],
    	item(),
    	'Day',
    	'dddd'
    )}
    }
     
     
    Result:
     
  • Ahmedmuz Profile Picture
    203 on at
     
    Can you we write into the excel one by one date for the refer for SAP etc.
     
    And current month is April but i need Mar month date - please help 
     
    Best Regards 
    Muzamil Ahmed
  • Suggested answer
    eetuRobo Profile Picture
    4,204 Super User 2025 Season 2 on at
    Yes you can do it like this:


     
    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateOnly CurrentDateTime=> CurrentDateTime
    SET CurrentDay TO CurrentDateTime.Day
    SET CurrentDayPlus1 TO CurrentDateTime.Day + 1
    SET DaysToSubstract TO 0 - CurrentDayPlus1
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: DaysToSubstract TimeUnit: DateTime.TimeUnit.Days ResultedDate=> LastDayOfLastMonth
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Months ResultedDate=> LastMonth
    SET LastMonth TO $'''01/%LastMonth.Month%/%LastMonth.Year%'''
    Text.ConvertTextToDateTime.ToDateTime Text: LastMonth DateTime=> LastMonth
    SET CurrentLoopDay TO LastDayOfLastMonth.Day
    Excel.LaunchExcel.LaunchUnderExistingProcess Visible: True Instance=> ExcelInstance
    SET RowNum TO 1
    LOOP LoopIndex FROM 1 TO LastDayOfLastMonth.Day STEP 1
        Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: LastMonth Column: $'''A''' Row: RowNum
        DateTime.Add DateTime: LastMonth TimeToAdd: 1 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> LastMonth
        Variables.IncreaseVariable Value: RowNum IncrementValue: 1
    END
    
     
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at
     
     solutions works very well but if you want to reduce the time and keep the code simple try this below method.
     
    and i can see even march has 31st but i can see in the excel it has written upto 30th only in his answer.
     
     
    Scripting.RunVBScript.RunVBScript VBScriptCode: $'''Dim currentDate, firstDayOfCurrentMonth, lastDayOfPreviousMonth, firstDayOfPreviousMonth
    Dim dateIterator, output
    
    \' Get today\'s date
    currentDate = Date
    
    \' First day of current month
    firstDayOfCurrentMonth = DateSerial(Year(currentDate), Month(currentDate), 1)
    
    \' Last day of previous month = day before first of current month
    lastDayOfPreviousMonth = DateAdd(\"d\", -1, firstDayOfCurrentMonth)
    
    \' First day of previous month
    firstDayOfPreviousMonth = DateSerial(Year(lastDayOfPreviousMonth), Month(lastDayOfPreviousMonth), 1)
    
    \' Initialize output
    output = \"\"
    
    \' Iterate from first to last day of previous month
    dateIterator = firstDayOfPreviousMonth
    Do While dateIterator <= lastDayOfPreviousMonth
        output = output & FormatDateTimeCustom(dateIterator) & vbCrLf
        dateIterator = DateAdd(\"d\", 1, dateIterator)
    Loop
    
    \' Output the result so PAD can capture it
    WScript.Echo output
    
    \' --- Custom format function ---
    Function FormatDateTimeCustom(d)
        FormatDateTimeCustom = Right(\"0\" & Day(d), 2) & \".\" & Right(\"0\" & Month(d), 2) & \".\" & Year(d)
    End Function
    ''' ScriptOutput=> PreviousMonthDates ScriptError=> ScriptError4
    Text.SplitText.Split Text: PreviousMonthDates StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> DatesList
    Excel.LaunchExcel.LaunchUnderExistingProcess Visible: True Instance=> ExcelInstance
    Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: DatesList Column: $'''A''' Row: 1
    
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
    Thanks & Regards
    Vishnu Reddy
     
  • Suggested answer
    eetuRobo Profile Picture
    4,204 Super User 2025 Season 2 on at
    Ah you are correct VishnuReddy1997 I had a mistake and there for the loop does not include the last date of the month. So if you want to use my solution remember to add + 1 to the "End to" field in the loop. So it would be %LastDayOfLastMonth.Day + 1%

  • Ahmedmuz Profile Picture
    203 on at
     

    Thanks for your response. I'm still facing an issue—I'm getting a few dates from February along with March, which shouldn't happen.

    What I need is to store only the dates of the previous month (e.g., if the current month is April, I should get only dates from 1st March to 31st March). If the previous month has 30 or 28 days, then the list should reflect that accurately (i.e., 1st to 30th or 28th only).

    I also want to store these dates in a variable instead in excel, Each date at a time not all to store in Variable, so I can use them later in an SAP recording. Could you please help me fix this?

     

    Best Regards


    ​​​​​​
  • Suggested answer
    eetuRobo Profile Picture
    4,204 Super User 2025 Season 2 on at
    I might have had some mistakes earlier. I simplified the flow bit so try this way:


    End of the flow with the results in the Excel:


    If there are still troubles then add breakpoints and check the variable values so that the LastDayOfLastMonth -variable is correct and the LastMonth -variable is correctly first day before the flow goes inside the loop.

    Here are the actions:
    **REGION Get the last day of the last month
    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateOnly CurrentDateTime=> CurrentDateTime
    SET CurrentDay TO CurrentDateTime.Day
    SET DaysToSubstract TO 0 - CurrentDay
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: DaysToSubstract TimeUnit: DateTime.TimeUnit.Days ResultedDate=> LastDayOfLastMonth
    # Now we should have the last day of last month in a %LastDayOfLastMonth% -variable
    **ENDREGION
    **REGION Get the first day of the last month
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Months ResultedDate=> LastMonth
    SET LastMonth TO $'''01/%LastMonth.Month%/%LastMonth.Year%'''
    Text.ConvertTextToDateTime.ToDateTime Text: LastMonth DateTime=> LastMonth
    # Now we should have the first day of last month in %LastMont% -variable
    **ENDREGION
    Excel.LaunchExcel.LaunchUnderExistingProcess Visible: True Instance=> ExcelInstance
    # In the loop add all the dates from the first day to the last day to the excel
    LOOP LoopIndex FROM 1 TO LastDayOfLastMonth.Day STEP 1
        Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: LastMonth Column: $'''A''' Row: LoopIndex
        DateTime.Add DateTime: LastMonth TimeToAdd: 1 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> LastMonth
    END
    
     
  • Ahmedmuz Profile Picture
    203 on at
    @eetuRobo Thanks for the sharing,
     
    I'm getting confused so sorry to trouble you, could you please share me only one flow which is correct.
     

    What I need is to store only the dates of the previous month (e.g., if the current month is April, I should get only dates from 1st March to 31st March like 1st march,2nd march). If the previous month has 30 or 28 days, then the list should reflect that accurately (i.e., 1st to 30th or 28th only).

    I want to store these dates in a variable instead in excel, Each date at a time not all to store in Variable, so I can use them later in an SAP recording. Could you please help me fix this?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard