Posts by cobber1418

Important Notice


Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.

    MODERATOR NOTICE: This topic has also been posted on other sites and may already have an answer elsewhere. Please take this into consideration when answering this question

    Power Query dynamic previous months filter
    Hi all, how can I create a dynamic filter for 'previous months' but excluding last month, ie if it's currently October I want to filter for August and all…
    www.mrexcel.com


    Hi all, how can I create a dynamic filter for 'previous months' but excluding last month, ie if it's currently October I want to filter for August and all previous months. Must be dynamic so that in November it filters for September and all previous months.


    Is it possible to change the below code to suit?


    Code
    = Table.SelectRows(#"Sorted Rows", each Date.IsInPreviousMonth([Remedy Date Due]))


    Cheers Cobber.

    Hi KjBox , it works.....I just needed to remove the last End If to suit my code and it worked, cheers

    Code
    If Month(Date) > 1 Then
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    End If
    ElseIf Year(x(i, DtCol)) = Year(Date) - 1 And Month(x(i, DtCol)) = 12 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then

    Hi KjBox , is this all I need?

    Code
    If Month(Date) > 1 Then
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    End If
    ElseIf Year(x(i, DtCol)) = Year(Date) - 1 And Month(x(i, DtCol)) = 12 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    End If

    HI KjBox , I deleted a couple of lines and it worked perfectly. Thanks again, much appreciated.

    Code
    If Month(Date) > 1 Then
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    End If
    ElseIf Year(x(i, DtCol)) = Year(Date) - 1 And Month(x(i, DtCol)) = 12 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    'Your code here
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    End If

    Hi KjBox , I tried the below code and it ran without an error but still didn't count the data for Dec. Note, I deleted Category from the code, not required.

    Code
    If Month(Date) > 1 Then
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    'Your code here
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    End If
    ElseIf Year(x(i, DtCol)) = Year(Date) - 1 And Month(x(i, DtCol)) = 12 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    'Your code here
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 8) = InspType Then
    End If
    End If

    Hi royUK, I can't attach an example because it's from work. I assumed the problem is that the code substracts from a previous year, see below.


    If it isn't a simple change to the code I won't worry about it. Thanks anyway.


    Hi, I use the below code to select the previous months data but when I ran the code in January it didn't select the December data. Any help will be much appreciated.


    Code
    If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 7) = Category And x(i, 8) = InspType Then

    Hi KjBox , a trap for novices, I didn't actually need 1-10. I removed them and it's perfect.....thanks so much yet again.


    In regard to my other question,

    'Also, extra code to EXCLUDE current and previous month but includes ALL months prior ie if macro is run in Sept it only includes July and all prior months, needs to be dynamic.'


    Do I just need to change -1 to -2 after the Month(Date) in the code below?

    Code
     If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 7) = Category And x(i, 8) = InspType Then

    Hi, I need help with a date code that gives 'Type Mismatch' error and I would also like to modify the date code to exclude the current and previous month and only include all months prior. See notes in the code attached.