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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
maiiiii
New Member

power Query similar to Xlookup in excel

HI, I am trying to use power query that perform similar funtionality as xlookup. 

 

I have 2 tables Table 1 and Table 2. I am trying to look for a matching data in Table 2 and if there are no match found, it returns the next larger number. In excel, i can use xlookup (table1, table2, table2,) with match mode of  1. But I am struggling in converting this to power query. 

1 - Exact match. If none found, return the next larger item.

 

 

Table 1

3

2

5

 

Table 2

1.1

2.5

3.3

6

 

Result:

3.3

2.5

6

2 ACCEPTED SOLUTIONS
v-heq-msft
Community Support
Community Support

Hi @maiiiii ,
You can try the follow code
Create a function

let
    NextLarger = (lookupValue as number, lookupTable as table) as number =>
    let
        // Filter the table to find values greater than or equal to the lookup value
        FilteredTable = Table.SelectRows(lookupTable, each [Column1] >= lookupValue),
        // Get the first row from the filtered table
        FirstRow = Table.FirstN(FilteredTable, 1),
        // Extract the value from the first row
        Result = if Table.IsEmpty(FirstRow) then null else FirstRow{0}[Column1]
    in
        Result
in
    NextLarger

Invoke the function in table 1

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlKK1YlWMgaTpkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}}),
    SortedTable2 = Table.Sort(#"Table 2",{{"Column1", Order.Ascending}}),
    AddCustom = Table.AddColumn(#"Changed Type", "Result", each NextLarger([Column1], SortedTable2))
in
    AddCustom

Final output

vheqmsft_0-1723096082357.png

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

 

View solution in original post

dufoq3
Super User
Super User

Hi @maiiiii, another solution:

 

Result

dufoq3_0-1723102517551.png

 

let
    Table1 = Table.FromList({3,2,5}, (x)=> {x}),
    Table2 = Table.FromList({1.1, 2.5, 3.3, 6}, (x)=> {x}),
    StepBackT1 = Table1,
    Ad_Xlookup = Table.AddColumn(StepBackT1, "Xlookup", each List.Select(List.Buffer(List.Sort(Table2[Column1])), (x)=> x >= [Column1]){0}?, type number)
in
    Ad_Xlookup

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

3 REPLIES 3
dufoq3
Super User
Super User

Hi @maiiiii, another solution:

 

Result

dufoq3_0-1723102517551.png

 

let
    Table1 = Table.FromList({3,2,5}, (x)=> {x}),
    Table2 = Table.FromList({1.1, 2.5, 3.3, 6}, (x)=> {x}),
    StepBackT1 = Table1,
    Ad_Xlookup = Table.AddColumn(StepBackT1, "Xlookup", each List.Select(List.Buffer(List.Sort(Table2[Column1])), (x)=> x >= [Column1]){0}?, type number)
in
    Ad_Xlookup

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

v-heq-msft
Community Support
Community Support

Hi @maiiiii ,
You can try the follow code
Create a function

let
    NextLarger = (lookupValue as number, lookupTable as table) as number =>
    let
        // Filter the table to find values greater than or equal to the lookup value
        FilteredTable = Table.SelectRows(lookupTable, each [Column1] >= lookupValue),
        // Get the first row from the filtered table
        FirstRow = Table.FirstN(FilteredTable, 1),
        // Extract the value from the first row
        Result = if Table.IsEmpty(FirstRow) then null else FirstRow{0}[Column1]
    in
        Result
in
    NextLarger

Invoke the function in table 1

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlKK1YlWMgaTpkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}}),
    SortedTable2 = Table.Sort(#"Table 2",{{"Column1", Order.Ascending}}),
    AddCustom = Table.AddColumn(#"Changed Type", "Result", each NextLarger([Column1], SortedTable2))
in
    AddCustom

Final output

vheqmsft_0-1723096082357.png

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

 

wdx223_Daniel
Super User
Super User

NewStep=let a=Table.Buffer(Table.Sort(Table2,{"ColOfTable2"}) in Table.AddColumn(Table1,"ValueFromTable2",each Table.Skip(a,(x)=>x[ColOfTable2]<[ColOfTable1]){0}?[ColOfTable2]?)

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.