0% found this document useful (0 votes)
36 views13 pages

Complete DAX Functions Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views13 pages

Complete DAX Functions Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

VISHNU VARDHAN

Complete DAX Functions Guide for Power BI


Table of Contents
1. Introduction to DAX
2. Fundamental Concepts

3. DAX Calculations
4. Logical Functions

5. Aggregate Functions
6. Count Functions

7. Mathematical Functions

8. Text Functions

9. Date Time Functions

10. Time Intelligence Functions


11. Advanced DAX Functions
12. Best Practices

Introduction to DAX
DAX (Data Analysis Expressions) is a powerful formula language used in Power BI and Power Pivot in
Excel. It comprises functions, operators, and constants that enable sophisticated data analysis and
business intelligence solutions.

Why Use DAX?


Data Insights: Derive meaningful insights from large datasets

Problem Solving: Address real-world business challenges through measures and calculated columns
Data Maximization: Extract maximum value from your data investments

Fundamental Concepts

Key Components
1. Syntax: The grammar and structure of DAX formulas

All DAX formulas begin with the = sign


Functions follow specific naming conventions and parameter requirements
2. Functions: Pre-built formulas that perform calculations

Mathematical, logical, date/time, and text functions

Each function has specific arguments and return types

3. Context: The environment in which DAX evaluates expressions

Row Context: Current row being evaluated in calculated columns


Filter Context: Applied filters affecting the calculation scope

DAX Operators
Operator Type Symbols Purpose

Arithmetic +, -, *, / Basic mathematical operations

Comparison = , > , < , >= , <= , <> Value comparisons

Logical && , `

Text Concatenation & Combining text strings


 

DAX Calculations

1. Calculated Tables
Tables created using existing model data. Best example: Date tables for time intelligence.

dax

DateTable = CALENDAR(DATE(2020,1,1), DATE(2025,12,31))

2. Calculated Columns
New columns computed for every row in a table. Stored in memory and calculated at refresh time.

dax

Sales Value = 'Sales Data'[Price of Item] * 'Sales Data'[Quantity Ordered]

3. Measures
Dynamic calculations evaluated at query time. Do not occupy model storage space.

dax

Total Sales = SUM('Sales Data'[Sales Value])


Key Difference: Measures change with context (filters/slicers), while calculated columns are static.

Logical Functions

Core Logical Functions


OR Function

dax

OR(<logical1>, <logical2>, ...)

Returns TRUE if at least one argument is TRUE.

AND Function

dax

AND(<logical1>, <logical2>, ...)

Returns TRUE only if both arguments are TRUE.

IF Function

dax

IF(<logical_test>, <value_if_true>, <value_if_false>)

Example: Customer categorization

dax

Customer Category =
IF('Sales Data'[Price of Item] > 500 && 'Sales Data'[Quantity Ordered] >= 20,
"High Value", "Standard")

SWITCH Function

dax

SWITCH(<expression>, <value1>, <result1>, <value2>, <result2>, ..., <else_result>)

Example: Sales categorization

dax
Sales Category =
SWITCH(
TRUE(),
'Sales Data'[Sales Value] <= 3000, "Low Sales",
'Sales Data'[Sales Value] <= 5000, "Medium Sales",
"High Sales"
)

Error Handling Functions


IFERROR: Handle errors gracefully

dax

IFERROR(<expression>, <value_if_error>)

ISBLANK / ISEMPTY: Check for blank or empty values

dax

ISBLANK(<expression>)
ISEMPTY(<expression>)

Aggregate Functions

Basic Aggregations
SUM / SUMX

dax

Total Revenue = SUM('Sales'[Revenue])


Total Sales = SUMX('Sales Data', 'Sales Data'[Price] * 'Sales Data'[Quantity])

AVERAGE / AVERAGEX

dax

Average Price = AVERAGE('Sales Data'[Price of Item])


Average Sales = AVERAGEX('Sales Data', 'Sales Data'[Price] * 'Sales Data'[Quantity])

MIN / MAX

dax
Minimum Price = MIN('Sales Data'[Price of Item])
Maximum Price = MAX('Sales Data'[Price of Item])

Count Functions
COUNT: Counts numerical values

dax

Number Count = COUNT('Sales Data'[Price of Item])

COUNTA: Counts non-blank values (including text)

dax

Non Blank Count = COUNTA('Sales Data'[Customer Name])

DISTINCTCOUNT: Counts unique values

dax

Unique Customers = DISTINCTCOUNT('Sales Data'[Customer ID])

COUNTROWS: Counts table rows

dax

Total Rows = COUNTROWS('Sales Data')

COUNTBLANK: Counts blank cells

dax

Blank Values = COUNTBLANK('Sales Data'[Optional Field])

Mathematical Functions
ABS: Absolute value

dax

Absolute Difference = ABS('Sales Data'[Target] - 'Sales Data'[Actual])


SQRT: Square root

dax

Standard Deviation Component = SQRT('Statistics'[Variance])

POWER: Exponentiation

dax

Compound Growth = POWER(1.05, 'Investment'[Years])

RAND: Random number (0-1)

dax

Random Sample = RAND()

Text Functions
CONCATENATE / & Operator

dax

Full Name = CONCATENATE('Customer'[First Name], " " & 'Customer'[Last Name])

LEFT / RIGHT / MID

dax

First Three Chars = LEFT('Product'[Product Code], 3)


Last Two Chars = RIGHT('Product'[Product Code], 2)
Middle Chars = MID('Product'[Product Code], 4, 2)

LEN: String length

dax

Code Length = LEN('Product'[Product Code])

UPPER / LOWER: Case conversion

dax

Product Name Upper = UPPER('Product'[Product Name])


TRIM: Remove extra spaces

dax

Clean Text = TRIM('Data'[Text Field])

Date Time Functions

Basic Date Functions


TODAY / NOW

dax

Current Date = TODAY()


Current DateTime = NOW()

DATE / TIME

dax

Custom Date = DATE(2024, 3, 15)


Custom Time = TIME(14, 30, 0)

YEAR / MONTH / DAY

dax

Order Year = YEAR('Orders'[Order Date])


Order Month = MONTH('Orders'[Order Date])
Order Day = DAY('Orders'[Order Date])

WEEKDAY

dax

Day of Week = WEEKDAY('Orders'[Order Date], 1) // 1=Sunday, 7=Saturday

Date Calculations
DATEDIFF: Calculate differences

dax

Days Between = DATEDIFF('Orders'[Order Date], 'Orders'[Delivery Date], DAY)


DATEADD: Add/subtract intervals

dax

Target Delivery = DATEADD('Orders'[Order Date], 2, DAY)

EOMONTH: End of month

dax

Month End = EOMONTH('Orders'[Order Date], 0)

Date Table Creation

dax

Date Table =
ADDCOLUMNS(
CALENDAR(DATE(2020,1,1), DATE(2025,12,31)),
"Year", YEAR([Date]),
"Month", MONTH([Date]),
"Month Name", FORMAT([Date], "MMMM"),
"Quarter", "Q" & QUARTER([Date]),
"Weekday", WEEKDAY([Date]),
"Day Name", FORMAT([Date], "DDDD")
)

Time Intelligence Functions

Year-to-Date Calculations
TOTALYTD: Year-to-date totals

dax

YTD Sales = TOTALYTD(SUM('Sales'[Amount]), 'Date'[Date])

TOTALMTD: Month-to-date totals

dax

MTD Sales = TOTALMTD(SUM('Sales'[Amount]), 'Date'[Date])

TOTALQTD: Quarter-to-date totals


dax

QTD Sales = TOTALQTD(SUM('Sales'[Amount]), 'Date'[Date])

Date Range Functions


DATESYTD: Year-to-date date range

dax

YTD Date Range = DATESYTD('Date'[Date])

DATESINPERIOD: Flexible date ranges

dax

Last 30 Days = DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -30, DAY)

DATESBETWEEN: Specific date range

dax

Date Range = DATESBETWEEN('Date'[Date], DATE(2024,1,1), DATE(2024,12,31))

Period Comparison
SAMEPERIODLASTYEAR: Previous year same period

dax

Sales LY = CALCULATE(SUM('Sales'[Amount]), SAMEPERIODLASTYEAR('Date'[Date]))

PARALLELPERIOD: Offset periods

dax

Sales Previous Quarter =


CALCULATE(SUM('Sales'[Amount]), PARALLELPERIOD('Date'[Date], -1, QUARTER))

Advanced DAX Functions

CALCULATE: Context Modification


The most powerful DAX function for changing filter context.
dax

North Region Sales =


CALCULATE(
SUM('Sales'[Amount]),
'Customer'[Region] = "North"
)

FILTER: Table Filtering

dax

High Value Orders =


CALCULATE(
SUM('Sales'[Amount]),
FILTER('Sales', 'Sales'[Amount] > 1000)
)

SUMMARIZE: Data Aggregation

dax

Country Sales Summary =


SUMMARIZE(
'Sales',
'Customer'[Country],
'Customer'[City],
"Total Sales", SUM('Sales'[Amount]),
"Average Sale", AVERAGE('Sales'[Amount])
)

ADDCOLUMNS: Table Extension

dax

Enhanced Sales =
ADDCOLUMNS(
'Sales',
"Sales with Tax", 'Sales'[Amount] * 1.1,
"Discount Amount", 'Sales'[Amount] * 0.1
)

VAR and RETURN: Variable Usage

dax
Complex Calculation =
VAR BasePrice = 'Product'[Price]
VAR DiscountRate =
IF('Product'[Category] = "Electronics", 0.1,
IF('Product'[Category] = "Clothing", 0.15, 0.05))
VAR FinalPrice = BasePrice * (1 - DiscountRate)
RETURN
FinalPrice

RELATED: Cross-table References

dax

Product Category = RELATED('Product'[Category])

FORMAT: Value Formatting

dax

Formatted Percentage = FORMAT('Metrics'[Rate], "Percent")


Formatted Currency = FORMAT('Sales'[Amount], "Currency")

ALL: Remove Filters

dax

Total Sales All Regions =


CALCULATE(
SUM('Sales'[Amount]),
ALL('Customer'[Region])
)

Best Practices

1. Organization Strategy
Create a dedicated "Measures" table for all DAX calculations

Store all measures in one location for easy maintenance


Use consistent naming conventions

2. Performance Optimization
Use measures instead of calculated columns when possible
Leverage variables (VAR) to avoid repeated calculations
Be mindful of filter context in complex calculations

3. Date Table Setup


Always create a proper date table using CALENDAR function
Mark your date table in Power BI settings

Create date hierarchies for better user experience

4. Data Type Considerations


Ensure date columns have proper data types
Set correct locale settings for date interpretation

Handle data type conversions explicitly

5. Error Handling
Use IFERROR for robust calculations

Check for blank values with ISBLANK


Provide meaningful default values

6. Documentation
Use descriptive names for measures and columns

Comment complex DAX formulas


Maintain consistency across similar calculations

Practical Implementation Tips

Setting Up Date Locale


1. Go to File → Options & Settings → Options
2. Navigate to Regional Settings

3. Set appropriate Locale for Import (e.g., English (United States))


4. This ensures correct date interpretation

Creating Measure Tables


1. Select Enter Data to create new table

2. Load empty table


3. Right-click table → New Measure

4. Store all DAX calculations in this centralized location


DateTime Storage Format

DateTime values stored as decimal numbers


Integer part = date, Decimal part = time

Example: 2024-03-04 12:00 PM = 45264.5

Conclusion
DAX is a powerful language that transforms raw data into actionable business insights. By mastering
these functions and following best practices, you can create sophisticated analytical solutions in Power BI
that drive data-driven decision making.

Remember: Start with basic functions, build complexity gradually, and always prioritize performance and
maintainability in your DAX solutions.

This comprehensive guide covers fundamental to advanced DAX concepts for Power BI professionals. For the
latest updates and detailed examples, refer to the official Microsoft DAX documentation.

FOLLOW

https://www.linkedin.com/in/viznuu

You might also like