How to filter out current week in a datatable?

Hi UiPath,

I’m facing a difficult problem with my project.

Let say I have the sample Excel file below:

My goal is to filter out the rows with current week using “Created on” column.

Hope someone can help me

Thanks!

Alvin

Hi @alvin.c.apostol26

Use the below syntax

((CreatedOn_Variable.Date >= DateTime.Today.AddDays(-(CInt(DateTime.Today.DayOfWeek))) AndAlso CreatedOn_Variable.Date <= DateTime.Today.AddDays(6 - CInt(DateTime.Today.DayOfWeek))))

Hope it help!!

Hi @alvin.c.apostol26

  1. Create a new DataTable to hold the filtered data, let’s call it dtFilteredData.
  2. Use a For Each Row activity to loop through each row in dtInputData.
  3. Use if condition to check:

DateTime.Parse(row("Created on").ToString()).Date >= GetFirstDayOfWeek(DateTime.Today) AndAlso DateTime.Parse(row("Created on").ToString()).Date <= GetLastDayOfWeek(DateTime.Today)

  1. If true: add the row to the dtFilteredData DataTable using the Add DataRow activity.
1 Like

Hey @alvin.c.apostol26 , Try the below code

dtInput.As.Enumerable.Where(Function(row) row("Created On) < DateTime.Now.AddDays(-1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Monday)) mod 7).Date.ToString("MM/dd/yyyy") or row("Created On) > DateTime.Now.AddDays(1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Monday)) mod 7).Date.ToString("MM/dd/yyyy"))

This will give dates other than the current week days if you week starts from Monday and ends at Sunday

Hi @Quenton_Wayne_Rebello,

Thanks for your time

I’m getting an error below:

@alvin.c.apostol26

Please try this

dtInput.As.Enumerable.Where(Function(row) row("Created On") < DateTime.Now.AddDays(-1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Monday)) mod 7).Date.ToString("MM/dd/yyyy") or row("Created On") > DateTime.Now.AddDays(1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Monday)) mod 7).Date.ToString("MM/dd/yyyy"))

Hi @alvin.c.apostol26

Try this syntex in If condition

It’s working for me

Sorry for the error generated, try using the below code

dtInput.AsEnumerable.Where(Function(row) row("Created On").ToString < DateTime.Now.AddDays(-1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Monday)) mod 7).Date.ToString("MM/dd/yyyy") or row("Created On").ToString > DateTime.Now.AddDays(1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Monday)) mod 7).Date.ToString("MM/dd/yyyy"))