cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ShaneFlakes
Regular Visitor

Getting previous week's date

Hi,

I have a flow in place where it generates a file to SharePoint. The file name's expression is as follows:

concat('ReportName_',formatdatetime(utcNow(),'yyyy_MM_dd'))
However, this process is changing and I will need to append last Friday's date to it instead of the day when it's exported. 
Does anyone know if this can be done? Any help is appreciated.
 
Thanks,
Shane
1 ACCEPTED SOLUTION

Accepted Solutions
Paulie78
Super User
Super User

You have your filter setup wrong, which is my fault as I didn't make it clear:

ItemWeekDay.PNG

In the expression editor put:

item()['weekDay']

That should do it.

View solution in original post

10 REPLIES 10
Paulie78
Super User
Super User

Try this:

lastFriday.png

The code for the lastWeekDays array is:

[
  {
   "date":"@{formatdatetime(addDays(utcNow(),-1),'yyyy_MM_dd')}",
   "weekDay":"@{formatdatetime(addDays(utcNow(),-1),'ddd')}"
  },
  {
   "date":"@{formatdatetime(addDays(utcNow(),-2),'yyyy_MM_dd')}",
   "weekDay":"@{formatdatetime(addDays(utcNow(),-2),'ddd')}"
  },
  {
   "date":"@{formatdatetime(addDays(utcNow(),-3),'yyyy_MM_dd')}",
   "weekDay":"@{formatdatetime(addDays(utcNow(),-3),'ddd')}"
  },
  {
   "date":"@{formatdatetime(addDays(utcNow(),-4),'yyyy_MM_dd')}",
   "weekDay":"@{formatdatetime(addDays(utcNow(),-4),'ddd')}"
  },
  {
   "date":"@{formatdatetime(addDays(utcNow(),-5),'yyyy_MM_dd')}",
   "weekDay":"@{formatdatetime(addDays(utcNow(),-5),'ddd')}"
  },
  {
   "date":"@{formatdatetime(addDays(utcNow(),-6),'yyyy_MM_dd')}",
   "weekDay":"@{formatdatetime(addDays(utcNow(),-6),'ddd')}"
  },
  {
   "date":"@{formatdatetime(addDays(utcNow(),-7),'yyyy_MM_dd')}",
   "weekDay":"@{formatdatetime(addDays(utcNow(),-7),'ddd')}"
  }
]

This builds output like this:

[
  {
    "date": "2020_11_11",
    "weekDay": "Wed"
  },
  {
    "date": "2020_11_10",
    "weekDay": "Tue"
  },
  {
    "date": "2020_11_09",
    "weekDay": "Mon"
  },
  {
    "date": "2020_11_08",
    "weekDay": "Sun"
  },
  {
    "date": "2020_11_07",
    "weekDay": "Sat"
  },
  {
    "date": "2020_11_06",
    "weekDay": "Fri"
  },
  {
    "date": "2020_11_05",
    "weekDay": "Thu"
  }
]

The array is then filtered to include only days which include weekday = Fri

 

The final compose then selects the date element of the only remaining element of the array.

lastFridayOutput.PNG

 

Please...

If I answered your question Accept it as a solution ✔️ 

If I helped you out, please give me some Kudos 👍

Thanks 😙

@Paulie78 , thanks for responding. I can give your solution a shot. In your Week Days Array, are you just setting the variable?

Also when those steps are done, do I just create the file like concat('ReportName_',outputs from compose) ?

Paulie78
Super User
Super User

Yes, just setting up the variable, it compiles an array of the last 7 days with the date formatted how you like it and the associated day name.

You can do a concat, or you can just type in ReportName_ and then select the output from the compose in dynamic content window next to it. Either will work.

 

Even easier you could modify the compose I did to just have ReportName_ in front of the date, like this:

NameLastFriday.PNG

Stick the file extension on the end and you've got your filename composed.

@Paulie78 , for some reason, my initialize variable & filter array is returning the same outputs as you shared earlier. For some reason, in my filter array, when I chose item from expressions, the item icon to the left of it is a clock, not an expression like you have it. 

Also, where is your date in the compose coming from? I replicated it and I am receiving this error 

Unable to process template language expressions in action 'Compose' inputs at line '1' and column '17798': 'The template language expression 'body('Filter_array')[0]['date']' cannot be evaluated because array index '0' cannot be selected from empty array. Please see https://aka.ms/logicexpressions for usage details.'.

 

Paulie78
Super User
Super User

You have your filter setup wrong, which is my fault as I didn't make it clear:

ItemWeekDay.PNG

In the expression editor put:

item()['weekDay']

That should do it.

krootz
Solution Supplier
Solution Supplier

There might be a simpler solution, but I was able to get last friday's date using below:

lastfriday.png

 

mul(add(sub(dayOfWeek(utcnow()),5),if(less(sub(dayOfWeek(utcnow()),5),0),7,0)),-1)
formatdatetime(addDays(utcnow(),outputs('Calc_Days_to_Go_Back')),'yyyy_MM_dd')
ShaneFlakes
Regular Visitor

@Paulie78 Thank you SO MUCH! That worked! Moving forward, say next Monday 11/16, if I run this flow again the file generated should show this Friday 11/13 right?

Paulie78
Super User
Super User

That’s the idea!

Hey @Paulie78 , quick question. I ran the flow again today and it's taking last Friday's date (which is fine from my original ask).

Would you know what change has to be made to keep the file exported that was today along with next week's (11/16-11/19) files to have today's date of 11/13 up until next Friday and from then on (11/20-11/26), to have the 11/20 date?

Thanks,

Shane

takolota
Memorable Member
Memorable Member

Also if anyone needs to get the date of the next monday, tuesday, friday, saturday... etc. Then you can use this example flow to continually get the date of any upcoming day of the week https://powerusers.microsoft.com/t5/Power-Automate-Cookbook/Date-of-Next-Chosen-Day-of-Week/td-p/146...

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

MPA Licensing.jpg

Ask your licensing questions at the Power Automate AMA!

Join Priya Kodukula and the licensing team, super users and MVPs to find answers to your questions on Power Automate licensing.

Users online (5,196)