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 to take ...
Power Automate
Answered

Python script to take week-1 when leap year

(0) ShareShare
ReportReport
Posted on by 93

Hello I am building a desktop flow and need to take week numbers. I take current week number with Python with the following script:

import datetime
week = datetime.date(%CurrentDateTime.Year%, %CurrentDateTime.Month%, %CurrentDateTime.Day%).isocalendar()[1]
print(week)

With this I am able to get current week number. The issue is I need to take previous 3 weeks. Unfortunately I get a lot of errors and have no idea how to make this work. I tried:

import datetime
week = datetime.date(%CurrentDateTime.Year%, %CurrentDateTime.Month%, %CurrentDateTime.Day%).isocalendar()[1]
week - datetime.timedelta(weeks=1)
print(week)

I tried the following code which works fine if the year did not flip (now I get 0, -1 and -2 instead of going to weeks last year):

import datetime
week1 = datetime.date(%CurrentDateTime.Year%, %CurrentDateTime.Month%, %CurrentDateTime.Day%).isocalendar()[1]-1
week2 = datetime.date(%CurrentDateTime.Year%, %CurrentDateTime.Month%, %CurrentDateTime.Day%).isocalendar()[1]-2
week3 = datetime.date(%CurrentDateTime.Year%, %CurrentDateTime.Month%, %CurrentDateTime.Day%).isocalendar()[1]-3
print(str(week3) + ",", str(week2) + ",", str(week1))

I either get invalid syntax, or trying to convert int to date etc. Is there a way to actually do this? I do not know Python at all so I am at a loss here.

 

What I want to achieve example: using current date I get 03.01.2023. With script I want to get the following string printed: "52, 51, 50".

I have the same question (0)
  • Verified answer
    VJR Profile Picture
    7,635 on at

    Hi @GeorgiGeorgiev 

     

    If you are okay with Vbscript please check if this is what you intended for.

    VJR_0-1672804116548.png

     

    Vbscript -

     

     

    Dim iweeknum 
    iweeknum = DatePart("ww", Now() - 7) & "," & DatePart("ww", Now() - 14) & "," & DatePart("ww", Now() - 21) 
    
    Wscript.Echo iweeknum 

     

     

     

    Please test and modify it as per your requirements for various scenarios, for example some weeks start with Sunday and some with Monday.

  • GeorgiGeorgiev Profile Picture
    93 on at

    Hello @VJR 

    Thank you for the reply. VB script works great! I will check if it leaps monday next week as well when week number is 1.

     

    For those who are wondering about the Python script I made it work like this:

    from datetime import datetime, timedelta
    my_date = datetime.today()
    week1sub = my_date - timedelta(weeks=1)
    week2sub = my_date - timedelta(weeks=2)
    week3sub = my_date - timedelta(weeks=3)
    week1 = week1sub.isocalendar()[1]
    week2 = week2sub.isocalendar()[1]
    week3 = week3sub.isocalendar()[1]
    week=str(week3)+", "+str(week2)+", "+str(week1)+"."
    print(week)

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 Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 238 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 225

#3
Haque Profile Picture

Haque 181

Last 30 days Overall leaderboard