• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Return a list based on multiple conditions in Power Query

ak123

New Member
I have some forecast data which is currently structured like so (i made the below in excel just to give you an idea) -
forecast timestampsettlement datePeriod 1Period 2Period n
01/01/2000 09:3005/01/20000.8999140.4515630.6084770.009698
06/01/20000.8915230.824220.0300270.0654
07/01/20000.6637290.2979370.8740330.743295
08/01/20000.5007660.5652750.1429540.652788
09/01/20000.1941980.0171050.9369810.884122
10/01/20000.508480.4887650.6894770.405967
01/01/2000 13:3005/01/20000.8227750.3388630.5564130.194292
06/01/20000.1434460.1362070.7421180.850311
07/01/20000.6234090.1934690.1421940.321664
08/01/20000.8101110.0174590.8966120.628561
09/01/20000.6409970.176880.8717790.345542
10/01/20000.4687830.2185560.1583360.987228
01/01/2000 21:3005/01/20000.5937710.5066650.6642360.094826
06/01/20000.3928370.4026040.3589110.831175
07/01/20000.8116330.231890.0685490.602519
08/01/20000.3785390.9077860.684050.132635
09/01/20000.0689820.4777730.2834420.547764
10/01/20000.3039740.485170.9718590.823869

The data is sorted according to the settlement date (ascending). Also there are 3 forecasts a day as you can see in the LHS column (note that these times are not static and can vary by a few minutes).

What I would like to have is a column at the end of the table, containing a list (per each row or settlement date) which shows all of the forecast timestamps -> which are between the settlement date for that row and the settlement date - 14 days.

So for example, if the row's settlement date was the 15th January; there would be a list containing all of the 14 * 3 time stamps = 42 time stamps (1 Jan 09:30, 1 Jan 14:30, 1 Jan 21:30, ... 14 Jan 21:30 ) between the 1st and 15th January.

I'm thinking this could be done by a "vlookup" in to a column of all distinct time stamps (which I've been able to create), I'm just unsure of how to create this additional column.

Once I've created this column of lists - I would then like to do another "vlookup" for each time stamp inside that list and retrieve the values for Period 1, Period 2, ..., Period n.

Any help would be greatly appreciated!
 
In the forecast timestamp column, are blank cells taken to be the same as the value above the blank cells or remain as blank (and therefore ignored)?
It would be much better if you were to attach a file containing more data so that we can test properly for 14 days' results and so that we know what's really going on with the Period n and columns in order to record/write proper M code.
 
hi pascal,

sure, the blank cells should be the same as the value above please. i'll post below some more context about what i'm trying to solve.

I'm envisaging a table that looks like below which contains a new column of lists.
forecast timestampsettlement dt - 14settlement datePeriod 1Period 2Period ntimestamps within date range
10/01/2000 09:3001/01/200015/01/20000.6940760.8364120.470130.264499list
02/01/200016/01/20000.80360.0333090.4606030.097421list
03/01/200017/01/20000.2791290.7612340.8292950.582785list
04/01/200018/01/20000.6631470.5093030.7985640.5087list
05/01/200019/01/20000.6990190.3014920.7352260.385019list
06/01/200020/01/20000.6693950.294490.6101920.442273list
10/01/2000 13:3001/01/200015/01/20000.1326740.0788590.3515410.539105list
02/01/200016/01/20000.5131590.1295520.3867610.986358list
03/01/200017/01/20000.7574510.9035530.1682240.148062list
04/01/200018/01/20000.2964390.8288340.2349680.263399list
05/01/200019/01/20000.0104510.4806230.1355640.41822list
06/01/200020/01/20000.0530520.0611750.7543880.03808list
10/01/2000 21:3001/01/200015/01/20000.4603260.2424160.7238050.072719list
02/01/200016/01/20000.3548410.3428650.2611040.238647list
03/01/200017/01/20000.2058880.1160110.6168830.008302list
04/01/200018/01/20000.4676150.7059120.4873250.946343list
05/01/200019/01/20000.205220.1548130.792440.255762list
06/01/200020/01/20000.4395270.0630460.2303210.414235list

If we expanded the list on the first row (where settlement date = 15/1/2000), we would end up with a list of all of the timestamps (from the 1st column) that lie between settlement dt - 14 and settlement date.
i.e. this is what it should look like after expanding the list in the first row:
01/01/2000 09:30
01/01/2000 13:30
01/01/2000 21:30
14/01/2000 21:30

upon checking the condition i.e. that the timestamp should be >= settlement date - 14d AND timestamp <= settlement date, the list should return all timestamps that satisfy this condition... from the 1st column - it should not be an automated date function that returns a sequence of dates between a start / end date.

this is because the hh:mm portion of the datetime is not constant i.e. it does not always follow the pattern of 09:30 -> 13:30 -> 21:30. it can vary by up to 20 minutes for example...

once it's possible to create the column of lists above, i want to isolate only the columns for settlement date, timestamps within the date range and the values (like so):
settlement datetimestamps within date rangePeriod 1Period 2Period n
15/01/200001/01/2000 09:30----
01/01/2000 13:30----
01/01/2000 21:30----
----
14/01/2000 21:30----
16/01/200002/01/2000 09:30----
02/01/2000 13:30----
02/01/2000 21:30----
----
15/01/2000 21:30----

i will retrieve the values for the "period 1, period 2, ..." columns by doing a merge on the first table.

thank you for looking into this!!
 
Not using a settlement dt - 14 column but sticking with your table in msg#1, first land the table into Power Query with a query named Table1:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.Buffer(Table.TransformColumnTypes(Source,{{"forecast timestamp", type datetime}, {"settlement date", type date}}))
in
    #"Changed Type"
Then another query:
Code:
let
    Source = Table1,
    CTSL = List.Buffer(Source[forecast timestamp]),
    #"Added Custom2" = Table.AddColumn(Source, "timestamps within date range", each let sd = [settlement date] in List.Select(CTSL,each _ <= DateTime.From(sd) and _ >= DateTime.From(Date.AddDays(sd,-14))))
in
    #"Added Custom2"
should give you your lists.
(CTSL=Complete Time Stamp List)
It would be so much easier to attach a file here, easier even than pasting a table, then I could give you a working example.
 
tyvm pascal!!

it's so close.
the only thing now is, when i click on one of the lists - it returns duplicates.
is there a way to filter the list of timestamps so that it only shows unique values?
 
You could List.Distinct here:
Code:
= Table.AddColumn(Source, "timestamps within date range", each let sd = [settlement date] in List.Distinct(List.Select(CTSL,each _ <= DateTime.From(sd) and _ >= DateTime.From(Date.AddDays(sd,-15)))))
but it would be more efficient to do it here:
Code:
CTSL = List.Buffer(List.Distinct(Source[forecast timestamp])),
 
Back
Top