March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe 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
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
Solved! Go to Solution.
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
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
Hi @maiiiii, another solution:
Result
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
Hi @maiiiii, another solution:
Result
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
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
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
NewStep=let a=Table.Buffer(Table.Sort(Table2,{"ColOfTable2"}) in Table.AddColumn(Table1,"ValueFromTable2",each Table.Skip(a,(x)=>x[ColOfTable2]<[ColOfTable1]){0}?[ColOfTable2]?)
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
21 | |
16 | |
13 | |
13 | |
9 |
User | Count |
---|---|
36 | |
31 | |
20 | |
19 | |
17 |