Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Filter date ranges for previous week

Hello,

I have the following Submissions table in Power Query. I would like to create a new table which contains only the rows where the ranges of dates from the start to end dates overlap in the previous week, based on today's date. Note: Every week is from Monday to Sunday.

My suggestion would be to expand the range of dates between the Start date and End date. The next step would be to restrict the rows to only the previous week's dates (from 04/04/2022 to 10/04/2022). I don't know how to do this in Power Query or if it is the best approach to solve this problem, so any advice is welcome!

Submissions table:

Student nameReasonStart dateEnd date
Cosamaloapan de Carpiolate28/03/202202/04/2022
Zagrodnosick01/04/202205/04/2022
Luntassick05/04/202208/04/2022
Frei Pauloblank07/04/202214/04/2022
Seedorfblank28/03/202211/04/2022
Bellegardesick18/04/202222/04/2022
Gangarampurlate28/03/202220/04/2022
Luntassick01/04/202210/04/2022
Zagrodnoblank07/04/202209/04/2022
Gangarampursick04/04/202204/04/2022

 

The results should be:

Student nameReasonStart dateEnd date
Zagrodnosick01/04/202205/04/2022
Luntassick01/04/202210/04/2022
Gangarampursick04/04/202204/04/2022
Luntassick05/04/202208/04/2022
Frei Pauloblank07/04/202214/04/2022
Zagrodnoblank07/04/202209/04/2022
Seedorfblank28/03/202211/04/2022
Gangarampurlate28/03/202220/04/2022
  • Yes, you are using UK date format whereas I used US date format. 

    Use this code which has dates in UK format. 

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZDBDoIwEER/hfRMQlsh4lUSvXgw8SbhsNqVEEpLCvy/RaNuE+G0meTNZHbKkhV2gA60hR5MpDAqwPWNZTHTMKI/Mk/4JpFcSi+4THj6FlVcsivUzioz00Nzb2dAfAEvsoA+TWaEgbAZZfOAPThsojNMes6+aTAvw5YYRBoYLojKugehg+JCBPQetcYanMJfHZGTdBk+egTjaej6yS0sI/n6r3QXwZdW/Psp3610+eSn1ECmqZ4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Student name" = _t, Reason = _t, #"Start date" = _t, #"End date" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start date", type date}, {"End date", type date}}),
        StartofPreviousWeekDate = Date.StartOfWeek(Date.AddDays(Date.From(DateTime.FixedLocalNow()),-7),1),
        EndofPreviousWeekDate = Date.EndOfWeek(Date.AddDays(Date.From(DateTime.FixedLocalNow()),-7),1),
        Result = Table.SelectRows(#"Changed Type", (x)=> x[End date]>=StartofPreviousWeekDate and x[Start date]<=EndofPreviousWeekDate)
    in
        Result

     

12 Replies

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    Frei Paulo should not be part of the output as range of dates from the start to end dates does not overlap in the previous week. Its end date is 14-Apr-22 which is outside the range.

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fdCxCoMwEAbgV5HMgkmq1K4V2qVDoVvF4dpcRYyJRH3/aqW9CNopB/k47v/znGW2gwa0hRZMoDDIwLWVZSHT0OP47CKZRpJLOc5xJOexCHN2h9JZZSbaVc/68y1IJiQvg+mh811CLiV3clgFVxj0tPOhwcx4T1jEpG+IyrqXR5enCkH2iFpjCU7h4lo/mJfsDGa00LSD2+yB/4vn1SD4amOr6Q5bJ/wWx4S/TRRv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Student name" = _t, Reason = _t, #"Start date" = _t, #"End date" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start date", type date}, {"End date", type date}}),
        StartofPreviousWeekDate = Date.StartOfWeek(Date.AddDays(Date.From(DateTime.FixedLocalNow()),-7),1),
        EndofPreviousWeekDate = Date.EndOfWeek(Date.AddDays(Date.From(DateTime.FixedLocalNow()),-7),1),
        Result = Table.SelectRows(#"Changed Type", (x)=> x[Start date]>=StartofPreviousWeekDate and x[End date]<=EndofPreviousWeekDate)
    in
        Result

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Vijay_A_Verma Thanks for your reply! Sorry if i was not clear on the dates overlap issue but what i meant was that if any date within the range is in the previous week, then that row should be kept. In the example results, Frei Paulo should be in the results since the dates 07/04 until 10/04 are in the previous week.

      Is it possible to implement this functionality?

      • Vijay_A_Verma's avatar
        Vijay_A_Verma
        Most Valuable Professional

        Everything is possible but I need following clarity. As per your logic even following should be part of result as dates are overlapping within Monday to Sunday of previous week i.e. between 4-10 Apr.

        Zagrodnosick1/4/20225/4/2022
        Seedorfblank28/03/202211/4/2022
        Gangarampurlate28/03/202220/04/2022