DEV Community

Cover image for Mastering DAX in Power BI: A Beginner’s Guide
Maurine Nyongesa
Maurine Nyongesa

Posted on • Edited on

Mastering DAX in Power BI: A Beginner’s Guide

Introduction

1. What is DAX?

DAX (Data Analysis Expressions) is the formula language used in Power BI, Excel Power Pivot, and SQL Server Analysis (SSAS). It is designed to handle data manipulation and computations, allowing for powerful data models and analysis.

Example Scenario: Imagine you have a dataset of retail transactions. You want to calculate total sales, profit margin, or year-over-year growth. DAX enables you to write formulas that generate these values dynamically, adapting to filters and user selections in your reports.

It plays a vital role in enhancing the reporting and analysis capabilities of Power BI, enabling users to dive deeper into their data by creating custom insights.

DAX is the primary used for creating calculated columns, calculated measures and managing relationships between data tables.

This guide introduces essential DAX functions and concepts to get you started with creating powerful calculations in Power BI.

2. Essential DAX Concepts and Functions

Calculated Columns vs. Measures

Understanding the difference between calculated columns and measures is crucial when working with DAX:

Calculated Columns: These are computed row-by-row and added to your data model as new columns. They are useful for creating static calculations.

Example: Calculating a Profit column in the Sales table:

 Profit = Sales[Revenue] - Sales[Cost]
Enter fullscreen mode Exit fullscreen mode

Measures: Measures are dynamic calculations that aggregate based on the current filter context, which means they change based on the data you view in your reports. Measures are great for calculations like sums, averages, and ratios.

Example: Calculating total sales as a measure:

 Total Sales = SUM(Sales[Amount])
Enter fullscreen mode Exit fullscreen mode

3. Key DAX Functions Categories with examples

3.1 Aggregate Functions:

SUM: Adds up all the values in a specific column.

Example: This DAX formula calculates the total sales amount from the SalesAmount column of the Sales table.

 Total Sales = SUM(Sales[SalesAmount])
Enter fullscreen mode Exit fullscreen mode

AVERAGE: Returns the average of all values in a column.

Example:

 Average Sales = AVERAGE(Sales[SalesAmount])
Enter fullscreen mode Exit fullscreen mode

This computes the average sales amount for all rows in the Sales table.

COUNT: Counts the number of non-blank cells in a column.

Example:

 Number of products = COUNT(Products[ProductID])
Enter fullscreen mode Exit fullscreen mode

This counts the number of non-empty ProductID entries in the Products table.

SUMX: This performs row-by-row calculations for each record in a table and then returns the total sum of these calculations.

Example:

 Total Revenue = SUMX(Sales, Sales[Quantity] * Sales[UnitPrice])
Enter fullscreen mode Exit fullscreen mode

This formula multiplies Quantity by UnitPrice for each row and sums the result across all rows.

3.2 Filter Funtions:

CALCULATE: Modifies the filter context of an expression, allowing you to customize the filters applied to your calculations.

Example:

 Sales in West = CALCULATE(SUM(Sales[Amount]), Sales[Region] = "West")
Enter fullscreen mode Exit fullscreen mode

This calculates the total sales only for the west region, regardless of any other filters applied.

FILTER: Returns a table containing only rows that satisfy a given condition.

Example:

 Expensive Products = FILTER(Products, Products[Price] > 100)
Enter fullscreen mode Exit fullscreen mode

This filters out products that cost more than 100, returning only those rows.

ALL: Ignores any filters that might be applied to a column or table.

Example:

 Total Sales All Regions = CALCULATE(SUM(Sales[SalesAmount]), ALL(Sales[Region]))
Enter fullscreen mode Exit fullscreen mode

This calculates the total sales without considering any filters on the Region column.

RELATED: Retrieves values from a related tables using relationships.

Example:

 Product Name = RELATED(Products[ProductName])
Enter fullscreen mode Exit fullscreen mode

This pulls the product name from the Products table into the Sales table, assuming a relationship exists between the two tables.

3.3 Time Intelligence Functions:

SAMPLEPERIODLASTYEAR: Compare data from the same period in the previous year.

Example:

 Sales Last Year = CALCULATE(SUM(Sales[SalesAmount]),SAMPLEPERIODLASTYEAR(Sales[Date]))
Enter fullscreen mode Exit fullscreen mode

This formula calculates the total sales for the same period last year.

TOTALYTD: Calculates the year-to-date total measure.

Example:

 YTD Sales = TOTALYTD(SUM(Sales[SalesAmount]), Sales[Date])
Enter fullscreen mode Exit fullscreen mode

This gives the total sales from the beginning of the year to the current date.

DATEADD: Shifts dates in a date column by a specified number of days, months, or years.

Example:

 Sales Previous Month = CALCULATE(SUM(Sales[SalesAmount]),  DATEADD(Sales[Date], -1, MONTH))
Enter fullscreen mode Exit fullscreen mode

This moves the date back by one month and calculates the sales for that period.

3.4 Logical Functions:

IF: Evaluates a condition and returns one values if the condition is TRUE and another if it is FALSE.

Example:

 SalesCategory = IF(SUM(Sales[SalesAmount] > 1000,"High","Low")
Enter fullscreen mode Exit fullscreen mode

This classifies sales into "High" or "Low" categories based on whether the SalesAmount is greater than 1000

AND: Returns TRUE if all conditions are TRUE

Example:

 Big Discount = IF(AND(Sales[Quality] > 50, Sales[Discount] > 10), "Yes","No")
Enter fullscreen mode Exit fullscreen mode

This checks if both conditions (quantity greater than 50 and discount greater than 10 are met, then labels them "Yes" or "No".

OR: Returns TRUE if atleast one conditions is TRUE

Example:

 Special Offer = IF(OR(Sales[Quality] > 100, Sales[Discount] > 20), "Special","Regular")
Enter fullscreen mode Exit fullscreen mode

Thsi checks if either quantity is greater than 200 or the discount is greater than 20.

3.5 Text Functions

CONCATENATE: Joins two or more strings into one.

Example:

 Full Product Name = CONCATENATE(Products[ProductName],"-", Products[Category])
Enter fullscreen mode Exit fullscreen mode

This joins the product name and category with a hyphen in between.

UPPER: Converts text to uppercase.

Example:

 Upper Product Name = UPPER(Products[ProductName])
Enter fullscreen mode Exit fullscreen mode

This converts the product name to uppercase.

LEFT/RIGHT: Extracts a specified number of characters from the left or right side of a text string.

Example:

Left Part of Name = LEFT(Products[ProductName], 5)
Enter fullscreen mode Exit fullscreen mode

This returns the first 5 characters from the left of the product name.

5.6 Mathematical Functions:

DIVIDE: Performs division and handles division by zero.

EXAMPLE:

 Price Per Unit = DIVIDE(Sales[Amount], Sales[Quantity])
Enter fullscreen mode Exit fullscreen mode

This calculates the price per unit, handling cases where the quantity might be zero.

MOD: Returns the remainder of a division operation.

Example:

 Remainder = MOD(Products[Quantity],2)
Enter fullscreen mode Exit fullscreen mode

This returns the remainder when dividing the quantity by 2 (Useful for checking odd or even numbers)

Conclusion:

DAX provides a powerful set of functions that allow you to create dynamic, context-aware calculations in Power BI. By using aggregate, filter, time intelligence, and logical functions, you can craft precise and flexible reports and dashboards. It's essential to understand how these functions interact with your data model, especially in terms of context and relationships, to build complex and insightful calculations.

Top comments (0)