Forum Discussion
Rounding to the nearest Value Based on Values from another table.
- 4 years ago
Solution Excel @ https://1drv.ms/x/s!Akd5y6ruJhvhuWkS2pLB0BHnAigv?e=DDSrQv
Use this for Table 1. See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJSitWJVnICsozBLGeQmCWY6QJkGpqCma4gUQMw0w3ENAcz3UEKDMFMDyATIugJZAH1xwIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Location = _t, Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Location", type text}, {"Value", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Low", each Table.Last(Table.SelectRows(Table2, (x)=> x[Column1]<=[Value]))[Column1]), #"Added Custom1" = Table.AddColumn(#"Added Custom", "High", each Table.First(Table.SelectRows(Table2, (x)=> x[Column1]>=[Value]))[Column1]), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Result", each if Number.Abs([Value]-[Low])>=Number.Abs([Value]-[High]) then [High] else [Low]), #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Low", "High"}) in #"Removed Columns"If you need test code for Table2
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1YlWMgWThhCOEZSCCBoDebEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}}) in #"Changed Type"
Hi roncruiser ,
I guess the question is whether your example data is a fair reflection of the quantity of rounding values you want to use.
If it is, then a simple IF statement would be the smart way to go:
if [value] < 2.5 then 0
else if [value] >= 2.5 and [value] < 7.5 then 5
else if...
Pete
BA_Pete
The quantity of rounding values for the reference table, Table 2 is relatively accurate. Actual quantity of table 2 values will likely not exceed 150 values in the column. If it does, it will not exceed more than 300 values in rare cases.
Table one values may exceed more than 500 but no more than 1000 rows.
Thanks for quick the response. Will respond again soon.