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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Jeff_PowerBI
Frequent Visitor

Power Query: Lookup data from other table with date condition

Hi.

I have 2 tables and needed to get the data from table 2 with ceratin condition based on date.

It must return the column 'Cost' in table 1 if the date in Table 1 is greater than or equal to date in Table 2. See below sample.

 

Jeff_PowerBI_0-1701146180528.png

This can be achieved thru SQL but needed a simplified way in Power Query.

Your help is greatly appreciated.

3 REPLIES 3
spinfuzer
Solution Sage
Solution Sage

 

Hi @Jeff_PowerBI ,

This should take care of your null ID issue if you want to use the below instead.  You could also insert a dummy row in table2 with a null id and very early date and do the fill down method.

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    previousStep = Table.TransformColumnTypes(Source,{{"TRANSDATE", type date}, {"id", Int64.Type}}),
    #"Added Custom" = 
        let
            buff = Table.Buffer(Table.Sort(Table2, {{"id", Order.Ascending}, {"trans_date", Order.Descending}}))
            // must sort dates in descending order for Occurence.First below to work
        in
            Table.AddColumn(previousStep, "cost", each 
                try 
                    buff[cost]{
                        Table.PositionOf(
                        buff, 
                        _, 
                        Occurrence.First, 
                        (v,t) => t[id] = v[id] and v[trans_date] <= t[TRANSDATE]
                        // v is the table2 with costs and t is from table1
                        )
                    } 
                otherwise 0
            )
in
    #"Added Custom"

 

 

 

spinfuzer_2-1701669833817.png

 

 

 

AlienSx
Super User
Super User

Hi, @Jeff_PowerBI 

    append = Table.AddColumn(Table1, "data", each true) & Table.RenameColumns(Table2, {"trans_date", "TRANSDATE"}),
    sort = Table.Sort(append,{{"TRANSDATE", Order.Ascending}, {"cost", Order.Descending}}),
    fd = Table.FillDown(sort,{"cost"}),
    filtered = Table.SelectRows(fd, each ([data] = true)),
    rc = Table.RemoveColumns(filtered, "data")

Hi @AlienSx,

 

Thank you so much for the solution.

It worked on majority of IDs but since it used fill down, some IDs have data mismatch particularly if the ID has null cost.

Trying to work out how to solve this problem.

 

Thank you.

Helpful resources

Announcements
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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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