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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
paulj1
Helper II
Helper II

LOOKUPVALUE Nearest not exact

So i have a Table with column with  0, 2.5, 3.0, 3.5, 4.0, 4.5

 

I want to do a lookup value for nearest, in Excel id use VLOOKUP with the last parament True or False

 

So Looking up;

 

1.12 would return 0

2.62 would return 2.5

4.01 would return 4.0

 

Etc

 

How do i do this.....

 

Thanks in advance

 

Paul

 

7 REPLIES 7
TheMike
New Member

EDIT: solved - it's actually working - I had typed an incorrect year and was looking at a wrong date.

 

I'm trying to get this working for my case.


I have two tables:

  1. Table 1 - Products: product name, dates, price on date, etc.
  2. Table 2 - Exchange rates: unique dates, currencies, exchange rate

 

I'm trying to create a measure that, for each row of the Table 1 Products:

  • Gets the date
  • Finds the currency of that product (done)
  • Finds the nearest date in Table 2 Exchange Rates
  • Gets the exchange rate for that currency on the selected (closest) date

 

I've done a few attempts but so far what I get is:

 

- A date that is not the closest one

- No exchange rate for the [incorrectly] found closest date (even if it exists)

 

The measure is only working if there is an exact same date on Table 2 Exchange rates (which is impractical).


Below are my tables (sample):

 

Table 1 Products

DateProductPriceCurrencyAmountTOtal
26/02/2022XPTO €    23.86EUR59014074.45
31/01/2022XPTO €    24.08EUR59014207.20
31/12/2021XPTO €    19.31EUR59011392.90
21/12/2021XPTO €    19.44EUR59011468.42
17/12/2021XPTO €    18.80EUR59011089.64
10/12/2021XPTO €    19.44EUR59011467.24
04/12/2021XPTO €    19.36EUR59011421.22
24/11/2021XPTO €    18.36EUR59010833.58

 

Table 2 Exchange Rates

DateUSDEURGBP
27/02/20225.165.826.92
31/01/20225.336.107.21
15/01/20225.536.327.57
31/12/20215.576.337.54
21/12/20215.756.487.62
17/12/20215.706.407.54
10/12/20215.616.357.45
05/12/20225.656.407.48
24/11/20215.616.357.48
19/11/20215.616.347.55


My latest attempt in the measure:

=maxx(
TOPN (
1,
'Table2_Exchange_Rates',
ABS(
CALCULATE(
sum('Table1_Products'[Date]) -
sum('Table2_Exchange_Rates'[Date])
)
),ASC
),
'Table2_Exchange_Rates'[Date]
)

Thanks

cjayaneththi
Helper I
Helper I

1.12 would return 0 ???

 

is this correct? or it should be 1

Yes it is correct,needs to return a value that exists in the reference table....

Hi @paulj1,

 

If I understand your scenario correctly that you have two tables and you want to look up the value to match the value in another table?

 

If it is,  you could have a reference of this simialr thread.

 

If you still need help, please share your data sample, so that we can help further investigate on it?

 

Best Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

The thread you referenced is not the same issue. 

 

What if the "Search_Value" that we use in the LOOKUPVALUE column does not exist in the "Search_columnName". I'd like it to return the closest value in the "Search_columnName". 

 

In the original question 1.12 does not exist in our lookuptable but we want it to return 0 since that is the closest. Here is another thread which mentions a potential solution: https://stackoverflow.com/questions/52525377/return-top-value-ordered-by-another-column

 

You can modify the formula to find the closest value. Here is the result I came up with:

Closest Match Measure =
MAXX (
    TOPN (
        1,
        LookupTableName,
        ABS (
            CALCULATE (
                SUM ( 'DataTable'[SearchValue] ) - SUM ( LookupTableName[ReturnValue] )
            )
        ), ASC
    ),
    LookupTableName[LookupValue]
)

Basically we are just finding the smallest difference between our value and the values in the lookup table, and returning the result. 

Note that the above formula may need to me modified depending on whether you have relationships between your tables. I have found that storing the measure as a variable works in these situations

Closest Match using VAR =
VAR myMeasure = [MeasureName]
RETURN
    MAXX (
        TOPN (
            1,
            LookupTableName,
            ABS ( CALCULATE ( myMeasure - SUM ( LookupTableName[ReturnValue] ) ) ), ASC
        ),
        LookupTableName[LookupValue]
    )

 

 

 

Hey! I know this is an old post, but hoping someone can help. Is there a way to modify @Anonymous 's code so that instead of finding the smallest absolute value it finds the closest value in the lookup table that is not greater than the search value? (e.g. if search value is 9 and the closest lookup values are 5 and 10, I want it to return 5 since it's the closest match that is not greater than the seartch value (even though 10 has a smaller absolute difference)

@Anonymous 's solution works well for when there is only one lookup table. Do you have any suggestions for how this could work if there is a larger table involved, where there are multiple filter criteria?

 

Example below works for finding exact values in a large table that has 5 different domains and 4 different years. But it needs to be able to find the closest match and not just any exact matches.

 

Band = LOOKUPVALUE('Table 1'[Band],'Table 1'[Year],"9",'Table 1'[Domain],"Reading",'Table 1'[Scale Score],'2016 YR9 OutcomeLevelData'[READING_nb])
 
Edited - With limited knowledge this is what I came up with
 
Reading Band = CALCULATE(
MAXX (
    TOPN (
        1,
        'Table 1',
        ABS (
            CALCULATE (
                SUM ( 'Table 1'[Scale Score] ) - SUM ( '2016 YR9 OutcomeLevelData'[READING_nb] )
            )
        ), ASC
    ),
    'Table 1'[Band]
),
FILTER('Table 1','Table 1'[Domain]="Reading"),
FILTER('Table 1','Table 1'[Year]="9"))

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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