PQ Question - Add Conditional Column - Based off today's relative date

scott_86_

New Member
Joined
Sep 27, 2018
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi,

For context, the date for today for this question is 15 Oct 2024 (plus 90 days is 13 Jan 2025).

In the sample snippet data below, I have an Expiry date column with past and future dates. The Number Outcome column is an example of what I am hoping to achieve with the new conditional column.

Could anyone help me out with adjusting the below code so that the x3 #date lines reflect the snippet in the 'add conditional column' below so that it utilises a PQ function instead of what I would normally use in Excel for this (the today function).

Although I had some initial success using Date.From(DateTime.LocalNow()), I was not able to adjust it further to add 90 days to this where necessary.


Power Query:
    Table.AddColumn(#"Extracted Date", "Expiry #",
    each if [Expiry] = null then 1
    else if [Expiry] <= #date(2024, 10, 15) then 4
    else if [Expiry] <= #date(2025, 1, 13) then 3
    else if [Expiry] > #date(2025, 1, 13) then 2
    else 0)


1728957280698.png
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
hi, use Date.AddDays to get today's date + 90 days.
Power Query:
let
    #"Extracted Date" = #table(type table [Expiry = date], {{#date(2024, 1, 1)}, {#date(2025, 1, 1)}, {#date(2026, 1, 1)}, {null}}), 
    tod = Date.From(DateTime.FixedLocalNow()), 
    tod90 = Date.AddDays(tod, 90), 
    col = Table.AddColumn(
        #"Extracted Date", 
        "Expiry #", 
        (x) => if x[Expiry] is null then 1 else {4, 3, 2}{List.PositionOf({tod, tod90, x[Expiry]}, x[Expiry], Occurrence.First, (c, v) => v <= c)}
    )
in
    col
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,105
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top