Skip to main content

DAX Queries

For Data Analysts

This guide covers DAX query syntax supported by Olytix Core, including examples for common analytical patterns.

Query Structure

A DAX query consists of optional DEFINE statements followed by an EVALUATE statement:

[DEFINE
MEASURE Table[MeasureName] = <expression>
VAR VariableName = <expression>
]
EVALUATE <table expression>
[ORDER BY <expression> [ASC | DESC]]

EVALUATE Statement

The EVALUATE statement returns a table result. It is required in every DAX query.

Simple Table Query

Return all rows from a cube:

EVALUATE orders

With ORDER BY

Sort results by one or more columns:

EVALUATE orders
ORDER BY orders[order_date] DESC

SUMMARIZECOLUMNS

The primary function for analytical queries. Groups data by dimensions and calculates measures.

Basic Syntax

SUMMARIZECOLUMNS(
<grouping columns>,
[<filter tables>],
"<measure name>", <measure expression>,
...
)

Example: Revenue by Region

EVALUATE
SUMMARIZECOLUMNS(
orders[region],
"Total Revenue", SUM(orders[amount]),
"Order Count", COUNTROWS(orders)
)

REST API Request:

curl -X POST http://localhost:8000/api/v1/dax/execute \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": "EVALUATE SUMMARIZECOLUMNS(orders[region], \"Total Revenue\", SUM(orders[amount]), \"Order Count\", COUNTROWS(orders))"
}'

Response:

{
"columns": [
{"name": "region", "data_type": "String", "is_measure": false},
{"name": "Total Revenue", "data_type": "Double", "is_measure": true},
{"name": "Order Count", "data_type": "Integer", "is_measure": true}
],
"data": [
{"region": "NORTH", "Total Revenue": 125000.00, "Order Count": 1523},
{"region": "SOUTH", "Total Revenue": 98500.00, "Order Count": 1187},
{"region": "EAST", "Total Revenue": 112000.00, "Order Count": 1342},
{"region": "WEST", "Total Revenue": 87500.00, "Order Count": 1056}
],
"row_count": 4,
"execution_time_ms": 52.3
}

Multiple Grouping Columns

EVALUATE
SUMMARIZECOLUMNS(
orders[region],
orders[product_category],
"Revenue", SUM(orders[amount])
)
ORDER BY orders[region], orders[product_category]

With Inline Filters

Apply filters directly in SUMMARIZECOLUMNS:

EVALUATE
SUMMARIZECOLUMNS(
orders[region],
FILTER(ALL(orders[status]), orders[status] = "completed"),
"Completed Revenue", SUM(orders[amount])
)

FILTER Function

Creates a filtered table based on a condition.

Basic Filtering

EVALUATE
FILTER(
orders,
orders[amount] > 1000
)

Multiple Conditions

EVALUATE
FILTER(
orders,
orders[status] = "completed" && orders[amount] >= 500
)

Combined with SUMMARIZECOLUMNS

EVALUATE
SUMMARIZECOLUMNS(
orders[region],
FILTER(orders, orders[amount] > 100),
"High Value Orders", COUNTROWS(orders)
)

CALCULATE Function

Modifies the filter context for measure evaluation.

Apply Additional Filters

EVALUATE
SUMMARIZECOLUMNS(
orders[region],
"All Revenue", SUM(orders[amount]),
"Completed Revenue", CALCULATE(
SUM(orders[amount]),
orders[status] = "completed"
)
)

Remove Filters with ALL

EVALUATE
SUMMARIZECOLUMNS(
orders[region],
"Region Revenue", SUM(orders[amount]),
"Total Revenue", CALCULATE(SUM(orders[amount]), ALL(orders[region]))
)

TOPN Function

Returns the top N rows based on an expression.

Top 10 Customers by Revenue

EVALUATE
TOPN(
10,
SUMMARIZECOLUMNS(
orders[customer_id],
"Revenue", SUM(orders[amount])
),
[Revenue],
DESC
)

Top Products per Category

EVALUATE
TOPN(
5,
SUMMARIZECOLUMNS(
products[product_name],
products[category],
"Sales", SUM(orders[amount])
),
[Sales]
)

ADDCOLUMNS and SELECTCOLUMNS

Add calculated columns to a table.

ADDCOLUMNS

Adds columns while keeping existing ones:

EVALUATE
ADDCOLUMNS(
SUMMARIZECOLUMNS(
orders[region],
"Revenue", SUM(orders[amount])
),
"Revenue Rank", RANKX(ALL(orders[region]), [Revenue])
)

SELECTCOLUMNS

Creates a table with only specified columns:

EVALUATE
SELECTCOLUMNS(
orders,
"Order ID", orders[order_id],
"Customer", orders[customer_name],
"Total", orders[amount]
)

DEFINE Statement

Define calculated measures and variables for use in the query.

Define Measures

DEFINE
MEASURE orders[Total Revenue] = SUM(orders[amount])
MEASURE orders[Avg Order Value] = AVERAGE(orders[amount])
EVALUATE
SUMMARIZECOLUMNS(
orders[region],
"Revenue", [Total Revenue],
"AOV", [Avg Order Value]
)

Define Variables

DEFINE
VAR CurrentYear = 2024
EVALUATE
FILTER(
SUMMARIZECOLUMNS(
orders[region],
"Revenue", SUM(orders[amount])
),
YEAR(orders[order_date]) = CurrentYear
)

Aggregation Functions

FunctionDescriptionExample
SUMSum of valuesSUM(orders[amount])
AVERAGEAverage of valuesAVERAGE(orders[amount])
MINMinimum valueMIN(orders[order_date])
MAXMaximum valueMAX(orders[amount])
COUNTCount of non-blank valuesCOUNT(orders[customer_id])
COUNTROWSCount of rowsCOUNTROWS(orders)
DISTINCTCOUNTCount of distinct valuesDISTINCTCOUNT(orders[customer_id])
COUNTACount of non-empty valuesCOUNTA(orders[notes])
COUNTBLANKCount of blank valuesCOUNTBLANK(orders[discount])

Iterator Functions

Iterator functions (X functions) evaluate an expression row-by-row:

FunctionDescriptionExample
SUMXSum with row contextSUMX(orders, orders[quantity] * orders[unit_price])
AVERAGEXAverage with row contextAVERAGEX(orders, orders[amount] / orders[quantity])
MINXMinimum with expressionMINX(orders, orders[amount] - orders[discount])
MAXXMaximum with expressionMAXX(orders, orders[amount] * 1.1)
COUNTXConditional countCOUNTX(orders, IF(orders[amount] > 100, 1, BLANK()))

Example: Weighted Average

EVALUATE
SUMMARIZECOLUMNS(
products[category],
"Weighted Avg Price", SUMX(products, products[price] * products[units_sold]) / SUM(products[units_sold])
)

Filter Functions

FunctionDescriptionExample
FILTERFilter rows by conditionFILTER(orders, orders[status] = "completed")
ALLRemove all filtersALL(orders)
ALLEXCEPTRemove all filters except specifiedALLEXCEPT(orders, orders[region])
VALUESDistinct valuesVALUES(orders[region])
DISTINCTDistinct column valuesDISTINCT(orders[customer_id])

Logical Functions

FunctionDescriptionExample
IFConditionalIF(orders[amount] > 1000, "High", "Low")
ANDLogical ANDAND(orders[status] = "completed", orders[amount] > 0)
ORLogical OROR(orders[region] = "NORTH", orders[region] = "SOUTH")
NOTLogical NOTNOT(ISBLANK(orders[discount]))
SWITCHMultiple conditionsSWITCH(orders[status], "new", 1, "processing", 2, 0)

Date Functions

FunctionDescriptionExample
YEARExtract yearYEAR(orders[order_date])
MONTHExtract monthMONTH(orders[order_date])
DAYExtract dayDAY(orders[order_date])
TODAYCurrent dateTODAY()
NOWCurrent date/timeNOW()
DATEDIFFDifference between datesDATEDIFF(orders[order_date], TODAY(), DAY)

Time-Based Analysis

EVALUATE
SUMMARIZECOLUMNS(
YEAR(orders[order_date]),
MONTH(orders[order_date]),
"Monthly Revenue", SUM(orders[amount])
)
ORDER BY YEAR(orders[order_date]), MONTH(orders[order_date])

Query Validation

Validate DAX syntax before execution:

curl -X POST http://localhost:8000/api/v1/dax/validate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": "EVALUATE SUMMARIZECOLUMNS(orders[region], \"Revenue\", SUM(orders[amount]))"
}'

Valid Response:

{
"valid": true,
"errors": [],
"warnings": []
}

Invalid Response:

{
"valid": false,
"errors": ["Syntax error: unexpected token at line 1, column 15"],
"warnings": []
}

Error Handling

Common Errors

ErrorCauseSolution
Unknown tableTable/cube not foundVerify cube name in /dax/cubes
Unknown columnColumn not in cubeCheck available dimensions/measures
Syntax errorInvalid DAX syntaxUse /dax/validate to check syntax
Function not supportedUnsupported DAX functionCheck supported functions list

Error Response Format

{
"error": {
"code": "DAX_SYNTAX_ERROR",
"message": "Syntax error at line 1: unexpected token 'EVALUTE'",
"suggestion": "Did you mean 'EVALUATE'?"
}
}

Performance Tips

  1. Use SUMMARIZECOLUMNS instead of SUMMARIZE for better performance
  2. Limit result sets with TOPN or ORDER BY with a limit
  3. Filter early in the query to reduce data volume
  4. Avoid complex iterators when simpler aggregations work
  5. Use pre-aggregations for frequently queried patterns

Next Steps