Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Previous Week Tuesday Date

Hi Everyone, Is there any way to find previous week "Tuesday" date by using Power Query. I have a requirement to filter table for every refresh & It should contains only 1 week data( Data from Tues...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Anonymous ,

     

    Please add the "Filter Row" step in my M code to yours.

    ...
    #"Filtered Rows" = 
        let _WeekStart = Date.AddDays(Date.StartOfWeek(Date.From(DateTime.FixedLocalNow()), 2),-Date.DayOfWeek(Date.From(DateTime.FixedLocalNow()), Day.Tuesday)),
            _WeekEnd = Date.AddDays(_WeekStart,6)
        in
        Table.SelectRows(#"Changed Type", each [Date] >= _WeekStart and [Date] <= _WeekEnd)
    ...

    The step above "Filter Row" step names #"Change Type", please change the step name as your before step name.

    The whole M code.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc67DcAwDAPRXVgLsCT/ZzG8/xohUoVIea+6c5CeWcKLBwzhuPaxpHWlShpKjbSUOqkqDdJUmqSttEhNaf/HmIZUev9x7wM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Value", Int64.Type}}),
        #"Filtered Rows" = 
        let _WeekStart = Date.AddDays(Date.StartOfWeek(Date.From(DateTime.FixedLocalNow()), 2),-Date.DayOfWeek(Date.From(DateTime.FixedLocalNow()), Day.Tuesday)),
            _WeekEnd = Date.AddDays(_WeekStart,6)
        in
        Table.SelectRows(#"Changed Type", each [Date] >= _WeekStart and [Date] <= _WeekEnd)
    in
        #"Filtered Rows"

    My Sample:

    After filter my table will only show data from 2022/10/11 to 2022/10/17 until next Tuesday 2022/10/18.

    Result is as below.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.