Boolean measure returning false for blanks
For some Aimplan features, you may want to define measure that returns True/False depending on a condition in your Power BI model. For example, when working with lock-measures. When doing so, you might have encountered that more rows than expected are returned and that values that are expected to be blank are shown as "False". This is all due to a quirk in Power BI and this document describes how it can be avoided.
Consider you have a dataset like this:
Product | Old Price | New Price |
---|---|---|
A | Blank | Blank |
B | 10 | 15 |
C | 20 | 15 |
And then you would like to define a Boolean (True/False) measure that displays if the price has gone up or not. Easy, we'll just define a measure like this:
We then drag that measure into a table along with the product name, only to find that it also returned a value for product A too; False. Not too strange. It's checking if blank is larger than blank, which it isn't since they're the same value.
Product | More Expensive |
---|---|
A | False |
B | True |
C | False |
So, to work around this and remove product A which doesn't have any values, we'll just make the measure return blank when new or old price is missing:
Aaand, nothing. It still shows the same result. Strange. 🤔
It turns out that Power BI tries to infer the return-type of a measure, and if it infers that the return type is Boolean, then it will try to coerce the blank to be a Boolean; False.
To prevent Power BI from coercing blanks to false, we must trick Power BI into thinking the return-type is not necessarily Boolean. This can be done by replacing the blank return-value with yet another if-statement.
Finally! Our measure is now actually returning blank for product A, which means it will be hidden unless "Show all data" is enabled.