How to retrieve previous 7 days data from today

I have data like this in excel

image

I need to retrieve records based on the MailSentDateTime Column Where Only 7 day previous records should get picked.

Resulting Excel should be :

image

Excel File -
Book1.xlsx (11.9 KB)

Hi @Ishan_Shelke ,

Could you maybe check on the below Expression :

DT.AsEnumerable.Where(Function(x)CDate(x("MailSentDateTime").ToString).Date<Now.Date andAlso DateDiff(DateInterval.Day,CDate(x("MailSentDateTime").ToString).Date,Now.Date)<=7).CopyToDatatable

Let us know if this doesn’t work.

2 Likes

@Ishan_Shelke

Multiple ways

  1. Use filter activity in excel to filter the rows based on the days conditions…youc an select the number of days you want to filter
  2. Read the dat into datatable and use Filter datatable as "MailSendDateTime" > Now.Adddays(-7)
  3. Using linq dt.AsEnumerable.Where(function(x) Cdate(x("MailSentDateTime").ToString) > Now.Adddays(-7)).CopyToDatatable

Hope this helps

cheers

This is only fetching the 7th day data i.e 16th March, I need all the data from the last 7 days

Hi @Ishan_Shelke

  1. You can try with this LINQ expression
(From row In dtData.AsEnumerable()
Where row.Field(Of DateTime)("MailSentDateTime") = DateTime.Now.AddDays(-7)
Select row).CopyToDataTable()

This will retrieve the data from the dtData datatable where the Date column is greater than or equal to 7 days ago from today’s date. You can then use the CopyToDataTable() method to copy the filtered data to a new datatable

Regards
Gokul

Hey @Ishan_Shelke
Try this
Excel Tsk Forum.zip (77.7 KB)

image

Hope it helps
Cheers.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.