BLANK, ERROR, EVALUATEANDLOG, TOCSV and TOJSON in DAX

The DAX functions in Microsoft's Other category, BLANK, ERROR for your own error messages, EVALUATEANDLOG for debugging measures in Power BI Desktop, TOCSV and TOJSON for seeing a table inside a measure, and EXTERNALMEASURE for composite models, with examples.

Short answer: Microsoft's Other group has six functions. BLANK ( ) returns a blank, the "no value" that visuals hide. ERROR ( "text" ) stops a calculation with your own message, for inputs that must not happen. EVALUATEANDLOG ( value [, label] [, maxrows] ) returns its value unchanged and writes it to a trace event in Power BI Desktop, so you can see what a part of a measure returns. TOCSV and TOJSON turn a table into text, handy to look at a table variable on a card. EXTERNALMEASURE calls a measure of a remote model in a composite model.

Debugging a measure usually means guessing what a VAR holds. TOCSV and EVALUATEANDLOG let you look.

How do BLANK and ERROR work?

-- return nothing instead of 0, so empty rows disappear
Revenue New Products =
IF ( ISBLANK ( [Revenue LY] ), [Revenue], BLANK ( ) )

-- refuse an impossible setting with a clear message
Target Rate =
VAR R = SELECTEDVALUE ( Settings[Rate] )
RETURN
    IF ( R < 0 || R > 1, ERROR ( "Settings[Rate] must be between 0 and 1" ), R )

A visual that hits ERROR fails to load and shows your text in its error details, which is easier to fix than a silent wrong number. In depth: BLANK handling.

How do you see a table variable?

-- on a card while building: what does the filter keep?
Debug Top Customers =
VAR T = TOPN ( 5, VALUES ( Customer[Customer] ), [Revenue] )
RETURN TOCSV ( T )
FunctionReturnsDefaults
TOCSV ( table [, maxrows] [, delimiter] [, headers] )the rows as CSV text10 rows, comma, with headers
TOJSON ( table [, maxrows] )the rows as JSON text10 rows
TOCSV and TOJSON

Turn on word wrap on the card or use a table visual. Remove the debug measure when done.

How does EVALUATEANDLOG help?

Wrap any part of an expression; the result is unchanged, and the value is written to a DAX Evaluation Log event that SQL Server Profiler or the open-source DAX Debug Output tool shows:

Margin % =
VAR Rev = EVALUATEANDLOG ( [Revenue], "rev" )
VAR Cost = EVALUATEANDLOG ( [Cost], "cost" )
RETURN DIVIDE ( Rev - Cost, Rev )

Microsoft notes it is fully functional only in Power BI Desktop and acts as a simple passthrough elsewhere, so a forgotten one does no harm in the service, and that sometimes it is not executed because of optimisations. For a table argument, only the first maxrows rows are logged.

What is EXTERNALMEASURE?

EXTERNALMEASURE ( measure_name, datatype, connection ) invokes a measure from a remote model and returns its result as the given type. It works only in composite models with a connection to a remote model, where Power BI writes it for you; you rarely type it.

What goes wrong?

  • 0 instead of BLANK. Returns rows full of zeros in tables; return BLANK ( ).
  • ERROR for normal cases. One missing value breaks a whole visual; use it only for inputs that are truly wrong.
  • Debug measures left in the model. TOCSV measures confuse report users; put them in a "Debug" folder and delete them.
  • Expecting EVALUATEANDLOG in the service. It logs only in Desktop.

Where can you check this?

Where do you start?

Next time a measure gives a strange number, put its table variable in TOCSV on a card and look. DAX query view is the other way to see tables while you write.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.