Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ak123
Frequent Visitor

Return a list based on multiple conditions in Power Query

 

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! 

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @ak123 

 

This is my solution. Hope it would be helpful. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAwMDBQNLK2MDJR0lA1OYkFKsTrSSAkjIDFPIHFPIAlPIEkPI0ABFCMkBhsYD7AAjQ7o7IBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"forecast timestamp" = _t, #"settlement date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"forecast timestamp", type datetime}, {"settlement date", type date}}, "en-GB"),
    
    timeStampList = {#time(9,30,0), #time(13,30,0), #time(21,30,0)},
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Zip({List.Sort(List.Repeat(List.Dates([settlement date], 14, -1 * #duration(1, 0, 0, 0)),3)), List.Repeat(timeStampList, 14)})),
    #"Transform Custom" = Table.TransformColumns(#"Added Custom",{{"Custom", each List.Transform(_, each Text.Combine(List.Transform(_, Text.From), " "))}})
in
    #"Transform Custom"

vjingzhang_0-1664961431313.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Jing, thank you v much for your reply!

 

That was real close to what I'm looking for! I forgot to mention 1 other detail.

 

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 10 to 20 minutes...

 

once again, thank you v much for your help. fyi i'm posting some more context around what i'm trying to do below (if useful).

 

------------------

 

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 10 to 20 minutes...

 

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!!

 

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors