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
frankhofmans
Helper IV
Helper IV

Lookup closest value to a number

hi PBI experts,

 

i have the following questions:

 

i have 2 tables, one with all employee ID and their weekly salary, and one table with the amount of taxes that we have to withheld from the salary.

 

Table 1 (simplified):

 

Employee IDSalary
111800
1121100
113600
1141300
1151800
1163000
1171600
118700
EtcEtc

 

Table 2 (simplified):

 

SalaryTaxes
00
45050
65080
850110
1050130
1250170
1650220
2050260
3050380

 

I need the following output in the employee table:

 

Employee IDSalaryTaxes
11180080
1121100130
11360050
1141300170
1151800220
1163000260
1171600170
11870080

 

So i need the closest lower number. Does anyone know how to solve this? it is a large table, so an if(and(salary>=xx,salary<=xx) is no option.

 

Thanks in advance!

 

Regards,

 

Frank

1 ACCEPTED SOLUTION
d_gosbell
Super User
Super User


@frankhofmans wrote:

it is a large table, so an if(and(salary>=xx,salary<=xx) is no option.

 


I'm curious as to why this is not an option? A between filter like that would probably be the most efficient sort of scan compared to alternative approaches. What do you mean by the table being large - how many rows are in the employee and tax tables?

 

I can think of two possible solutions that would look up the lowest value.

 

You could do it in DAX using a calculated column in your employee table. You would find the max salary value in the tax rates table that is less than the salary for the current employee row, then use lookupvalue to get the tax for that lower salary range.

 

Tax = 
var salary = Employee[Salary]
var lowerRange = maxx(filter(all('Tax Rates'[salary]), 'Tax Rates'[Salary] < salary), 'Tax Rates'[Salary])
return LOOKUPVALUE('Tax Rates'[Taxes], 'Tax Rates'[Salary] , lowerRange)

 

Or you could use similar logic in Power Query by adding a custom column and putting the following expression in

 

Table.SelectRows(#"Tax Rates", (taxrow) => taxrow[Salary] = List.Max(List.Select(#"Tax Rates"[Salary], (salary) =>  salary < [Salary] ))){0}[Taxes]

 

 

View solution in original post

2 REPLIES 2
d_gosbell
Super User
Super User


@frankhofmans wrote:

it is a large table, so an if(and(salary>=xx,salary<=xx) is no option.

 


I'm curious as to why this is not an option? A between filter like that would probably be the most efficient sort of scan compared to alternative approaches. What do you mean by the table being large - how many rows are in the employee and tax tables?

 

I can think of two possible solutions that would look up the lowest value.

 

You could do it in DAX using a calculated column in your employee table. You would find the max salary value in the tax rates table that is less than the salary for the current employee row, then use lookupvalue to get the tax for that lower salary range.

 

Tax = 
var salary = Employee[Salary]
var lowerRange = maxx(filter(all('Tax Rates'[salary]), 'Tax Rates'[Salary] < salary), 'Tax Rates'[Salary])
return LOOKUPVALUE('Tax Rates'[Taxes], 'Tax Rates'[Salary] , lowerRange)

 

Or you could use similar logic in Power Query by adding a custom column and putting the following expression in

 

Table.SelectRows(#"Tax Rates", (taxrow) => taxrow[Salary] = List.Max(List.Select(#"Tax Rates"[Salary], (salary) =>  salary < [Salary] ))){0}[Taxes]

 

 

hi D_Gosbell,

 

Thanks for your reply. The employee table is a table with around 100k rows, the taxes table has around 2k rows. Option 1 works perfect for me. Many thanks!

 

Regards,

 

Frank

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.