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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
seppel123
Frequent Visitor

Power Query: Lookup a value in a give timeframe and return in custom column

Hi everyone,

 

i have a problem with a dataset that i tried to solve with a custom column in Power Query.

My data shows how long a Container ("Container", Nr. of container) is in the Depot ("Depot", time in days) and how long that Container was filled up again ("Filling 1", "Filling 2" or "Filling 3" time in hours). The problem is, that for every step there is a new line of data and every container can come multiple times in the year. 

 

Example:

ContainerFilling 1Filling 2Filling 3DepotStartEnd
GESU808378-910nullnullnull30.08.202330.08.2023
GESU808378-9nullnullnull8,81666666730.08.202308.09.2023
GESU808378-910nullnullnull30.08.202330.08.2023
GESU808378-9null10,21666667nullnull05.09.202306.09.2023
GESU808378-9nullnullnullnull25.09.2023null
GESU808378-923nullnullnull25.09.202325.09.2023
GESU808378-925nullnullnull18.10.202318.10.2023
GESU808378-9nullnullnull7,68472222218.10.202326.10.2023
GESU808378-9null1,866666667nullnull25.10.202325.10.2023
GESU808378-921nullnullnull06.11.202306.11.2023
GESU808378-9nullnullnull3,93819444406.11.202310.11.2023
GESU808378-9null1,783333333nullnull08.11.202308.11.2023
GESU808378-9nullnullnull4,97013888929.11.202304.12.2023
GESU808378-9nullnullnullnull29.11.2023null
GESU808378-9null3,266666667nullnull30.11.202330.11.2023
       

 

What i would like is to lookup the time frame inbetween that Container was in the Depot (start and end dates of that line where "Depot" has a value) and check if in that time frame there are values for that specific container in Filling 1,2 or / and 3. If thats the case fill the new custom column with the end date of the line of Filling 1, 2 or 3. At the end it could look like this: 

 

seppel123_0-1709285759389.png

 

Thank you in advance and best regards

Sebastian

1 ACCEPTED SOLUTION

I'll give you the general direction.  But I can't really judge how useful that would be to do in Power Query.  I'd rather do it in DAX or not at all.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZJNDoIwEEbvwnps5gfa6QGMBzCuCDcg7ry/FQUHaBGNs6B8kL686bRtq9PxfFFUCXqIFVSE6XG99f1qEXSojpFlHjpYQbL7FZT8UGFJS28Yi7R/KhEC0yQxp2EzWaTgt5SyNq+FLWb4lgG8f25uNyEHaUoQUkc4QkzY20oAr3XgRy1p7HfQCNT7wjGnngyt2aIxlQTTeIjMrMawt0GBKEqxTrWkJZ/PNIKg8qy1m1o3/dqthhiQRFWHM4iWVjvi326lxZRu5XQ4XJyeoOGY0HV3", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Container = _t, #"Filling 1" = _t, #"Filling 2" = _t, #"Filling 3" = _t, Depot = _t, Start = _t, End = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Filling 1 end", each if [Depot] = "null" then null else
try Table.SelectRows(Source,(k)=> [Container]=k[Container] and [Start]=k[Start] and k[Filling 1]<> "null")[End]{0} otherwise null)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

 

he problem is, that for every step there is a new line of data and every container can come multiple times in the year.

 

The first issue can be handled by unpivoting, the second one is covered by the Start Date.

 

lbendlin_0-1709421308593.png

lbendlin_1-1709421822168.png

 

 

 

 

fill the new custom column with the end date of the line of Filling 1, 2 or 3

 

which one? a random one? the last? the first?

 

Thank you so much for your help. 

Your Table by Start Date shows the results i would like to have in the table in Power Query. However, the listed end dates do not correspond with the end dates of the original data. 

 

The end date of the line of Filling 1, 2 or 3 should not be a random one. It should be the one that lies in between the Startdate and the Enddate of the corresponding "Depot" line. 

For example

Depot:  Startdate 18.10.2023 and Enddate 26.10.2023. 

Only Filling 2 has a value, so the Enddate 25.10.2023 is relevant and that value should be written in the new column. No other Filling dates fit with the timeframe between 18.10.2023 and 26.10.2023 for that specific container.

With this information further analysis of performance of the Depot is possible (how long each Container waits until it is filled, how long each Container waits when it is already filled to be picked up, ...). 

 

Hope that helped to clarify the purpose.

 

Best regards

Sebastian

I'll give you the general direction.  But I can't really judge how useful that would be to do in Power Query.  I'd rather do it in DAX or not at all.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZJNDoIwEEbvwnps5gfa6QGMBzCuCDcg7ry/FQUHaBGNs6B8kL686bRtq9PxfFFUCXqIFVSE6XG99f1qEXSojpFlHjpYQbL7FZT8UGFJS28Yi7R/KhEC0yQxp2EzWaTgt5SyNq+FLWb4lgG8f25uNyEHaUoQUkc4QkzY20oAr3XgRy1p7HfQCNT7wjGnngyt2aIxlQTTeIjMrMawt0GBKEqxTrWkJZ/PNIKg8qy1m1o3/dqthhiQRFWHM4iWVjvi326lxZRu5XQ4XJyeoOGY0HV3", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Container = _t, #"Filling 1" = _t, #"Filling 2" = _t, #"Filling 3" = _t, Depot = _t, Start = _t, End = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Filling 1 end", each if [Depot] = "null" then null else
try Table.SelectRows(Source,(k)=> [Container]=k[Container] and [Start]=k[Start] and k[Filling 1]<> "null")[End]{0} otherwise null)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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