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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors