The other day I asked one of my co-workers how many ways he knew of to round a number. His answer was one… if it ends in .4 it rounds down and if it ends in .5 it rounds up. My guess is that most people would answer along similar lines, but that's NOT how Power Query's Round function actually works...
Interestingly though, there are a bunch of different ways to round, depending on your needs, and Excel has a bunch of functions to support them: ROUND, ROUNDUP, ROUNDDOWN, FLOOR, CEILING, EVEN, ODD, TRUNC, INT and MROUND.
Power Query also has a bunch of rounding formulas as well but, given that the function names can be somewhat different in Power Query (as we first saw here), I thought it might be interesting to see how to convert each of those functions from Excel to Power Query’s structure.
To start with, I created a very simple structure: a two column table with some random values in the “Value” column and then rounded them to 2 decimals using the formula =ROUND([@Value],2) The output, after feeding it through Power Query, looks like this:
The blue table on the left is the Excel table, and the green table on the right is the Power Query output. (There is a completed example file available here example file available here.)
Creating the Round function
I love the ROUND function in Excel. I use it constantly – rounding everything that uses multiplication or division – and pretty much have it burned into muscle memory. So to me this was a logical place to start with Power Query. Naturally, the syntax is just a bit different from Excel though:
|Power Query||=Number.Round(value, digits, roundingMode)|
Hmm… we know that the Power Query function will be case sensitive. In addition, it has an extra parameter. The valid options are:
Let’s see what we can do with this.
I open up my query, select Add Column, and put in the formula as shown below:
Pretty easy, just Number.Round([Value],2). In fact, it’s so similar to Excel it’s awesome!
So I click OK, save the query, and have a look at my results. And that’s when I notice something isn’t quite right. I’ve added some conditional formatting to the table below so you can see it easily:
This is the default?
Notice all the numbers that don’t match? Can you spot the pattern? It’s the oddest damn thing I’ve ever seen, to be honest, and I’ve never heard of anyone rounding in this way.
The default “RoundingMode” for Power Query is “Round to Even”. What that means is that if there is a tie in the numbers it will round up or down to the closest even number. So in the case of 1.645, it will round down to 1.64 as that is closer than 1.66. (1.64 is .05 away from 1.645, where 1.66 is 0.15 away from 1.645).
I find this deeply disturbing. I personally think that every user would expect Excel and Power Query’s default rounding methods to line up exactly, and this doesn’t. How serious is this? I’m not sure. I think I’ll let someone from the scientific community ponder that.
Since the default plainly doesn’t work for us, it looks like it’s time to figure out which of the additional parameters we need. Let’s try adding RoundingMode.Up to see if that will fix it.
I open Power Query again, and added a new custom column with the following formula:
And the results are as follows:
Um… uh oh. It seems to work above 0, but below is another matter. That –5.245 is rounding down, not up! (Yes, from a technical perspective I am aware you can argue the words I used, but you get the idea.)
Now I’d be surprised if this came up with numbers consistent with the Excel formula, but let’s just check it for good measure. The formula is:
And the results:
So now numbers greater than 0 get rounded down, where numbers less than 0 are being rounded up (away from zero).
Let’s try another:
Here’s our next option:
And these results are pleasing!
Look at that… we finally found the one that works!
We’ve only got one other option we haven’t explored, so we might as well use it too, just for the sake of completeness:
For some reason, I’m incapable of typing TowardZero the first time I type this. I always type TowardsZero and end up with an error! At any rate, the results:
As a tool that is built for the Excel audience, I am having some real difficultly accepting the default parameter for this function in Power Query. I HOPE that this is a bug, and not a design choice, although the documentation would suggest it is the latter. If that’s the case, I think it’s a HUGE mistake.
Excel’s ROUND formula defaults to round away from zero. Power Pivot’s DAX ROUND formula defaults to round away from zero. VBA’s Application.Round function defaults to round away from zero. (As pointed out by Rory Archibald on Twitter, VBA's Round function - without the application. prefix - does use banker's rounding though.)
In my impression, if the Power Query formula holds the same name (at least after the Number. portion) it should return the same results as the Excel function. In fact, I would venture to say that virtually every Excel pro would expect this.
My bigger concern would be that, with one of Power Query’s big selling features being it’s ability to re-shape and process large volumes of data, how quickly will a user realize that the Rounding function they thought they had is NOT working the way they expected? Not good news at all.
I’m curious to hear your impressions. Please leave a comment!
Want to see if for yourself?
Download the example file with all the formulas already in place.