Skip to main content

Power BI: Use Switch over IF To Make Life Easier

 


The IF function in DAX is a very popular logical function.  It will perform a logical check on a value expression and produce a value of True or False.  Based on the resulting True or False you can then decide what value you want to be reported.  Your value can be a hardcoded string value or some other calculation.  When you only have two outcomes to choose from the IF statement is very straightforward.  But what if you have 3 or more outcomes.  Then you will need to use more than one IF statement.  Instead of reporting a value or calculation for your first false result, you will code in another IF statement to run.  The more outcomes you need the more IF statements you will use.  This can be tiring to code and hard to read.  Here is where the SWITCH function comes into play.

 

The SWITCH function allows for an easier-to-read function compared to nested IF statements. The SWITCH function can always be used in place of the IF function.  The basic workings of the SWITCH function are you decide what expression you want to be checked.  Then you decide what value to look for and what result you want to be returned.  If you have multiple values to check for and multiple results you can just string them along by using commas. 

 


The SWITCH function has to be tweaked to check for inequalities because the SWITCH function is set to only search for a unique match and NOT a range of values.  To work around this issue we will incorporate the TRUE() function.  Take a look at this video to see how to take your complex IF statements and translate them into easier SWITCH statements.


If you enjoy this blog or any of my other videos and are interested in formal training on Power BI, Power Apps, Azure, or other Microsoft products you can use my code "Matt20" at check out when purchasing any private training or On-Demand Learning classes from https://pragmaticworks.com/pricing/

Comments

Popular posts from this blog

The Power of CALCULATE in Power BI

Last week in my blog and video I discussed how Power BI could have made my life as a teacher much easier.  It is not that Power BI can do things that I could not on my own.  It is the fact that Power BI could have saved me so much time.   This led me to some thoughts and questions.   How could I have impacted the students more in the classroom with this extra time?  Would teachers who were not analyzing student data due to time constraints now start?  How can I make the data tell my students a story that gives them an accurate picture of their progress?   That last question is what leads to this week’s blog.   Many times the metrics (which is just a fancy word for "numbers") of what we want to show are easy to do by hand with a basic math background.  Although easy, it can be very time consuming when dealing with different groups of data: like 6 periods of students instead of 1 period. When we want to do these calculations quickly with minimal effort in Power BI it is not

Pizza and DAX are my Love Languages

Okay, okay, not really. Beer is though. But if my wife asks, it's "quality time spent with her watching British dramas on PBS Masterpiece." In all seriousness, if you are new to Power BI - then DAX may seem like a foreign language.  Do you want to find out some quick tips or best practices?  Well, let me give you a few tips that were helpful to me when first starting to write DAX formulas. The TAB key is your best friend.   Don’t try to type everything character for character for your formulas.   Just type until you the formula, column, table, etc. shows up directly under the formula bar.   Once, it is there hit the tab button and it automatically promotes it into your formula bar.  Is the formula text too small for you?   It’s always too small for me when doing demonstrations for others.   Place your cursor in the formula bar.   Hold down the control key on your keyboard and use your mouse scroll wheel to make the text larger or smaller. Would you like to move to a new l

Loading Folders as Data Sources in POWER BI

  Power BI can connect to a plethora of different data sources.   Did you know Power BI can also make loading multiple files at one time a cinch?   There is an option from the Get Data icon for “Folder”.   Wait, a folder is not a data source, is it?   Well, all the files in that folder are for sure data sources. So what makes the “Folder” option something you need to know about?   Do you get new files with the same column structure sent to you on a set time frame and then have to go and append them to your existing file?   Have you ever found yourself appending multiple files over and over that have the same column structure?   If you answered yes to any of those last 2 questions, then you are going to fall in love with the “Folder” data source. Simply put, the “Folder” option allows you to bring in all of your files at once into Power BI.   You will then make all of your transforms on one sample file and Power BI will automate the process across all the files of your choosing and