DAX Calculated Fields Guide
DAX Calculated Fields Guide
WITH DAX
MEET
DAX
Data Analysis Expressions, commonly known as DAX, is the formula language that drives
Power BI. With DAX, you can:
• Add calculated columns and measures to your model, using intuitive syntax
• Go beyond the capabilities of traditional “grid-style” formulas, with powerful and flexible
functions
built specifically to work with relational data models
PRO TIP:
Calculated columns are typically used for filtering data, rather than creating numerical values
CALCULATED COLUMNS
(EXAMPLES)
In this case we’ve added a calculated column named
“Parent”, which equals “Yes” if the [TotalChildren] field
is greater than 0, and “No” otherwise (just like Excel!)
• Since calculated columns understand row context, a
new value is calculated in each row based on the
value in the [TotalChildren] column
• This is a valid use of calculated columns; it creates a
new row “property” that we can now use to filter or
segment any related data within the model
• Unlike calculated columns, measure values aren’t visible within HEY THIS IS IMPORTANT!
As a rule of thumb, use measures
tables; they can only be “seen” within a visualization like a (vs. calculated columns) when a
chart or matrix (similar to a calculated field in an Excel pivot) single row can’t give you the
answer (in other words, when you
• Measures are evaluated based on filter context, which means need to aggregate)
they recalculate when the fields or filters around them change
(like when new row or column labels are pulled into a matrix or
when new filters are applied to a report)
PRO TIP:
Use measures to create numerical, calculated values that can be analyzed in the “values” field of a report visual
RECAP: CALCULATED COLUMNS VS.
MEASURES
CALCULATED COLUMNS
MEASURES
• Values are calculated based on information from each Values are calculated based on information from any
• filters in the report (has filter context)
row of a table (has row context)
Does not create new data in the tables themselves
• Appends static values to each row in a table and (doesn’t increase file size)
•
stores them in the model (which increases file size) Recalculate in response to any change to filters within
the report
• Recalculate on data source refresh or when changes
• Almost always used within the values field of a visual
are made to component columns
• Primarily used as rows, columns, slicers or filters
•
*In older versions of Power BI, the “New Measure” and “New Column” options were found in the Modeling tab of the Data view
QUICK MEASURES
PRO TIP:
Just say “NO” to quick measures
(you’re better than that)
IMPLICIT VS. EXPLICIT
MEASURES
Implicit measures are created when you drag raw numerical fields
(like “OrderQuantity”) into the values pane of a visual and manually
select the aggregation mode (Sum, Average, Min/Max, etc)
Remember that measures are evaluated based on filter context, which means that they
recalculate whenever the fields or filters around them change
For this particular value in the matrix, the Total Orders measure is calculated based
on the following filter context: Products[ProductName] = “Touring Tire Tube”
• This allows the measure to return the total order quantity for each product specifically (or whatever
the row and column labels dictate – years, countries, product categories, customer names, etc)
This Total is not calculated by summing the values above; it evaluates as its own
measure, with no filter context (since we aren’t calculating orders for a specific
product)
This is a page-level
filter, which impact
ALL visuals on the
report page
Product Table
Accessories
Product[CategoryName] = “Accessories”
= 1,115
Count of rows in the AW_Returns_Data
Product Table table, filtered down to only rows
11 where the product category is
Accessories “Accessories”
AW_Returns_Data
*
* Accessories
AW_Sales_Data
Accessories
COMMON DAXFUNCTIONS
DAX SYNTAX
MEASURE NAME
Referenced Referenced
• Note: Measures are always surrounded
in brackets (i.e. [Total Quantity]) when TABLE COLUMN NAME
referenced in formulas, so spaces are OK NAME
& Concatenates two values to produce one text string [City] & “ “ & [State]
&& Create an AND condition between two logical expressions ([State]=“MA”) && ([Quantity]>10)
|| (double pipe) Create an OR condition between two logical expressions ([State]=“MA”) || ([State]=“CT”)
IN Creates a logical OR condition based on a given list (using curly brackets) ‘Store Lookup’[State] IN { “MA”, “CT”, “NY” }
*Head to [Link] for more information about DAX syntax, operators, troubleshooting, etc
COMMON FUNCTION CATEGORIES
Common Examples: Common Examples: Common Examples: Common Examples: Common Examples:
• SUM • IF • CONCATENATE • CALCULATE • DATEDIFF
• AVERAGE • IFERROR • FORMAT • FILTER • YEARFRAC
• MAX/MIN • AND • LEFT/MID/RIGHT • ALL • YEAR/MONTH/DAY
• DIVIDE • OR • UPPER/LOWER • ALLEXCEPT • HOUR/MINUTE/SECOND
• COUNT/COUNTA • NOT • PROPER • RELATED • TODAY/NOW
• COUNTROWS • SWITCH • LEN • RELATEDTABLE • WEEKDAY/WEEKNUM
• DISTINCTCOUNT • TRUE • SEARCH/FIND • DISTINCT
• FALSE • REPLACE • VALUES Time Intelligence Functions:
Iterator Functions: • REPT • EARLIER/EARLIEST • DATESYTD
• SUBSTITUTE • HASONEVALUE • DATESQTD
• SUMX
• TRIM • HASONEFILTER • DATESMTD
• AVERAGEX
• UNICHAR • ISFILTERED • DATEADD
• MAXX/MINX
• USERELATIONSHIP • DATESINPERIOD
• RANKX
• COUNTX
*Note: This is NOT a comprehensive list (does not include trigonometry functions, parent/child functions, information functions, or other less common functions)
BASIC DATE & TIME
FUNCTIONS
DAY/ Returns the day of the month (1-31), month
=DAY/MONTH/
MONTH/ of the year (1-12), or year of a given date
YEAR() YEAR(Date)
HOUR/MINUTE/ Returns the hour (0-23), minute (0-59), or
second (0-59) of a given datetime value =HOUR/MINUTE/SECOND(Datetime)
SECOND()
)
CONCATENATE() Joins two text strings into one =CONCATENATE(Text1, Text2)
RELATED() Returns related values in each row of a table based on relationships with other tables
=RELATED(ColumnName)
HEY THIS IS IMPORTANT!
RELATED works almost exactly like a VLOOKUP function – it uses
The column that contains the the relationship between tables (defined by primary and foreign
values you want to retrieve keys) to pull values from one table into a new column of another
Since this function requires row context, it can only be used as a
Examples:
• Product_Lookup[ProductName] calculated column or as part of an iterator function that cycles
• Territory_Lookup[Country] through all rows in a table (FILTER, SUMX, MAXX, etc)
PRO TIP:
Avoid using RELATED to create redundant calculated columns unless you absolutely need them, since those
extra columns increase file size; instead, use RELATED within a measure like FILTER or SUMX
BASIC MATH & STATS FUNCTIONS
Name of an existing measure, or a DAX List of simple Boolean (True/False) filter expressions
formula for a valid measure (note: these require simple, fixed values; you cannot
Examples:
create filters based on measures)
• [Total Orders] Examples:
• SUM(Returns_Data[ReturnQuantity]) • Territory_Lookup[Country] = “USA”
• Calendar[Year] > 1998
PRO TIP:
CALCULATE works just like SUMIF or COUNTIF in Excel, except it can evaluate measures based on ANY
sort of calculation (not just a sum, count, etc); it may help to think of it like “CALCULATEIF”
CALCULATE
(EXAMPLE)
Bikes
Products[CategoryName] = “Accessories” 11
table, filtered down to only rows
where the product category is “Bikes” = 342
Product Table AW_Returns_Data
*
Accessories * Bikes
AW_Sales_Data
Bikes
ALL
ALL() Returns all rows in a table, or all values in a column, ignoring any filters that have been applied
PRO TIP:
Instead of adding filter context, ALL removes it. This is often used when you need unfiltered values that
won’t react to changes in filter context (i.e. % of Total, where the denominator needs to remain fixed)
FILTE
R
FILTER() Returns a table that represents a subset of another table or expression
PRO TIP:
Since FILTER iterates through each row in a table, it can be slow and processor-intensive; don’t use FILTER
if a CALCULATE function will accomplish the same thing
ITERATOR (“X”) FUNCTIONS
Iterator (or “X”) functions allow you to loop through the same calculation or expression on
each row of a table, and then apply some sort of aggregation to the results (SUM, MAX, etc)
=SUMX(Table, Expression)
PRO TIP:
Imagine the function adding a temporary new column to the table, calculating the value in each row
(based on the expression) and then applying the aggregation to that new column (like SUMPRODUCT)
*In this example we’re looking at SUMX, but other “X” functions follow a similar syntax
TIME INTELLIGENCE FORMULAS
Time Intelligence functions allow you to easily calculate common time comparisons:
PRO TIP:
To calculate a moving average, use the running total calculation above and divide by the number of intervals
BEST PRACTICES: CALCULATED COLUMNS &
MEASURES
Don’t use a calculated column when a measure will do the trick
• Only use calculated columns to “stamp” static, fixed values to each row in a table
• Use measures when aggregation is necessary, or to create dynamic values in a report
Write measures for even the simplest calculations (i.e. Sum of Sales)
• Once you create a measure it can be used anywhere in the report and as an input to other,
more complex calculations (no implicit measures!)