cancel
Showing results for
Did you mean:
Post Prodigy

## Calculating week numbers

I am not getting it right with the week numbers in Power Automate. I tried...

add(div(dayOfYear(outputs('Date')),7),1) - it gives week 31 for Friday 29th of July 2022. It's week 30. It works for 2020.

div(dayOfYear(outputs('Date')),7) - it gives week week 30 for Wednesday 29th of July 2020. It's week 31. It works for 2022.

In another thread I found a calculation to find weeks between dates from @ccc333ab. This has worked for all years I have tried:

``add(div(div(sub(ticks(outputs('Date')),ticks(formatDateTime(outputs('FirstDateOfYear'),'yyyy-MM-dd'))),864000000000),7),1)``

For the FirstDateOfYear:

``concat(formatDateTime(outputs('Date'),'yyyy'),'-01-01')``

If you need to find the Monday of the Date you can use this formula in the compose of Date:

``subtractFromTime(outputs('ActualDate'), if(equals(dayofweek(utcNow()),0),6,sub(dayofweek(outputs('ActualDate')),1)), 'Day')``

Grateful for this community 🙂

2 ACCEPTED SOLUTIONS

Accepted Solutions
Solution Sage

Ahh...OK.

My Calendar is based on the US settings which starts the year on the first day of the year regardless if its the start of the week or not. Now based on ISO, I believe it sets week 1 of a year based on a week at least having 4 days of the following year. So based on that I created the following formula.

``if(greater(dayofweek(concat(formatDateTime(triggerBody()['date'],'yyyy'),'-01-01')),3),div(dayofyear(triggerBody()['date']),7),add(div(dayofyear(triggerBody()['date']),7),1))``

I'm sure there is a more eloquent way of doing this but this:

• Four days require at least starting on Thurs (if your week starts on a Monday, I believe ISO starts on a Monday)
• So I check to see what day of the week the first day of the year is
• If its greater than Wednesday (day of week #3), don't add 1 to the calculation
• If its before Wednesday, add 1 to the calculation

When I do this it seems to work (haven't done full testing on more than a few dates).

Post Prodigy

That's great,   I actually found your other calculation working fine that you made in another thread:`add(div(div(sub(ticks(outputs('Date')),ticks(formatDateTime(outputs('FirstDateOfYear'),'yyyy-MM-dd'))),864000000000),7),1)`

But this new one might be more wholesome 😊

@ccc333ab

4 REPLIES 4
Solution Sage

@shavora I think your formula is correct.

When using your exact formula I actually get the result of:
July 22, 2022 = Week 31

July 29, 2020 = Week 31 (you say you are getting Week 30)

Now when I look in Outlook at the week numbers it says:

July 29, 2022 = Week 31 (you say it should be Week 30)

July 29, 2020 = Week 31 (you say it should be week 30)

And from outlook: July 29, 2020:

And July 29, 2022:

Post Prodigy

My outlook says this @ccc333ab :

Maybe you have another kind of calculation on your calendar? Here is a link using iso week numbers.

You must have misunderstood, I didn't mention what the first formula produced for 2020. The first formula using add +1 is working well for 2020. It gives week 31. So to clarify, add(div(dayOfYear(outputs('Date')),7),1) works well for 2020.

Solution Sage

Ahh...OK.

My Calendar is based on the US settings which starts the year on the first day of the year regardless if its the start of the week or not. Now based on ISO, I believe it sets week 1 of a year based on a week at least having 4 days of the following year. So based on that I created the following formula.

``if(greater(dayofweek(concat(formatDateTime(triggerBody()['date'],'yyyy'),'-01-01')),3),div(dayofyear(triggerBody()['date']),7),add(div(dayofyear(triggerBody()['date']),7),1))``

I'm sure there is a more eloquent way of doing this but this:

• Four days require at least starting on Thurs (if your week starts on a Monday, I believe ISO starts on a Monday)
• So I check to see what day of the week the first day of the year is
• If its greater than Wednesday (day of week #3), don't add 1 to the calculation
• If its before Wednesday, add 1 to the calculation

When I do this it seems to work (haven't done full testing on more than a few dates).

Post Prodigy

That's great,   I actually found your other calculation working fine that you made in another thread:`add(div(div(sub(ticks(outputs('Date')),ticks(formatDateTime(outputs('FirstDateOfYear'),'yyyy-MM-dd'))),864000000000),7),1)`

But this new one might be more wholesome 😊

@ccc333ab

Announcements

#### Announcing | Super Users - 2023 Season 1

Super Users – 2023 Season 1    We are excited to kick off the Power Users Super User Program for 2023 - Season 1.  The Power Platform Super Users have done an amazing job in keeping the Power Platform communities helpful, accurate and responsive. We would like to send these amazing folks a big THANK YOU for their efforts.      Super User Season 1 | Contributions July 1, 2022 – December 31, 2022  Super User Season 2 | Contributions January 1, 2023 – June 30, 2023    Curious what a Super User is? Super Users are especially active community members who are eager to help others with their community questions. There are 2 Super User seasons in a year, and we monitor the community for new potential Super Users at the end of each season. Super Users are recognized in the community with both a rank name and icon next to their username, and a seasonal badge on their profile.    Power Apps  Power Automate  Power Virtual Agents  Power Pages  Pstork1*  Pstork1*  Pstork1*  OliverRodrigues  BCBuizer  Expiscornovus*  Expiscornovus*  ragavanrajan  AhmedSalih  grantjenkins  renatoromao    Mira_Ghaly*  Mira_Ghaly*      Sundeep_Malik*  Sundeep_Malik*      SudeepGhatakNZ*  SudeepGhatakNZ*      StretchFredrik*  StretchFredrik*      365-Assist*  365-Assist*      cha_cha  ekarim2020      timl  Hardesh15      iAm_ManCat  annajhaveri      SebS  Rhiassuring      LaurensM  abm      TheRobRush  Ankesh_49      WiZey  lbendlin      Nogueira1306  Kaif_Siddique      victorcp  RobElliott      dpoggemann  srduval      SBax  CFernandes      Roverandom  schwibach      Akser  CraigStewart      PowerRanger  MichaelAnnis      subsguts  David_MA      EricRegnier  edgonzales      zmansuri  GeorgiosG      ChrisPiasecki  ryule      AmDev  fchopo      phipps0218  tom_riha      theapurva  takolota     Akash17  momlo     BCLS776  Shuvam-rpa     rampprakash   ScottShearer     Rusk   ChristianAbata     cchannon   Koen5     a33ik        AaronKnox        Matren        Alex_10        Jeff_Thorpe        poweractivate        Ramole        DianaBirkelbach        DavidZoon        AJ_Z        PriyankaGeethik        BrianS        StalinPonnusamy        HamidBee        CNT        Anonymous_Hippo        Anchov        KeithAtherton        alaabitar        Tolu_Victor        KRider        sperry1625        IPC_ahaas      zuurg     rubin_boer     cwebb365       If an * is at the end of a user's name this means they are a Multi Super User, in more than one community. Please note this is not the final list, as we are pending a few acceptances.  Once they are received the list will be updated.