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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Python script modulus ...
Power Automate
Answered

Python script modulus operator

(0) ShareShare
ReportReport
Posted on by 31

I am trying to run a python script to get the date of the most recent Saturday. However, power automate desktop throws a syntax error when I apply the modulus operator. Is there a way around this error while preserving the functionality of the script?

 

 

import datetime as dt

today = dt.date.today()

# offset weekday index by 2 and restart sums > 7
daysSubtract = (today.weekday() + 2) % 7
recentSaturday = today - dt.timedelta(days = daysSubtract)

print(recentSaturday)

 

I have the same question (0)
  • Verified answer
    Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at

    Hi @tgut03 

     

    Try this updated script

     

    import datetime as dt
    
    today = dt.date.today()
    
    # offset weekday index by 2 and restart sums > 7
    daysSubtract = (today.weekday() + 2) %% 7
    recentSaturday = today - dt.timedelta(days = daysSubtract)
    
    print(recentSaturday)

     

    Hope it helps !

    Thanks & Regards,

    Nived N 🚀

    LinkedIn: Nived N's LinkedIn
    YouTube: Nived N's YouTube Channel
    Blog: Nived Nambiar's Blogs

    🔍 Found my answer helpful? Please consider marking it as the solution!
    Your appreciation keeps me motivated. Thank you! 🙌

     

  • VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at

    Hi @tgut03 ,

     

    If it is not compulsory to use python only to generate this. You can try this with Vbscript.The correct updated VBscript is provided below. You Can use Run Vbscript action to get the recent Saturday Date.

    ' Function to get the most recent Saturday date in the current month
    Function GetRecentSaturday()
     Dim currentDate, currentMonth, currentYear
     Dim dayOfWeek, recentSaturday
    
     ' Get the current date without time
     currentDate = Date
     currentMonth = Month(currentDate)
     currentYear = Year(currentDate)
     
     ' Initialize recentSaturday with the current date
     recentSaturday = currentDate
     
     ' Find the most recent Saturday
     Do While Weekday(recentSaturday) <> vbSaturday
     recentSaturday = DateAdd("d", -1, recentSaturday)
     Loop
     
     ' If the recent Saturday is in the previous month, find the last Saturday of the current month
     If Month(recentSaturday) <> currentMonth Then
     recentSaturday = DateSerial(currentYear, currentMonth + 1, 0)
     
     Do While Weekday(recentSaturday) <> vbSaturday
     recentSaturday = DateAdd("d", -1, recentSaturday)
     Loop
     End If
     
     ' Return the recent Saturday
     GetRecentSaturday = recentSaturday
    End Function
    
    ' Call the function and display the result
    Dim recentSaturday
    recentSaturday = GetRecentSaturday()
    
    WScript.Echo "The most recent Saturday date in this month is: " & recentSaturday

     

    (Note:- if you got your solution you can mark as solution and gives kudos)


    Thanks & Regards

    Vishnu Reddy

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Valantis Profile Picture

Valantis 471

#2
11manish Profile Picture

11manish 246

#3
David_MA Profile Picture

David_MA 238 Super User 2026 Season 1

Last 30 days Overall leaderboard