Formatting DAX Code
Power BI is Microsoft’s data visualization solution

As computer scientist Donald Knuth said, “Programs are meant to be read by humans and only incidentally for computers to execute.” And this should also apply to your code within Power BI reports.

That is why, when creating columns and measures, it is important to make sure your DAX code is properly formatted. This makes the code readable and easier to debug for you and your colleagues.

For example, take the following measure:

Order Average Timeline = divide(CALCULATE(COUNT('Order'[Id]) + 0, filter(all('Account Calendar'), 'Account Calendar'[Date] <= max('Account Calendar'[Date]))), CALCULATE(COUNT('Account'[Id]) + 0, filter(all('Account Calendar'), 'Account Calendar'[Date] <= max('Account Calendar'[Date]))), blank()) 

A bit messy, right? To format this code:

  1. Copy and paste your DAX code into SQLBI’s DAX formatting tool. Click on Format.

    2020-10-21-format-dax-code-pbi-img01

  2. See the results

     order Average Timeline =
     DIVIDE (
         CALCULATE (
             COUNT ( 'order'[Id] ) + 0,
             FILTER (
                 ALL ( 'Account Calendar' ),
                 'Account Calendar'[Date] <= MAX ( 'Account Calendar'[Date] )
             )
         ),
         CALCULATE (
             COUNT ( 'Account'[Id] ) + 0,
             FILTER (
                 ALL ( 'Account Calendar' ),
                 'Account Calendar'[Date] <= MAX ( 'Account Calendar'[Date] )
             )
         ),
         BLANK ()
     )
    

Happy DAX coding!

Formatting DAX Code
Older post

Display the Last Refresh Date and Time in Power BI

Give your stakeholders the ability to know when your data was last refreshed precisely

Newer post

Go to Column in Power Query

A simple trick to make your Power Query work more efficient

Formatting DAX Code