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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MrMarshall
Helper II
Helper II

Lookup on previous rowindex and criteria

I need to lookup the closest value from previous rows based on the criteria (Group < 900). There is an Index column so we know what order to put the rows in.

I took out the following subset of data and I need help to calculate the "Desired Result" column.

For each row, I need to find the first row with (group < 900) from the rows above and I cannot figure this one out.

 

IndexOrderNrOrderRowGroupDesired Result
1100001110110
2100002900110
3100003910110
4100004900110
5100005120120
6100006910120
7100007905120
8100008900120
9100009910120
10100011100100
11100012910100
12100013905100

 

My first try was to filter on OrderNr and use the lowest Group on each OrderNr, but there can be multiple values below 900 for each OrderNumber so that didn't work.

 

Next thoght was to use the Lookup function, but that will error out since there is a lot of rows matching, I only want the closest one... 

LOOKUPVALUE(test[Group], test[Group], test[Group] < 900)

Ideas? 

1 ACCEPTED SOLUTION

Hi,

 

This calculated column formula works

 

=LOOKUPVALUE(Table1[Group],Table1[Index],CALCULATE(MAX(Table1[Index]),FILTER(Table1,Table1[OrderNr]=EARLIER(Table1[OrderNr])&&Table1[Group]<900&&Table1[Index]<=EARLIER(Table1[Index]))))

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

6 REPLIES 6
MFelix
Super User
Super User

Hi @MrMarshall,

 

Try the following measure:

 

Measure =
CALCULATE (
    MAX ( Test[Group] );
    Test[Group] < 900;
    ALLEXCEPT ( Test; Test[OrderNr] );
    FILTER ( ALL ( Test[Index] ); Test[Index] <= MAX ( Test[Index] ) )
)

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks @MFelix!

As a measure, that works like a charm! 
Now I realized that i badly need this as a column... 

 

Would this info be possible rewrite this to get as a column instead?

The one you provided does not work as Column I am afraid. 

 

ThScreenshot_277.png

Hi,

 

This calculated column formula works

 

=LOOKUPVALUE(Table1[Group],Table1[Index],CALCULATE(MAX(Table1[Index]),FILTER(Table1,Table1[OrderNr]=EARLIER(Table1[OrderNr])&&Table1[Group]<900&&Table1[Index]<=EARLIER(Table1[Index]))))

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @MrMarshall,

Why there is the neees to have it as a column?

A good.practice is to create measure when.you can, because they do not.add size to your data and are more flexible.in terms of the use you can.give them because they are context related.

Regards,
MFelix

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi!


I need to create other columns based on this one, and I also need to use the calculated column as Row value in a matrix.

Hi @MrMarshall,

 

To use the measure value in a matrix you just need to change the settings on the matrix and choose the option SHOW ON ROWS.

 

Regarding the calculation of another column you can always use a measure to calculate another measure however try the following code to create a new column instead of a measure:

 

Maximum_Per_group = 
var Temp_Table = CALCULATE(MAX(Test[Group]);FILTER(Test;Test[Index]<EARLIER(Test[Index]));Test[Group]<900;FILTER(Test;Test[OrderNr]=EARLIER(Test[OrderNr])))
return
if(Temp_Table= BLANK();Test[Group];Temp_Table)

Regards,

MFelix

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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