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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
mgirvin
Advocate I
Advocate I

Lookup nth item in column

Dear Team,

 

In Excel we can lookup a value using INDEX and MATCH function, where the MATCH function returns the row number or relative position of an item in a list. How do we do this is DAX? Specifically, I have a calculated column and this formula that returns the relative position (or row number) of an item in a column:


=COUNTROWS(FILTER(disDiscount,disDiscount[Units]<=fSales[Quanity]))

For each row in this Calculated Column, that formula is returning a number between 1 and 7, which represents the row in a column that contains the item I want to go and get and bring back to the calculated column.

I can't use a formula like this to lookup the value:

=CALCULATE(MAX(disDiscount[Discount]),FILTER(disDiscount,disDiscount[Units]<=fSales[Quanity]))

 

becasue the value I am looking up is not always the MAX value. 

 

I need a lookup formula that return an item in a column based on its position.

 

What DAX Function can I use to lookup an item in a column based on its position?

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

Add an Index column in your query?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Using your approach of adding an index column, I came up with this solution that works:

=LOOKUPVALUE(disDiscount[Discount],disDiscount[Index],COUNTROWS(FILTER(disDiscount,disDiscount[Units]<=fSales[Quanity])))

Another solution I found in the comments at YouTube is this:

=LOOKUPVALUE(disDiscount[Discount],disDiscount[Units],CALCULATE(MAX(disDiscount[Units]),FILTER(disDiscount,disDiscount[Units]<=fSales[Quanity])))

where the logic is to find max value in first column of lookup table (units) and then use that as an Exact Match in LOOKUPVALUE.

View solution in original post

10 REPLIES 10
Greg_Deckler
Super User
Super User

Add an Index column in your query?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

That would work. Thank you, Greg!

But, my question remains: What DAX Function can I use to lookup an item in a column based on its position? If there is not a function like this, it seems strange that such a common function in Excel (INDEX), would not be in DAX.

Well, I would only say that DAX isn't really geared toward "position" so much. Under the hood things really aren't sorted neatly into ordered columns and rows like a sheet in Excel. You have to filter your way to specific values because there is no reference frame like A12, B13, etc.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

O, yes... That does make perfect sense becasue the Columnar Database stores unique list : )


Using your approach of adding an index column, I came up with this solution that works:

=LOOKUPVALUE(disDiscount[Discount],disDiscount[Index],COUNTROWS(FILTER(disDiscount,disDiscount[Units]<=fSales[Quanity])))

Another solution I found in the comments at YouTube is this:

=LOOKUPVALUE(disDiscount[Discount],disDiscount[Units],CALCULATE(MAX(disDiscount[Units]),FILTER(disDiscount,disDiscount[Units]<=fSales[Quanity])))

where the logic is to find max value in first column of lookup table (units) and then use that as an Exact Match in LOOKUPVALUE.

O, I added an index to the lookup table. You said "add an idex column in your query", how would I do that? Is there a way to use ADDCOLUMNS to add an index internally in the formula?

In the Query Editor, click your query, click "Add Column" tab in ribbon and then Index Column.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Yes, I did add an index column. But I was wondering if there was a way to do it internally in the formula?

Eh, maybe, see this post:

 

https://stackoverflow.com/questions/38599531/dax-create-dynamic-index-column

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Thank you very much for the help Greg : )

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.