- Ciao friends,
DAX Formatter is an essential tool to work with DAX.
You simply paste your code here,
and you click Format.
It's very easy,
and you get your DAX code formatted.
It's very important to properly format the DAX code
because, remember, if it's not formatted, it's not DAX.
DAX Formatter has a few options that you can use
to improve the formatting.
First of all, we manage different separators here
and if you have a tool like Power Pivot for Excel
or Visual Studio in a country that is not
using the comma as a list separator,
you can change the list separator here.
And as you see, if I choose the different separator here,
I get an error.
The default, the automatic tries to
automatically recognize what are the separators used,
but in case it doesn't work for your code,
you can explicitly select the separator that you prefer.
In terms of spacing, we have just two options.
You see that this format, the default format,
in this case is a format that keeps a space between
the name of a function
and the beginning of the function, the bracket.
But this is what we use, especially in our articles,
in our books to improve the reading.
In the real world that when you write a code,
you might prefer using this type of formatting
where we don't have a space after the function name.
It's also the way probably that is more natural
when you type a code.
Finally, we have two options here,
long lines and short lines.
What's the difference here?
We can see that if I click on short lines,
I could have, not in this case,
but I could have a slightly different format,
depending on the length of the expression.
I prepared
some code here that I can show in this format.
And just to show you that, you see that there is a line here,
the first line that has different arguments,
but the function itself can still stay on a line.
So if I choose the long lines format,
we see that the format keeps everything in a single line.
So if there are multiple arguments
DAX Formatter tries to keep the line in a single line
and when I choose Short lines,
no matter what, every argument is in a new line
and the code is probably easier to read,
even though it requires more lines.
This is the technique that we use
in our articles and our books.
But once again, probably in the real world,
this is the setting that I will use in, you know,
development environment.
You can also copy, you can save as DOCX,
but basically what we do, we copy this in the clipboard
and we paste the code in Power BI,
Power Pivot, in Visual Studio.
Now, if you use DAX, you probably use the tools
like DAX Studio.
And if you use DAX Studio, DAX Studio actually has
an integration with DAX Formatter,
so you can just click on Format query
and you can format the query.
Now, this query was already formatted
so I show you that if I get the code that I prepared here,
which is not formatted and I paste the code,
I click on Format query, and I get my code formatted.
If you want to change the short and the long lines
in DAX Studio you have the option here,
where I see the DAX Formatter settings
and I can choose the default format style
that could be Long line or Short line.
And you can also choose whether you want the space
after the function name or not.
So if I click this,
and I go back, and I format the code again,
you see that now,
I don't longer have the space here.
The last tool we see
that has an integration with DAX Formatter is
Tabular Editor.
In this case, I have a Tabular Editor attached to
a model where I have many, many measures.
You see here there are many measures here
and I have this measure that is not properly formatted.
So once again, I can just click on the button here
Format DAX, and my code is formatted.
So we have seen how you can use DAX Formatter
to improve your code and remember once again,
if it's not formatted, it's not DAX. Enjoy.