Stop 07 · DAX FundamentalsTopics · 2/10

DAX Syntax and Formatting

Part of the Power BI Roadmap

In short

DAX formulas grow fast. A measure squeezed onto one line is impossible to debug, while the same measure laid out neatly reads almost like a sentence.

A DAX formula has a name, an equals sign and an expression. Tables are written as 'Table Name' when they contain spaces, columns as Table[Column], and measures as [Measure], and line breaks with indents keep long formulas readable.

The jaadu carefully writes a neat multi-line address on a brown parcel while a messy, scribbled parcel sits rejected beside the post office counter.

Think of it like this

Writing an address on a parcel

An address has a fixed order: name, house, street, city, PIN. Write it neatly on separate lines and the courier finds the house. Scrawl it on one line and parcels go astray. DAX has its own address format: 'Table'[Column] for columns and [Measure] for measures, laid out on clear lines.

Watch it happen

Reading one measure, piece by piece

Margin %
0
=
1
DIVIDE (
2
[Gross Profit],
3
[Total Sales]
4
)
5

1/5A measure is a name, an equals sign and an expression.

The big ideas

What you need to know

Tables and columns

Sales[Amount] is a column. Quote table names with spaces or reserved words: 'Date'[Month], 'Sales Lines'[Qty].

Measures without tables

Write measures as [Total Sales]. Writing columns with their table and measures without makes code easy to read.

One argument per line

Put each function argument on its own indented line, with the closing bracket under the function name.

Comments

Use -- or // for a line comment and /* */ for a block. Shift+Enter adds a new line in the formula bar.

See it in code

Walk through the code

dax
1-- Hard to read2Margin % = DIVIDE(SUM(Sales[Amount])-SUM(Sales[Cost]),SUM(Sales[Amount]))3 4-- Same measure, formatted5Margin % =6DIVIDE (7    SUM ( Sales[Amount] ) - SUM ( Sales[Cost] ),  // profit8    SUM ( Sales[Amount] )                         // revenue9)

1/4Correct, but you have to count brackets to see where each argument ends.

The same calculation twice. Tools like DAX Formatter by SQLBI apply this layout for you.

Save yourself the pain

Common mistakes

Oops

Writing measures as Sales[Total Sales], with a table prefix.

Do this instead

Write measures as [Total Sales] and columns as Sales[Amount]. Readers can then tell them apart instantly.

Oops

Forgetting quotes around table names with spaces, like Sales Lines[Qty].

Do this instead

Use single quotes: 'Sales Lines'[Qty].

Your mission

Format three measures

~15 min

Quick check

Which reference is written in the recommended style for a measure?

Go deeper

  1. 1Format a messy measure with DAX Formatter by SQLBI.
  2. 2Use Shift + Enter in the formula bar to add line breaks.
  3. 3Always write the table name before a column and never before a measure.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge