Skip to main content
Power Automate
    • Connectors
    • Templates
    • Take a guided tour
    • Digital process automation
    • Robotic process automation
    • Business process automation
    • Process Mining
    • AI Builder
  • Pricing
  • Partners
    • Blog
    • Documentation
    • Roadmap
    • Self-paced learning
    • Webinar
    • Business process and workflow automation topics
    • Overview
    • Issues
    • Give feedback
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • User groups
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Microsoft Power Automate Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power Automate
    • General Power Automate Discussion
    • Using Connectors
    • Building Flows
    • Using Flows
    • Power Automate Desktop
    • Process Mining
    • AI Builder
    • Power Automate Mobile App
    • Translation Quality Feedback
    • Connector Development
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Galleries
    • Community Connections & How-To Videos
    • Webinars and Video Gallery
    • Power Automate Cookbook
    • Events
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Community Engagement
    • Community AMA
    • Community Blog
    • Power Automate Community Blog
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power Automate Community
    • Galleries
    • Power Automate Cookbook
    • Delete Excel Rows X Amount of Days Old

    Delete Excel Rows X Amount of Days Old

    09-29-2020 09:04 AM

    Jcook
    MVP
    7986 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    Jcook
    Jcook
    MVP
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    Delete Excel Rows X Amount of Days Old

    ‎09-29-2020 09:04 AM

    Summary:

    This Flow deletes Excel Rows Older than X amount of days based on a Date column in Excel. The Excel file can be in SharePoint or OneDrive. Very little configuration is needed by the user to make this work.

     

    Importing Flow:

    When importing the Flow, you might get an error message. This is because the Excel file that the Flow is being pointed to does not exist in your Tenant. Click 'Save as new flow' if you get this message.

    Errors.png

     

    Example Scenario:

    I have a Excel file that has 2 columns,

    My Name Field is my unique Key Column.

    Date Hired, I will be checking and deleting all records that have a Date Hired older than 7 days.

    ** Your Date column MUST be in Date format in Excel **

     

    Here is what my Excel file looks like.

    Jcook_1-1601391723566.png

     

    The Flow:

    Jcook_0-1601391408199.png

     

    The trigger can be anything you want:

    Button, Schedule, Anything!

     

    Each Step (Actions):

    All 3 Variables need to be inputted by the user:

     

    Days: Enter number of days you want the Flow to check against the date column to delete. For example, -7 will delete all records 8 days old

    ** Don't forget to put the - **

     

    KeyColumn: Enter your Key Column from Excel. This column MUST have unique values.

    In my Excel file this column name is Name Field

     

    DateColumn: Enter the name of your Date Column from Excel

    In my Excel file this column name is Date Hired

     

    Variables.png

     

    Past Date: Only change the Destination time zone field. Change to your time zone

    List rows present in table: Only fill in required information marked with red *

     

    ** If your Excel file has more than 1500 records. Click the 3 dots on the List rows action and select settings. Than up the value on the Threshold and click done. **

    Date and Excel List All Rows.png

     

     

    Filter array - Records older than X: Do not touch anything in here

    Jcook_3-1601352177109.png

     

     

    For each record older than X: The loop itself do not touch

    Delete a row: Fill in ONLY -- Location, Document Library, File, Table --

    Apply to Each Loop.png

     

    Conclusion / How The Flow Works:

    That is it, some important things to consider:

    - Make sure your Key Column has unique values

    - Change the trigger to your liking

    - Don't forget to add the minus symbol in the Days Variable

    - Only change the values highlighted in Yellow

    - Make sure to check the amount of records in your Excel file and configure Threshold values as needed

    - Your Date column in Excel MUST be formatted as Date

     

    Want to know how this Flow works?

    We get todays date using utcNow() expression, than add days based on the amount of days entered into the Days variable. This date is converted to local time, and formatted like yyyy/MM/dd. This is to do easy comparison/evaluation.

    The expression I use is:

     

    addDays(utcNow(),variables('Days'))

     

     

    We list ALL rows in Excel because Excel connector has limitations on the Filters we can use. Also the Excel Connector does not like spaces in the Column names. Which is why Filter array is used instead.

    The Filter array grabs the Converted date from the first step and checks if the date is greater than the Excel date.

     

    Note the Excel date comes over as a serial date in Power Automate. This is a long Integer, so to convert the serial date into a date we can read and use, we use this expression:

     

    addDays('1899-12-30', int(item()?[variables('DateColumn')]), 'yyyy/MM/dd')

     

     

    Now in the Apply to each loop, we use the values returned from the Filter array (Which is only records older than X date)

    We use a Delete a row action to delete the record in Excel.

    I am using the item() expression to select the Key Value from the row inside the Loop.

    The syntax of this is:

    item()?['<Excel Column Name>']

     

    item()?[variables('KeyColumn')]

     

     

    Done!!

     

    If you have any questions let me know. Thanks!


    Did I answer your question? Mark my post as a solution!

    If you like my post please hit the Thumbs Up


    Proud to be a Flownaut!


    Check out my blog for Power Automate tips,
    tricks, and guides
    FlowAltDelete


    Follow@FlowAltDelete


    ExcelDeleteRecordsOlderThanXDaysTemplate_20200929160047.zip
    Labels:
    • Labels:
    • Automated Flows
    • Button flows
    • Scheduled flows
    Message 1 of 24
    7,986 Views
    4 Kudos
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    • « Previous
      • 1
      • 2
      • 3
    • Next »
    Jcook
    Jcook
    MVP
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2021 07:23 AM

    Hey @Dsym 


    Can you give me a sample of your excel file?

     

     


    Did I answer your question? Mark my post as a solution!

    If you like my post please hit the Thumbs Up


    Proud to be a Flownaut!


    Check out my blog for Power Automate tips,
    tricks, and guides
    FlowAltDelete


    Follow@FlowAltDelete


    Message 11 of 24
    1,162 Views
    0 Kudos
    Reply
    Dsym
    Dsym
    New Member
    In response to Jcook
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2021 07:34 AM

    Hi @Jcook See the below screenshot oh how the excel file is set up. Date column is in date format (short date). This is just a test at the moment hence the made up names and numbers etc.

    Dsym_0-1612884837243.png

     

    Message 12 of 24
    1,159 Views
    0 Kudos
    Reply
    Jcook
    Jcook
    MVP
    In response to Dsym
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2021 08:05 AM

    Thank you @Dsym 

     

    Can you send me the outputs of the List Rows action in Power Automate in Run history.

    Jcook_0-1612886739902.png

     


    Did I answer your question? Mark my post as a solution!

    If you like my post please hit the Thumbs Up


    Proud to be a Flownaut!


    Check out my blog for Power Automate tips,
    tricks, and guides
    FlowAltDelete


    Follow@FlowAltDelete


    Message 13 of 24
    1,155 Views
    0 Kudos
    Reply
    Dsym
    Dsym
    New Member
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2021 09:06 AM

     

    {"body":{"value":[{"@odata.etag":"","ItemInternalId":"a5c29481-a829-43ff-a433-b2d7d876cec6","Date":"44228","Name":"a","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"c8550d5c-f023-420e-9c53-0f9208ed3df4","Date":"44229","Name":"s","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"d0973eed-06e3-4402-888e-f2d2399bc2a8","Date":"44231","Name":"d","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"35897161-6051-4d3a-ad0c-173edfc1ba55","Date":"44232","Name":"f","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"e4256e2e-7d8d-44a9-8d10-e0a7a7cfce75","Date":"44230","Name":"g","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"fd63fd99-1849-4b61-81e1-b7d5da349401","Date":"44233","Name":"h","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"0d8b635f-c880-4865-8335-2beea35142ef","Date":"44234","Name":"j","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"ce2dd1ef-ca6d-484e-a07a-541ed18c8a0e","Date":"44235","Name":"k","Phone Number":"","email address":"","Destination":"","International travel (14 days)":"","Close contact (48Hrs)":"","Symptoms":""},{"@odata.etag":"","ItemInternalId":"8b195829-791c-493a-bbbd-e5115babe1ca","Date":"44236.5030555556","Name":"ef","Phone Number":"ef","email address":"fe","Destination":"Sheringham Shoal Offshore","International travel (14 days)":"No","Close contact (48Hrs)":"No","Symptoms":"No"}]}}

    Message 14 of 24
    1,148 Views
    0 Kudos
    Reply
    Dsym
    Dsym
    New Member
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2021 09:09 AM

    @Jcook outputs of the list rows above. Hope its in the format you need.

    Message 15 of 24
    1,150 Views
    0 Kudos
    Reply
    Jcook
    Jcook
    MVP
    In response to Dsym
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-10-2021 09:23 AM

    Hey @Dsym 

     

    Thank you for the outputs, that is what I was looking for,

    In the outputs, the dates from excel come across like this:

    44228
    This is a serial date. Which is why we add the days to that number to get the real date in ISO format.
     
    The issue is some records in your outputs have a date of:
    44236.5030555556
    This is also a serial date, but the decimal specifies the time as well. The expression
    addDays('1899-12-30', int(item()?[variables('DateColumn')]), 'yyyy/MM/dd')
    Does not like that number, since the part int(...) is looking for a whole number.
     
    To fix this you can either
    1) Remove the time from the cell in excel (The cell may look correct, but if you select it, the date/time will show)

    OR

    2) Try to replace the expression in the Filter Array action.

    Instead of using  addDays('1899-12-30', int(item()?[variables('DateColumn')]), 'yyyy/MM/dd')

    USE

    addseconds('1899-12-30',int(formatnumber(mul(float(item()?[variables('DateColumn')]),86400),'0')))

     

     

    Let me know if this works,

    I am working on an updated version of this Flow which would make things way easier, which I will be posting on my blog this weekend.


    Did I answer your question? Mark my post as a solution!

    If you like my post please hit the Thumbs Up


    Proud to be a Flownaut!


    Check out my blog for Power Automate tips,
    tricks, and guides
    FlowAltDelete


    Follow@FlowAltDelete


    Message 16 of 24
    1,111 Views
    0 Kudos
    Reply
    Dsym
    Dsym
    New Member
    In response to Jcook
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-10-2021 11:19 AM

    Hi @Jcook Many thanks for the explanation. I tried changing the texts as suggested and although the test ran successfully, the excel sheet did not update. Reading through your explanation, I went back to the flow which pulled the data into the excel sheet from forms. The submission date carries over the time to, so I replaced this with 

    formatDateTime(utcNow(),'dd-mm-yyyy') and now it pulls just the date in. I've saved your template back to original (as the guide is) and everything is now functioning fine. Thank you so much for your help and assistance with this. Been a good learning curve for me!
    Message 17 of 24
    1,101 Views
    0 Kudos
    Reply
    Anonymous
    Not applicable
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎04-13-2021 06:58 PM

    Hi JCook,

    Firstly thanks for a great article. My team and I are based in Australia and are having a bit of trouble with this one. After a bit of tweaking (also reading all the comments) we can get the flow to run without errors. But the filter for a long time wouldn't return any rows legible for deletion. After changing the filter to 'less than' it now actually deletes, hurray! 

    However it is deleting everything and not excluding rows that it should be excluding. We think it might be to do with our time and date format in Australia. While we do use short date format, it's dd/MM/yyyy here and not MM/dd/yyyy. We think this might be the cause of our issue but haven't been able to nail it. Could you take a look at these screenshots and let me know what you think?

    2021-04-14 11_57_10-Server - NASBOX - TeamViewer.png2021-04-14 11_57_25-Server - NASBOX - TeamViewer.png

    Thanks!

    Message 18 of 24
    1,003 Views
    0 Kudos
    Reply
    Jcook
    Jcook
    MVP
    In response to Anonymous
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎04-13-2021 07:12 PM

    Hey @Anonymous ,

    Than you!

     

    Could you send me a sample of your excel file? 


    Did I answer your question? Mark my post as a solution!

    If you like my post please hit the Thumbs Up


    Proud to be a Flownaut!


    Check out my blog for Power Automate tips,
    tricks, and guides
    FlowAltDelete


    Follow@FlowAltDelete


    Message 19 of 24
    995 Views
    0 Kudos
    Reply
    Jcook
    Jcook
    MVP
    In response to Anonymous
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎04-13-2021 07:15 PM

    Hi @Anonymous 

     

    It just dawned on me, Can you try with the format 'yyyy/MM/dd' this is how dates are compared. My Flow converts both values to this date format before doing the compare.


    Did I answer your question? Mark my post as a solution!

    If you like my post please hit the Thumbs Up


    Proud to be a Flownaut!


    Check out my blog for Power Automate tips,
    tricks, and guides
    FlowAltDelete


    Follow@FlowAltDelete


    Message 20 of 24
    989 Views
    0 Kudos
    Reply
    • « Previous
      • 1
      • 2
      • 3
    • Next »

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign up free
    • Sign in

    Browse

    • Templates
    • Connectors
    • Partners

    Downloads

    • Mobile
    • Gateway

    Learn

    • Documentation
    • Learn
    • Support
    • Community
    • Give feedback
    • Blog
    • Pricing

    • © 2023 Microsoft
    • Contact us
    • Trademarks
    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Terms & conditions
    Consumer Privacy Act (CCPA) Opt-Out Icon Your Privacy Choices