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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
Anonymous
Not applicable

Lookup a value in same table

Hi,

 

I am trying to do a lookup and calculation in Power Query, but cannot figure it out.

 

I need to find the previous date in a table, based on filters and subtract a day.

 

Start table (simplyfied):

ListProductStartdatePrice
L0001    Product A    1-1-2022          1            
L0001Product A13-2-20221,01
L0001Product A21-4-20221,05
L0001Product A1-11-20221,03
L0002Product A1-2-20221,05
L0002Product A1-2-20231,25
L0002Product B1-2-20235,2
L0002Product A1-4-20231,31
L0003Product C1-1-20220,35
L0003Product C31-10-20220,36
L0003Product C1-4-20230,41
L0003Product D31-10-20224,01
L0003Product D1-4-20234,18
L0001Product A1-5-20231,15
L0001Product D1-5-20234,01

 

End table:

ListProductStartdatePriceEnddate
L0001   Product A      1-1-2022     1           12-2-2022       
L0001Product A13-2-20221,0120-4-2022
L0001Product A21-4-20221,0531-10-2022
L0001Product A1-11-20221,0330-4-2023
L0002Product A1-2-20221,0531-1-2023
L0002Product A1-2-20231,2531-3-2023
L0002Product B1-2-20235,2 
L0002Product A1-4-20231,31 
L0003Product C1-1-20220,3530-10-2022
L0003Product C31-10-20220,3631-3-2023
L0003Product C1-4-20230,41 
L0003Product D31-10-20224,0131-3-2023
L0003Product D1-4-20234,18 
L0001Product A1-5-20231,15 
L0001Product D1-5-20234,01 

 

Formula used in the column Enddate (row 2) in Excel would be:

=IF(
MIN( IF( C:C>C2; IF( B:B'=B2; IF( A:A'=A2; C:C))))=0;
"";
MIN( IF( C:C>C2; IF( B:B'=B2; IF( A:A'=A2; C:C))))-1)

Or in words: Find the smallest [startdate] that is larger than the current [startdate] and has the same [list] and [product] and subtract one. If you cannot find one, leave blank.

 

I am trying to figure out how to do this in Power query, not in dax.

Any tips?

1 ACCEPTED SOLUTION
slorin
Super User
Super User

Hi

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZE9CgIxEIWvIqkHdv4ituqWFvbLVtoLovfxLJ7MQSGJwyQkRUK+eW9eZlnSCREpQTrfb9fn5bHZ2xlpss3IbBd6v+KVVojLSSbkUg723iPZjLQhc5e0fogaUgrJQfMcanZJ+ZIckwdPZuChpFZJqdGlIY/+jxEkd0mx6NiQ26FmcUfQ2H32mtqOyJN/mgq0G40Ic81O8TBnT/7c1w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [List = _t, Product = _t, Startdate = _t, Price = _t]),
Type = Table.TransformColumnTypes(Source,{{"Startdate", type date}, {"Price", type number}}),
Group = Table.Group(
Type,
{"List", "Product"},
{{"Data", each Table.FromColumns(
{
[Startdate],
[Price],
List.Transform(List.Skip([Startdate]), each Date.AddDays(_,-1))
},
{"Startdate", "Price", "Enddate"}),
type table [Startdate=nullable date, Price=nullable number, Enddate=nullable date]}}),
Expand = Table.ExpandTableColumn(Group, "Data", {"Startdate", "Price", "Enddate"}, {"Startdate", "Price", "Enddate"})
in
Expand

Stéphane

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thanks!
I am going to do some research on the list.transform and list.skip queries to understand it better, but it works.

Thanks a lot

slorin
Super User
Super User

Hi

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZE9CgIxEIWvIqkHdv4ituqWFvbLVtoLovfxLJ7MQSGJwyQkRUK+eW9eZlnSCREpQTrfb9fn5bHZ2xlpss3IbBd6v+KVVojLSSbkUg723iPZjLQhc5e0fogaUgrJQfMcanZJ+ZIckwdPZuChpFZJqdGlIY/+jxEkd0mx6NiQ26FmcUfQ2H32mtqOyJN/mgq0G40Ic81O8TBnT/7c1w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [List = _t, Product = _t, Startdate = _t, Price = _t]),
Type = Table.TransformColumnTypes(Source,{{"Startdate", type date}, {"Price", type number}}),
Group = Table.Group(
Type,
{"List", "Product"},
{{"Data", each Table.FromColumns(
{
[Startdate],
[Price],
List.Transform(List.Skip([Startdate]), each Date.AddDays(_,-1))
},
{"Startdate", "Price", "Enddate"}),
type table [Startdate=nullable date, Price=nullable number, Enddate=nullable date]}}),
Expand = Table.ExpandTableColumn(Group, "Data", {"Startdate", "Price", "Enddate"}, {"Startdate", "Price", "Enddate"})
in
Expand

Stéphane

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.