I recently published a set of rules for DAX code formatting. The following is an example of what I obtain:

CALCULATE (
    SUMX (
        Orders,
        Orders[Amount]
    ),
    FILTER (
        ALL ( Customers ),
        CALCULATE (
            COUNTROWS ( Sales ),
            ALL ( Calendar[Date] )
        ) > 42 + 8 – 25 * ( 3 – 1 )
            + 2 – 1 + 2 – 1
            + CALCULATE (
                  2 + 2 – 2
                  + 2 – 2
              )
            – CALCULATE ( 4 )
    )
)

The goal is to improve code readability and I look forward to implement a code formatting feature in DAX Studio. The DAX Editor already supports the rules described in the article.

I am also considering whether to add a rule specific for ADDCOLUMNS / SUMMARIZE because I would like to see the “pairs” of arguments to define a column in the same row or with a special indentation rule (DAX expression for a column is indented in the line following the column name).

EVALUATE
CALCULATETABLE (
       CALCULATETABLE (
        SUMMARIZE (
            Audience,
            ‘Date'[Year],
            Individuals[Gender],
            Individuals[AgeRange],
            “Num of Rows”, FORMAT (COUNTROWS (Audience), “#,#”),
            “Weighted Mean Age”,
                SUMX (Audience, Audience[Weight] * Audience[Age]) / SUM (Audience[Weight])

        ),
        SUMMARIZE (
            BridgeIndividualsTargets,
            Individuals[ID_Individual]
        ),
        Audience[Weight] > 0
       ),
       Targets[Target] = “Maschi”,
    ‘Date'[Year] = 2010,
    ‘Date'[MonthName] = “January”
)

I would like to get feedback for that – you can use comments here or comments in original article. Thanks!

CALCULATE
Context transition

Evaluates an expression in a context modified by filters.

CALCULATE ( <Expression> [, <Filter> [, <Filter> [, … ] ] ] )

SUMX

Returns the sum of an expression evaluated for each row in a table.

SUMX ( <Table>, <Expression> )

FILTER

Returns a table that has been filtered.

FILTER ( <Table>, <FilterExpression> )

ALL
CALCULATE modifier

Returns all the rows in a table, or all the values in a column, ignoring any filters that might have been applied.

ALL ( [<TableNameOrColumnName>] [, <ColumnName> [, <ColumnName> [, … ] ] ] )

COUNTROWS

Counts the number of rows in a table.

COUNTROWS ( [<Table>] )

ADDCOLUMNS

Returns a table with new columns specified by the DAX expressions.

ADDCOLUMNS ( <Table>, <Name>, <Expression> [, <Name>, <Expression> [, … ] ] )

SUMMARIZE

Creates a summary of the input table grouped by the specified columns.

SUMMARIZE ( <Table> [, <GroupBy_ColumnName> [, [<Name>] [, [<Expression>] [, <GroupBy_ColumnName> [, [<Name>] [, [<Expression>] [, … ] ] ] ] ] ] ] )

CALCULATETABLE
Context transition

Evaluates a table expression in a context modified by filters.

CALCULATETABLE ( <Table> [, <Filter> [, <Filter> [, … ] ] ] )

FORMAT

Converts a value to text in the specified number format.

FORMAT ( <Value>, <Format> [, <LocaleName>] )

SUM

Adds all the numbers in a column.

SUM ( <ColumnName> )