Using an IF Function
- 04:49
Using an IF function.
Transcript
Using an IF Function Power BI formula can contain a DAX function, a DAX function gives us much more flexibility and how we calculate with our data. So instead of just using simple operators to perform mathematical equations.
We can use a function to perform more advanced calculations and derive information from all sorts of data even text as well as numbers and dates. If the formula uses a DAX function, it will always start with the column name followed by an equal sign. Then comes the function, first of all its name, followed by the information which is needed for that function to be performed and that is placed inside a set of brackets. Each piece of information is listed one after the other with a comma to separate them. They're known as arguments, an argument could be a field name a number or even a piece of text.
Some functions have one or two arguments, some have more than that. One of the most common and Powerful functions in DAX is the IF function. it has three arguments a logical test, a value if that test is true, and a value if that test is false.
A logical test is like a mini formula. It returns true or false and it uses operators such as greater than sign, equal sign, and less than sign. So for example, if we say 5 is less than 10, that's a logical test which would return true. If we say 5 is greater than 10, that's a logical test which would return false If we say 5 equals to 10, that's a logical test, which would also return false.
Instead of numbers like 5 or 10, we would normally use field names similar to what we do with a mathematical formula.
We can then display one answer if that test is true and a different answer if that test is false.
So, for example, we have two columns one contains the actual value and the other contains a threshold value. We want to work out if the threshold has been met, so we can use an IF statement to do that for us. We create a calculated column called threshold met. The function has three arguments. The Logical test would be the actual value is greater than or equal to the threshold value.
If that is true, we have the answer yes, and if it is false, we have the answer no.
Let's do a workout and see how to write that IF function in Power BI.
So I'm just going to get some data from an Excel workbook taking module 6 lesson 1B.
And I'm going to select a worksheet from here called trade values.
So I load that into my report and I'll go to data view by just clicking on this icon and make sure my table is selected so I can create a new column.
So the first thing I will do is create a new column just going to click here on your column from the table tools.
And I'll give my formula a name. First of all my new column I will call it threshold met.
Follow that by an equal sign and then I'll start my function. I'll type IF open brackets and I'm going to check to see if the actual value is greater than or equal to the threshold value, so when I start to type my field name we can see it comes up here and I'll just have that in and I'm using my operators a greater than sign followed by an equal sign and then I want to check to see is it greater than or equal to the threshold value, so there's the field name just tab that in.
No, that's the first argument, that's my logical test. It will either be true or false, so I'll put a comma in to indicate that I'm now going to insert the second argument and the second argument is the information I want to display if that test is true. Now, it can be any piece of text I want as long as I insert it in these quotation marks. So I'm going to put a yes here if that threshold has been met.
Put a comment in to enter my third argument and that is what should appear if the test is false and in this case, I just want a no.
Again, just making sure there that it goes into the double quotation marks. Now I can close my formula off with the closing bracket and press return.
And so we can see here that we have a yes or a no in each of the rows depending on whether the actual value is greater than or equal to the threshold value and again if I click on my report I can see I have a field noise, which can be used in my visuals.