Only show rows from dates in the last 7 days

Hi,

I’m having an issue comparing the date in a csv file. I have a condition that should only add rows where the following condition is met however, it’s bringing in incorrect data:
((row(“StartDate”).ToString) > Datetime.Today.AddDays(-7).ToString(“dd/MM/yyyy”))

My condition should only show rows where the date is from the last week, however i’m getting data from months too.

Data in the CSV file is already formatted to dd/MM/yyyy…

Can anyone help?

Thanks

@qwerty1
Use DateTime.Compare to correctly filter your csv

If you want to keep only the dates from the last week do the following

Declare Datetime.Today.AddDays(-7).ToString(“dd/MM/yyyy”) as a variable, let’s say d2
Assing the date from the row also as a variable, let’s say d1

Now put them in the if condition like this: if(DateTime.Compare(d1,d2) >= 0) do something

Hi,

Thanks for coming back to me.
I’ve tried to update, though now have a bunch of errors.
I’ve tried ‘Get row’ for the Start date and store that in a DateTime var however, now getting am ‘Invalid L-Value expression…’ error

Do you want to share the workflow so I can look at it?

Also, don’t forget that you need to parse you stored dates as DateTime variables, because the DateTime.Compare accepts only DateTime variables, not strings

Thanks for coming back to me
Unfortunately i can’t share as it’s a work project.
How would I parse the stored dates?

Use the following:

d2 = DateTime.ParseExact(now.AddDays(-7).ToString(“dd/MM/yyyy”),“dd/MM/yyyy”,CultureInfo.InvariantCulture)

and for the date coming from the csv

d1 = DateTime.ParseExact(row(“StartDate”).ToString,“dd/MM/yyyy”,CultureInfo.InvariantCulture)

Thank you. I’ve tried to update my workflow, but i’m getting an error on the variable d1. I’ve stored it as of type System.DateTime however, its now throwing an error when i try to run it.
The error states: String was not recognized as a valid DateTime.

Any ideas?

I’ve made a quick update (i updated the Get row activity to output to D1, then used the assign activity like you’d said). Now i’m getting the following error:

Main has thrown an exception

Source: Get row

Message: 28/02/2019 is not a valid value for DateTime.

Exception Type: FormatException

try below…
dt_new = dt.select().where(function(row) Date.ParseExact(row(“Date”).tostring,“dd/MM/yyyy”,Nothing) >= now.AddDays(-7)).CopyToDataTable

Thank you, but i have a bunch of conditions that i need to present in one final excel.
Could i use something where it just compares the dates & adds to the datatable?

When you are using the ParseExact, you are specifing the format you are providing expected format (dd/MM/yyyy). If it’s not the same, it will give an error.
My guess will be to try with dd/M/yyyy or you cand use DateTime.Parse which will search the format for you

Thanks @paul.placintar , i tried that but unfortunately i receive the same error message

try to log the value before parsing it… like below:

log message= "-" & row("Date").tostring & "-"
you will get the exact value of that row... to check if there are any leading or trailing space/s

Hi, tried that - no leading or training spaces, just showing the date in the correct format that i’m trying to compare.

… The data in the spread sheet is already in dd/MM/yyyy format, but UiPath that is still throwing an error…

Hi, I tried that however i receive the following error:
“String was not recognized as a valid date time”
The error is flagged on the Assign…

log the row value before parsing it…
might be possible only one row have invalid string…
log the value exactly like I mentioned…

I tried that & the data looks good - no unwanted spaces in the dataset, but still showing an error

Main.xaml (8.0 KB)

This was a solution for another user, maybe it will help you

Thank you. I’ll try modifying that. Just disappointing that there is not a simpler way

can you upload a sample csv file…
so that we can check what causing the issue…