DAX is simple, but it is not easy. I cannot remember how many times I read this sentence on social media, attributed to me. For good reason: I have repeated this sentence at almost every classroom course I delivered over the years, at conferences, user group meetings… it is my mantra.
The sentence itself is not mine. It came as the answer to a question I routinely ask my students at the end of a training: “What do you think of DAX now?”. The course was in Dublin, around 10 years ago, and a student answered that. I stole his sentence, and it became a mantra for both my DAX students and myself. This sentence is a powerful synthesis of DAX.
Anyway, the question is, “Why is DAX not easy, if it is simple?”. In this article I want to expand on the topic. I do not pretend to be thorough, or to provide an authoritative answer to the question. I just want to share my experience teaching DAX to my students, and to highlight the reasons why – to many – DAX is some sort of black magic, mastered by a few wizards and passed through their voice down to a small number of enlightened apprentices.
The truth is quite different: anybody can learn DAX; at SQLBI we are no wizards. Or maybe we are… In which case I want to share our secrets, so that any of my readers can join the secret wizardry of DAX developers!
Reason #1: DAX is a functional language
Functional languages are extremely elegant, from a mathematical point of view. Yet, they are also very counter-intuitive. As humans, when we describe an algorithm, we follow steps. We do not provide a function that computes the result: we provide the steps to reach the result.
Imagine you need to assemble a new piece of furniture that you just brought home. Let us say it is a chair. You open the instructions and find this:
CHAIR = ADD ( USESCREW ( Back ), REPEAT ( 4, ADD ( USESCREW ( Leg ), Seat ) ) )
That would be depressing, wouldn’t it? If you translate it from functional to iterative, it says: add a leg to the seat using a screw, 4 times. Once that is done, use some other screws to add the back to the seat. Et voilà, you built a chair!
Although I hope nobody would ever give me functional instructions in a furniture kit, with DAX you need to learn to reason in a functional way. Gaining this skill takes time and effort.
Functional languages are nothing new. They were around back in the time of LISP, in the prehistory of IT – yes, I was around at the time. They never gained a lot of popularity, mainly because they are hard to learn. With that said, an algorithm expressed in a functional language can be optimized much more easily than the same algorithm expressed in an iterative language – meaning step-by-step instructions, as you would expect for your new furniture kit. Being functional is part of the price you need to pay to have a fast language.
If you struggle with the functional nature of DAX, how can you improve your experience? Practice, practice, and practice again. At some point, your brain starts to read functional languages. Practice is the only thing that helps here. “Thinking functional” is hard and counterintuitive; practice is your only weapon. Which leads us to reason #2.
Reason #2: Practice makes perfect
DAX is our job: we spend all day writing and teaching DAX. All day, every day. This is how you become an expert: by practicing. With DAX, that means practicing a lot!
When it comes to Power BI, most users do not spend their entire time preparing reports day in, day out. This makes perfect sense: a report is meant to produce numbers, and users are expected to reason off of those numbers, not just produce them. Any regular user would need DAX when preparing a report, but this is a tiny fraction of that person’s daily job. So what happens is that you have a few days to prepare a report, and then weeks, maybe months go by during which you do not write any DAX. Until you need to update the report. Guess what? You struggled to learn certain details when you first authored the report, and after weeks without practice you discover that you forgot everything. Starting over is frustrating. You eventually succeed with the next step, and then you go through another round of months without writing DAX. And the cycle goes on.
Is there a solution to this? Yes. Incorporate DAX in your daily job. This means that if you work in a team comprised of many report builders, maybe you can become the DAX expert so that you spend more time writing DAX and less time analyzing numbers. If you cannot work all day long on DAX, at least try to allocate some time every week to work on your craft. Do not let months go by without writing a single formula or understanding a new concept.
The same could be said of any skill. Is DAX any different from other languages? Yes and no. After a few months not practicing SQL, you would have no problem writing a simple SELECT statement. But changing a complex query with five joins and two correlated subqueries is not a trivial task if you have lost your confidence with SQL. DAX is not very different, though details are more important and less evident. DAX is less verbose than SQL, after all.
Reason #3: There are few important concepts
The entire DAX language is based on just a few concepts:
- evaluation contexts
- context transition
- expanded tables
I have not been able to find a fifth important topic. If you master these four, you are a DAX guru. If you stopped at the first three, you are a DAX ninja. With the first two, you are already a good expert. The thing is: you need to master them, not only have some basic knowledge of what they are. Moreover, these are foundational concepts: they have nothing to do with specific functions.
So, how do you become a DAX guru?
Learning DAX does not mean learning what all the functions do in every detail. You can be a guru without knowing the meaning of the third argument of RANKX. For that kind of detailed knowledge, you can read articles, dax.guide, and many other sources of information at any time.
The main foundational topics are different. You need to learn and understand the foundations really well. You need to make them yours. Profoundly. By practicing and by authoring a lot of code; by understanding exactly what happens under the hood. Do not stop working on a formula just because it works… You need to have a profound understanding of how it works. This means mastering the foundations of DAX.
How can you learn these foundational topics? Be curious! Do not write a formula with the sole goal of producing a number. Be curious to understand exactly how the formula is evaluated and whether it is really the best way to compute that number. If your code generates an error, do not try to fix it by adding a CALCULATE here and there until it works. Yes, I know: we have all been there. But you need to go further: stop, read the code, understand exactly what is wrong with it. Only when you know the problem, is it time to fix it.
Reason #4: DAX is strongly logical
This actually comes as a consequence to the previous points: DAX is a language based on mathematics, built on a few foundational topics. If DAX were a new mathematical theory, this would be perfect. Unfortunately, DAX is not a mathematical theory: it is the language you use to build reports. Still, it is based on the same concepts as a mathematical theory is. Now, if you are a very logical type of person, then you will probably love DAX. By very logical, I mean you like to spend hours on enigmatography and puzzles. If on the other hand that is not your cup of tea, then DAX will need some getting used to. Read on!
Luckily, there is an easy way to avoid this problem: think about DAX as if it were a game. When you start, do not use it to solve real problems. Instead, start with simple games. Like: “Hey, no matter what, I want to write a single expression that computes the average age of my customers at the time they bought their first pair of shoes from my store”. You are likely to spend hours, maybe days trying to solve this. And yet that will be the most fruitful time ever invested on your path to learning DAX. You will have forced yourself to learn the foundations, in a brave attempt to strive for perfection.
If your mind goes straight to: “I need this number, does not matter if the formula is elegant or fast, I just want it computed”, then… well, you could end up wasting a lot of time. Attempting to learn DAX by just solving practical problems is not the way to do it: you need theory. Learning theory from practice is a much harder path to follow. The good thing is that – if you learn DAX as a game – it will be a lot of fun. Trust us, learning DAX is fun – if you’re doing it right!
Reason #5: The devil is in the details
Details… details everywhere. Humans do not love small details; we want to understand the big picture, leaving details for later. But DAX can be quite unforgiving if you forget a small detail in a formula. You forgot that the row context does not propagate through relationships? You get an inaccurate result. DAX will turn its nose up and think that you should have known better, my friend. You do not remember that a table filter in CALCULATE works on the expanded table? Guess what? The only thing DAX uses to warn you of the problem is an inaccurate result. There is nothing surprising about this behavior: DAX computes what you ask it to compute. If you ask the wrong question, you obtain the wrong answer. Plain and simple.
How come you asked the wrong question? This, again, goes back to the foundational theory of DAX: you need to master the basic theory. If you are serious about becoming a DAX guru, you need to not only understand it, but master it. You will master the theory once you have practiced with tons of formulas and you understand exactly how the pieces are bound together in a formula. Once you have tried several variations of the same code, with the sole purpose of understanding exactly how a small change affects the results. Once you have learned all the details. After a lot of practice you reach such a level of proficiency, that writing the formula comes naturally. Remember, you only set the bar at this level once you are prepared to graduate from DAX expert to DAX ninja, or even DAX guru.
When you have reached this point, DAX has no secrets. I know I am repeating myself, but the thing is: if your formula produces a wrong result, then it is likely that you forgot one detail. Do not fix the formula until you understand what the wrong number is. I know it does not look useful to be able to describe what a wrong number represents. In fact, it is: it is extremely useful. Understanding what the wrong number is forces you to discover exactly where the problem is. If the result of your formula is wrong, it means your brain still does not master the basic concepts. Stop, learn the basics again, and then proceed.
Reason #6: You need a good editor
If you think that writing DAX in a text box is a depressing experience, and you are wondering why we do not have a fully featured and powerful editor… well, I totally feel your pain. Building a complex model with tens of DAX measures is like writing an 800-page book on your cell phone, where you can see one paragraph at a time. Whatever the reason, authoring DAX in a text box is probably the only thing that remained constant since the first DAX edition, in Power Pivot for Excel.
Although there was a strong need for a good editor for DAX, Microsoft has not provided one. Besides, users have not made enough pressure to obtain a better editor. Indeed, you can find several ideas on ideas.powerbi.com, but they have not received enough votes to force the Microsoft engineers to consider them. This is such a pity. To me, the absence of a good editor is one of the main reasons learning DAX looks hard.
I have seen many students start to be enlightened when, during the training, they are given the option of using DAX Studio instead of Power BI to write DAX. By writing simple queries, by looking immediately at the results, by formatting the code with a keystroke, their brain is suddenly freed from many small user interface constraints and it has the option of quickly trying different variations of the same expression. As a teacher, I have learned to recognize when a student’s eyes start to light up as they are suddenly making sense of what they are doing. It often happens exactly at that point.
If you want to learn DAX, start with DAX Studio. Download it; learn it; use it to try different pieces of DAX code. Trust me, it is time very well spent. While it is true that Microsoft is lacking a good editor for DAX, it is also true that they are a very open company, humble enough to let others fill the gaps when they know there are some. The integration of Power BI with DAX Studio and Tabular Editor is only going to get better with time. Therefore, I am confident this reason – though compelling – is going to fade away pretty soon.
Reason #7: Trial and error simply does not work
If you have read everything so far, this last point should be no surprise. The way many people learn a new language is by googling examples, trying them on their model, correcting the errors and finetuning things until it works. There is nothing wrong with this approach: it works with most programming languages. To be honest, it works with most topics. That is how we learned to speak! No theory, just a lot of examples and a lot of mistakes.
DAX is more like mathematics, statistics, physics. You cannot learn DAX just by trial and error. You need to learn it the same way you learn a mathematical topic: you study it. You proceed to the next step only when the previous one is understood and solid.
I know, most of the users learn DAX because they want to solve a specific problem. They are not in it to learn a new programming language; they just want the next report done by yesterday. Unfortunately, this approach is not very productive with DAX, because of the additional time required to fix the calculation when the result is not the one you expected.
If you want to learn DAX, take the time required to learn it the right way. Do not try to solve complex problems before you have the knowledge required to understand exactly what you are doing. Once you master the basic concepts, you will be able to solve more complex problems.
If you need the moving average over the last 30 working days, you cannot search on the web for the formula, copy & paste it and hope for the best. I mean, you can, if you are proficient enough to fix any issue you might encounter. First, gather the knowledge about the fundamentals, then copy & paste will work just fine.
Time to draw some conclusions about why DAX is simple, but not easy. To me, it is all a matter of how you approach the learning process. DAX is simple, meaning that the fundamentals of the language are not complex. They are simple, and there are few of them. But you need to train yourself again and again, until you really master and understand them. If you succeed, then DAX is also easy. If you do not, or you just run too fast towards your objective, then learning DAX becomes very hard.
As I said in the introduction: at SQLBI we are not wizards. We have been practicing for years. We wanted to understand. We are not alone at all. A whole generation of great DAX developers is rising. We can see this in the increasing quality of blog posts of authors and developers who write elegant and efficient DAX code. I bet all these developers spent a lot of time on learning the fundamentals.
If I were to give a single piece of advice to any newbie in DAX, this would be it: practice the fundamentals. It is the same advice any seasoned coach would give an eager young athlete.
Do not give up when everything still seems fuzzy: go back to the fundamentals, study them once more. Good results will follow.
We know it worked for us. It will work for you too.
Returns the rank of an expression evaluated in the current context in the list of values for the expression evaluated for each row in the specified table.
RANKX ( <Table>, <Expression> [, <Value>] [, <Order>] [, <Ties>] )
Evaluates an expression in a context modified by filters.
CALCULATE ( <Expression> [, <Filter> [, <Filter> [, … ] ] ] )