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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

Round to nearest value in a list

Good morning!

I'm trying to create a function that takes a number and rounds ir to the closest number on a given list of numbers in power query. 

 

This is what I've got so far: 

RoundToNearest = (input as number) =>
     let
           numberList = {1, 3, 4, 10},
           closest = List.Min(List.Transform(numberList, each [Value = _, Distance = Number.Abs(_ - input)]))[Value]
     in
           closest

 

However, this is giving me errors.

Any ideas?

Thanks!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

That function wasn't giving me quite the right values, but I used it as a base to come up with the following:

 

RoundToNearest = (input as number) =>
    let
       numberList = {10, 2, 3, 4, 5},
       difference = List.Min(List.Transform(numberList, each Number.Abs(_ - input))),
       closest = if List.Contains(numberList, input + difference) then input + difference else input - difference
    in
       closest

 

Essentially, I first get the difference between the input and the closest match on the list. Similar to before but only the difference (without the record that included value).

Then, because I used the absolute value, I excecute the if-statement to check whether I need to add or subtract the difference from the input, based on which of the two would give me a value on the numberList

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

That function wasn't giving me quite the right values, but I used it as a base to come up with the following:

 

RoundToNearest = (input as number) =>
    let
       numberList = {10, 2, 3, 4, 5},
       difference = List.Min(List.Transform(numberList, each Number.Abs(_ - input))),
       closest = if List.Contains(numberList, input + difference) then input + difference else input - difference
    in
       closest

 

Essentially, I first get the difference between the input and the closest match on the list. Similar to before but only the difference (without the record that included value).

Then, because I used the absolute value, I excecute the if-statement to check whether I need to add or subtract the difference from the input, based on which of the two would give me a value on the numberList

 

Vijay_A_Verma
Super User
Super User

You may use this

closest = [a = List.Transform(numberList, each Number.Abs(_ - input)),
r = numberList{List.PositionOf(a,List.Min(a))}][r]

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors