Forum Discussion

pmbaranski's avatar
pmbaranski
Frequent Visitor
5 years ago
Solved

Filtering Rows for all dates within the next two days, excluding weekends.

Hi all,   I am working in Excel. I have been having trouble with the advanced editor where I used to not have issues. For example, I tried modifying the editor to find all dates where the date is i...
  • edhans's avatar
    5 years ago

    You just need a third closing paren at the end of your Filtered Rows like to fix. As to your question on filtering pmbaranski try this. It turns this:

    into this filtered list:

    Today is Thursday the 6th. SO to get the next two days, you need the 7th (friday) and 10th (Monday) as the 8th and 9th are weekends. 

    Here is the full code that I worked with:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtI31DcyMDJQitWJVjJG5pjqm4I4hlCOGTLHApljZIjMMzRA4aHImSNzLKGcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
        #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
        #"Filtered Rows" = 
            Table.SelectRows(
                #"Sorted Rows", 
                let
                    varToday = DateTime.Date(DateTime.LocalNow()),
                    varFilterDate = if Date.DayOfWeek(varToday) <= 3 then Date.AddDays(varToday, 2) else if Date.DayOfWeek(varToday) = 6 then Date.AddDays(varToday, 3) else Date.AddDays(varToday, 4)
                in
                each [Date] >= varToday and [Date] <= varFilterDate
                )
    in
        #"Filtered Rows"

     

    If you just needed the 2 days and excluding the weekends, then this would work (if I got all of the math right 😁)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtI31DcyMDJQitWJVjJG5pjqm4I4hlCOGTLHApljZIjMMzRA4aHImSNzLKGcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
        #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
        #"Filtered Rows" = 
            Table.SelectRows(
                #"Sorted Rows", 
                let
                    varToday = DateTime.Date(DateTime.LocalNow()),
                    varFirstDay = if Date.DayOfWeek(varToday) <= 4 then Date.AddDays(varToday, 1) else if Date.DayOfWeek(varToday) = 5 then Date.AddDays(varToday, 2) else Date.AddDays(varToday, 1),
                    varLastDay = if Date.DayOfWeek(varToday) <= 3 then Date.AddDays(varToday, 2) else if Date.DayOfWeek(varToday) = 6 then Date.AddDays(varToday, 3) else Date.AddDays(varToday, 4)
                in
                each [Date] = varFirstDay or [Date] = varLastDay
                )
    in
        #"Filtered Rows"

    It returns this:

    How to use M code provided in a blank query:
    1) In Power Query, select New Source, then Blank Query
    2) On the Home ribbon, select "Advanced Editor" button
    3) Remove everything you see, then paste the M code I've given you in that box.
    4) Press Done
    5) See this article if you need help using this M code in your model.

     

  • pmbaranski's avatar
    pmbaranski
    5 years ago

    In review, i believe all that was needed was the adding a less than or equal to in the lasts line of the editor, as seen below;

     in
                each [Date] <= varFirstDay or [Date] = varLastDay
                )