Project

Mastering DAX in Power BI

A comprehensive guide to understanding and mastering the use of Data Analysis Expressions (DAX) in Power BI.

Empty image or helper icon

Mastering DAX in Power BI

Description

This project aims at helping beginners understand the basics of DAX and gradually becoming expert Power BI users through practical tasks and examples. The project starts with introduction to DAX, its importance in Power BI and progresses towards application through table functions and eventually mastering more complex DAX patterns. The goal of this project is to simplify the learning process by dividing the course into 10 specific, atomic topics that build upon each other making it easier to grasp and implement.

Sure, I will provide an introduction to Power BI and DAX and guide you through setting it up.

Let me explain first: Power BI is a powerful business analytics service provided by Microsoft. It provides interactive visualizations and business intelligence capabilities with an interface that is easy to understand. DAX (Data Analysis Expressions) is a library of functions and operators that can be combined to build formulas and expressions in Power BI.

Power BI Installation

  1. Visit the official Power BI website.
  2. Click on "Start free."
  3. It will open a new web page, click on "Download free."
  4. After the download is complete, open the downloaded file and follow the prompts to install Power BI Desktop.

Introduction and Setup of DAX in Power BI

DAX helps to create new information from data already in our model. To create DAX formulas, one must use the Power BI Desktop. In Power BI Desktop, DAX formulas are created in the formula bar.

Follow these steps to set up DAX:

Firstly, you should have a dataset available in Power BI. To upload a dataset:

  1. Choose "Home" then "Get Data" located at the top left-hand corner of the Power BI application.

  2. From the Get Data dropdown, choose the type of file you are uploading (CSV, Excel, etc.) then choose the file.

Now, let's create a new DAX formula:

  1. From the Fields tab on the right-hand side of the Power BI application, you should select a dataset you uploaded. The dataset will expand displaying the various columns in the dataset.

  2. Choose a column from your dataset. Then, click on the "New quick measure" from the dropdown.

  3. The New quick measure window will be displayed. Here you are provided with the option to create a new DAX formula.

For example, let's create a simple DAX formula to calculate a sum:

SumOfSales = SUM ([Sales])

This DAX function SUM will add all the numbers in the [Sales] column for each row in the dataset.

Conclusion

This setup is the base for using DAX in Power BI, and with this, you can further explore more complex DAX functions, express different business rules, perform data analysis and more. Note that DAX functions are designed to work with tables and columns, not individual values. So, the way we shape and organize our data tables will impact the functionality of a DAX formula.

Every situation is unique and there are always different ways to accomplish a task. However, with DAX, it often takes time and practice to realize the most efficient method of accomplishing a goal. This guide is a good beginning to get you started mastering DAX in Power BI.

Writing basic DAX expressions in Power BI

To create basic DAX (Data Analysis Expressions) for Power BI, you need to understand how to write and implement basic expressions. This document will walk you through the practical implementation, assuming you already have Power BI set up and are familiar with its basics.

Section I: Writing Your First DAX Expression

Let's write our first DAX expression: a column that calculates total sales.

  1. In Power BI desktop, select the table in Fields where you want the expression.

  2. Click on the 'Modeling' tab and select 'New column'.

  3. In the formula bar, you would see the title of the column (entitled 'Column ='). Rename the column to 'Total Sales ='.

  4. Now write the formula: SUM('Table'[Sales])

    The SUM function calculates the sum of the column 'Sales' from 'Table'.

  5. Press Enter. The result will be a new column 'Total Sales' which shows the sum of all sales.

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

Section II: Using Functions in DAX

DAX includes a variety of functions you can use in your expression, such as:

  • Mathematical functions: COUNT, SUM, MAX, MIN, AVERAGE etc.
  • Text functions: CONCATENATE, REPLACE, UPPER, LOWER etc.
  • Date and Time functions: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND etc.

Here's an example using AVERAGE function:

Create a column to show the average sales.

  1. Click on 'New column' under the 'Modeling' tab.

  2. Name the column 'Average Sales =' in the formula bar.

  3. Now write the formula: AVERAGE('Table'[Sales])

    The AVERAGE function calculates the average of the column 'Sales' from 'Table'.

     Average Sales = AVERAGE('Table'[Sales])
    

Section III: Creating Calculated Fields

Calculated fields are measures created using DAX expressions, and they can be used in other areas of Power BI, like in visualizations.

  1. In the 'Modeling' tab, click on 'New Measure'.

  2. Name the measure 'Total Sales ='

  3. Now write the formula: SUM('Table'[Sales])

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

These are just the basics of DAX. DAX offers powerful capabilities for more complex calculations and operations to analyze your data in Power BI.

Conclusion

In this document, you understood and created basic DAX expressions. You learned how to create a new column, how to use functions in DAX, and how to create calculated Fields. DAX offers a rich set of functions and operators that help you create complex formulas to analyze your data in Power BI. With these principles in mind, you are ready to proceed with more complex DAX expressions.

Understanding DAX Syntax and Operators in Power BI

This guide is aimed at providing practical insights into understanding the DAX (Data Analysis Expressions) syntax and operators used in Power BI. As requested, the guide assumes knowledge of Power BI and basic DAX expressions.

Outline

  • Constants
  • Identifiers
  • Operators
  • Functions

**Please note that the expressions and formulas provided are examples and may require adjustment to suit your particular use case.

Constants

Constants are literal values used in calculations. They can be integer, decimal, string, or date values.

Examples

// Integer
123

// Decimal
123.45

// String
"This is a string"

// Date
DATE (2000, 12, 31)

Identifiers

In DAX, identifiers are names used to define columns, tables, or other objects. Identifiers can follow general rules: they must start with a letter, an underscore, or a backslash. After the first character, you can use any combination of letters, numbers. If an identifier contains spaces or special characters, you should enclose the identifier in single or double quotes.

Examples

// Identifiers
ProductName
"Product Name"
"Product_Name"

Operators

DAX uses operators to compare, combine, or modify values.

Arithmetic Operators

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Exponential (^)

Examples

// Addition
Total Sales = [Quantity] + [Price]

// Subtraction
Remaining Stock = [Total Stock] - [Sold]

// Multiplication
Total Price = [Quantity] * [Price]

// Division
Average Price = [Total Sales] / [Quantity]

// Exponential
Total Power = [Voltage] ^ [Current]

Comparison Operators

  • Equal to (==)
  • Not equal to (<>)
  • Greater than (>)
  • Lesser than (<)
  • Greater than or equal to (>=)
  • Lesser than or equal to (<=)

Examples

// Equal to
IsPromoItem = [Item Price] == [Promo Price]

// Not equal to
IsPromoItem = [Item Price] <> [Promo Price]

// Greater than
IsHighValue = [Item Price] > 1000

// Lesser than
IsLowValue = [Item Price] < 500

// Greater than or equal to
IsViable = [Revenue] >= [Cost]

// Lesser than or equal to
IsLossMaking = [Revenue] <= [Cost]

Text and Other Operators

  • Ampersand (&) for text concatenation
  • IN operator

Examples

// Ampersand (&)
ItemDesc = [ItemName] & " " & [ItemCategory]

// IN operator
TopSellers = [ItemName] IN {"Item1", "Item2", "Item3"}

Logical Operators

  • AND
  • OR
  • NOT

Examples

// AND
IsHighProfit = ([Profit] > 5000) AND ([Sales] > 10000)

// OR
IsPromo = ([Item] == "Promo item") OR ([Discount] > 0)

// NOT
IsFullPrice = NOT([Discount] > 0)

Functions

DAX has a wide suite of functions that perform various types of calculations.

// SUM function
Total Sales = SUM([Sales])

// AVERAGE function
Avg Sales = AVERAGE([Sales])

// MIN function
Min Sales = MIN([Sales])

// MAX function
Max Sales = MAX([Sales])

// COUNT function
Count Sales = COUNT([Sales])

This guide provided a brief yet practical understanding of DAX syntax and operators used in Power BI. Armed with this knowledge, one can start creating more complex and nuanced data models in Power BI.

Understanding Calculations: Calculated Columns & Measures in Power BI

One of the fundamental concepts in Power BI that you need to master in your journey to mastering Data Analysis Expressions (DAX) is the understanding of Calculated Columns and Measures. These are specific functions that perform user-defined computations. While they may seem similar, both have their unique use cases that determine when and how to use either.

Calculated Columns

A calculated column is a column that is added to an existing table in the Power BI data model. The values in this column are computed by a DAX formula that uses the row context to fetch data.

Here is a practical example. Let's say you have a simple Sales table with the following columns: Product, Quantity, and Price.

Product | Quantity | Price
--------|----------|------
Product A | 10 | $5
Product B | 20 | $3
Product C | 30 | $2

You want to add a calculated column to compute the total sales (Quantity * Price) for each row. Here is how you would do this.

  1. Click on the table "Sales".
  2. Select the "Modeling" tab from the top ribbon.
  3. Click on the "New Column" button.
  4. In the formula bar, enter the following expression and press the Enter key.
Total Sales = Sales[Quantity] * Sales[Price]

This will add a new column Total Sales to the "Sales" table with the computed total sales value for each row.

Product | Quantity | Price | Total Sales
--------|----------|-------|------------
Product A | 10 | $5 | $50
Product B | 20 | $3 | $60
Product C | 30 | $2 | $60

Measures

While calculated columns are computed at the row level, measures are computed at the level of the visual (chart, table, etc.) or at the level of each cell in table-like visuals. Measures use the filter context from the visual to fetch relevant data and perform the computation.

Using the same Sales table let's create a measure to calculate total sales.

  1. Click on the table "Sales".
  2. Select the "Modeling" tab from the top ribbon.
  3. Click on the "New Measure" button.
  4. In the formula bar, enter the following expression and press Enter.
Total Sales = SUMX(Sales, Sales[Quantity] * Sales[Price])

This creates a new measure Total Sales that computes the total sales for every selection in the visual.

The major difference between Calculated columns and Measures is in the way they handle context. Calculated Columns operate under the Row Context, and Measures under the Filter Context.

Remember, failing to apply the correct context can lead to incorrect results.

These concepts, once mastered, empower you to write more complex and advanced DAX expressions to effectively analyze and visually represent your data in Power BI.

Mastering Basic DAX Functions: SUM, COUNT, AVERAGE, MIN, MAX

In this guide, we will dive into some of the basic DAX (Data Analysis Expressions) functions in Power BI. Specifically, we will practice with the SUM, COUNT, AVERAGE, MIN, and MAX functions.

Dataset and Problem Scenario

To illustrate the application of these basic DAX functions, we will assume having a simple table named sales_data in our Power BI report which records the sales transactions of different products over time.

The sales_data table contains the following columns:

  • product_id: The unique identifier for each product
  • sale_date: Date of the sale
  • units_sold: Number of units sold
  • unit_price: Price per unit sold

DAX Basic Functions

SUM Function

SUM is a mathematical function that adds up all the numbers in a specified column. Assume we want to find the total unit sales from the sales_data table.

We can create a new measure with the following DAX expression:

Total Sales = SUM(sales_data[units_sold])

This measure will return the total units sold across all the products.

COUNT Function

COUNT is an arithmetic function that is used to get the count of an item in a column. To calculate the total number of unique product IDs, we can use the COUNT function.

Count Of Unique Products = DISTINCTCOUNT(sales_data[product_id])

DISTINCTCOUNT is a variation of the COUNT function, that counts the unique numbers in the specified column.

AVERAGE Function

AVERAGE function returns the average (arithmetic mean) of the numbers in the column. To compute the average units sold across all products, the DAX formula will look like this:

Average Units Sold = AVERAGE(sales_data[units_sold])

MIN Function

MIN function is used to determine the minimum value from the given numbers in a column. If we need to identify the lowest unit sold recorded, we can define a measure like this:

Minimum Units Sold = MIN(sales_data[units_sold])

MAX Function

MAX function returns the maximum number in a given column. We'll use it to find out the highest units sold from our 'sales_data'.

Maximum Units Sold = MAX(sales_data[units_sold])

These are the basic DAX functions which form the building block for most advanced DAX functions. These functions can now be used in visualizations, report layouts, filters, and other report entities to create meaningful insights from the data.

Mastering Advanced DAX Functions: FILTER, ALL, CALCULATE

Overview

In this section, we will dig deeper into learning some of the advanced DAX functions such as FILTER, ALL, and CALCULATE. These functions play a crucial role in data shaping and modeling to retrieve the data’s meaningful insights.

Please be aware that your data source connection, Power BI Desktop installation, and understanding of basic DAX functions should already have been completed as per the previous sections of this project.


FILTER()

The FILTER() function in DAX allows you to return a table of data based on specified filter expressions.

Here is how to use the FILTER function:

FILTER(
    TableName,       -- The name of the table that you want to filter
    Expression       -- The expression that will be evaluated for each row of the table
)

Suppose we have a table named 'Sales' and we want to filter those sales that are greater than 5000. This will be your DAX expression:

FilteredSales = 
CALCULATE(
    SUM('Sales'[SaleAmount]), 
    FILTER(
        'Sales', 
        'Sales'[SaleAmount] > 5000
    )
)

In the above example, we are using the FILTER function to filter the 'Sales' table for 'SaleAmount' > 5000, and then summing those values using the SUM function. CALCULATE function is explained in detail in the upcoming section.


ALL()

The ALL function is used to remove one or more filters from the columns or tables in Power BI.

Here is how you use the ALL function:

ALL (
    TableName      -- Name of the table for which you want to remove filters.
)

Suppose you have a 'Sales' table and you want to remove all the filters from it to calculate the total sales. You would use the following DAX statement:

TotalSales = 
CALCULATE(
    SUM('Sales'[SaleAmount]),
    ALL('Sales')
)

In the above example, we're calculating the total sales by removing all the filters on the 'Sales' table.


CALCULATE()

The CALCULATE function is a very powerful and versatile function that allows you to modify the context in which its arguments get evaluated.

Here is how you use the CALCULATE function:

CALCULATE (
    Expression,     -- An expression that you want to compute.
    Filter1, ...    -- One or more filters that redefine the context.
)

If you wanted to calculate the total sales in New York, using 'Sales' table, we would use this:

SalesNY = 
CALCULATE(
    SUM('Sales'[SaleAmount]),
    'Sales'[City] = "New York"
)

In the above example, we're using the CALCULATE function to override the default filter context and compute the sum of 'Sales'[SaleAmount] only for the 'Sales'[City] equals to "New York".


These three functions: FILTER, ALL, and CALCULATE are highly useful for data manipulation in DAX and Power BI. Understanding them is key to mastering DAX.

Sure, let's work on SUMX and COUNTX DAX aggregation functions. Both functions operate on a table by directing PowerBI to perform the operation row by row, column by column.

SUMX Function

The SUMX function returns the sum of an expression evaluated for each row in a table.

Here's a step-by-step example of its application:

  1. Assume you have a sales table like the one below:
| Product | Unit Price | Quantity |
|---------|------------|----------|
| A       | 10         | 3        |
| B       | 15         | 4        |
| C       | 20         | 2        |
  1. Now, you want to calculate the total sales (Unit Price * Quantity). You can create a new measure in Power BI using the SUMX function.
Total Sales = SUMX('Sales', 'Sales'[Unit Price] * 'Sales'[Quantity])

This formula goes row by row in the sales table, multiplies the unit price by the quantity for each product, and then sums those values up to give the total sales.

COUNTX Function

COUNTX function counts the number of rows of a table based on an expression.

For example, suppose you want to count the number of products with a quantity greater than 2.

  1. Assume you have the same sales table used above.

  2. Now, you can create a new measure in Power BI using the COUNTX function.

COUNTX('Sales', IF('Sales'[Quantity] > 2, 1, BLANK()))

In the COUNTX function, the expression used is IF('Sales'[Quantity] > 2, 1, BLANK()). This expression returns 1 if the quantity is greater than 2 and BLANK otherwise. The COUNTX function then counts the number of 1’s giving you the total number of products with a quantity greater than 2.

There you go, SUMX and COUNTX are two powerful aggregation functions that you can use on your tables in Power BI to perform complex calculations efficiently!

Sure, let's dive deep into the EVALUATE, ADDCOLUMNS, and SUMMARIZE table functions in DAX.

EVALUATE

EVALUATE is one of the most powerful DAX command since it allows you to output a table. EVALUATE requires the previous involvement of a table, and the output reflects the structure of this table.

The EVALUATE statement is a very simple expression that simply evaluates a table expression and returns a table:

EVALUATE <Table> 

For example, consider the following sample data:

Table: Sales
Columns: Product, Sales Amount

Product      Sales Amount
-------------------------
Product A    100
Product B    200
Product C    150

We can use EVALUATE to output this table:

EVALUATE Sales

ADDCOLUMNS

The ADDCOLUMNS function allows you to add new columns to the provided table. You can specify the name of the new column and DAX expression for each value of this column.

ADDCOLUMNS (<Table>, <Name>, <Expression>)

Using the same sales data from the EVALUATE section above:

EVALUATE 
    ADDCOLUMNS (
        'Sales',
        "Sales with VAT", 'Sales'[Sales Amount]*1.2
    )

This query will provide the table with an additional column "Sales with VAT" that contains sales amounts incremented by VAT (assumed here as 20%).

SUMMARIZE

SUMMARIZE helps you to create a summary of the specified table grouped by the specified columns. Additional columns can be added with expressions.

SUMMARIZE ( <Table>, <GroupBy_ColumnName> [, <GroupBy_ColumnName> [, ...] ] [, "<Name>", <Expression> [, ...] ] )

Using the same sales data from above:

EVALUATE 
    SUMMARIZE (
        'Sales',
        'Sales'[Product],
        "Total Sales", SUM('Sales'[Sales Amount])
    )

The SUMMARIZE will group by the 'Product' column and create an additional 'Total Sales' column that represents the total sales for each product.

These table functions provide you with powerful tools to manipulate and transform your data in DAX, enhancing your ability to perform complex data analysis in Power BI.

Time Intelligence Functions in DAX: DATE, EOMONTH, STARTOFMONTH

Introduction

Time Intelligence is a crucial concept in Power BI. In this section, we'll explore how to implement three common functions: DATE, EOMONTH, and STARTOFMONTH. We will not repeat the basics covered in previous sections, but will instead focus solely on the application of these functions in real-world scenarios.

Given that you have mastered understanding of Power BI and DAX, understanding calculations, mastering basic DAX functions, advanced DAX Functions, working with DAX Aggregation Functions and table functions in DAX, you should comfortably follow along.

Practical Implementation

We will use a simple Sales table consisting of two columns: OrderDate and SalesAmount.

Sales =
DATATABLE (
    "OrderDate", DATETIME,
    "SalesAmount", CURRENCY,
    {
        { "01/01/2020", 100 },
        { "01/15/2020", 200 },
        { "01/30/2020", 150 },
        { "02/15/2020", 125 },
        { "02/27/2020", 175 },
        { "03/10/2020", 225 },
        { "03/22/2020", 250 }
    }
)

DATE Function

In DAX, the DATE function takes in year, month, and day values and returns a date. It is useful when you have date components in separate fields and you need to combine them into a single datetime field.

NewColumn = DATE ( YEAR ( Sales[OrderDate] ), MONTH ( Sales[OrderDate] ), DAY ( Sales[OrderDate] ) )

This will create a new column, NewColumn in the Sales table where each value is the respective date of OrderDate.

EOMONTH Function

The EOMONTH function returns the date in datetime format of the last day of the month, which is some number of months before or after a specified date.

Sales[EOM] = EOMONTH ( Sales[OrderDate], 0 )

This will create a EOM column in the Sales table where each row will contain the end of month date based on the respective OrderDate.

STARTOFMONTH Function

The STARTOFMONTH function returns the first date of the month for the specified date, as a datetime value.

Sales[SOM] = STARTOFMONTH ( Sales[OrderDate] )

This will create a SOM column in the Sales table where each row will contain the start of month date based on the respective OrderDate.

This was a simple example demonstrating how to use the DAX DATE, EOMONTH, and STARTOFMONTH functions. These functions can be useful for creating reports on a monthly or custom time basis. In real-world applications, the tables and calculations can become much more complex. However, the core principles you learned here will still apply.

Sure, we can move further into DAX (Data Analysis Expressions) exploration by creating solutions for some more advanced patterns and use cases. We will create DAX expressions for the following patterns and scenarios:

  1. Handling Blank Values.
  2. Creating running total (Cumulative Sum).
  3. Forming a percent of total.
  4. Generating a Year-to-Date (YTD) calculation.
  5. Crafting an average per category.
  6. Establishing multi-level ranking.

We will assume you are already familiar with the Data Model of Power BI and have available fact and dimension tables for testing and checking our samples.

1. Handling Blank Values

In Power BI, it's common for numeric columns to have blank fields. We can handle these using the DAX function ISBLANK.

Measure_Blanks = 
IF(
   ISBLANK([Your Measure]), 
   0, 
   [Your Measure]
)

This expression checks if your measure returns a Blank and if so, it replaces the blank with a 0.

2. Creating Running Total (Cumulative Sum)

The running total is a common ask in Power BI - essentially a total that adds up as you go further down the rows. Here's how you can create it using CALCULATE and FILTER.

Cummulative Total =
CALCULATE (
    SUM ( FactSales[SalesAmount] ),
    FILTER (
        ALL ( DimDate ),
        DimDate[Date] <= MAX ( DimDate[Date] )
    )
)

3. Creating a Percent of Total

Calculating the part of the total a certain row represents can be useful in various occasions. This can be achieved using DIVIDE and ALLSELECTED.

Percent Of Total Sales = 
DIVIDE ( 
    [Total Sales], 
    CALCULATE ( 
        [Total Sales],
        // Consider all selected values
        ALLSELECTED ( DimProduct )
    ) 
)

4. Generating a Year-to-Date Calculation

Year-to-Date (YTD) calculations are very common in financial reports and many other scenarios. Power BI provides a set of Time Intelligence functions like TOTALYTD.

YTD Sales = 
TOTALYTD (
    SUM ( FactSales[SalesAmount] ),
    DimDate[Date]
)

5. Crafting Average per Category

We might want to calculate the average order quantity per category. Here's how to do that using AVERAGEX and VALUES.

Average = 
AVERAGEX(
    VALUES( FactSales[Category]),
    [Total Quantity]
)

6. Establishing Multi-Level Ranking

Power BI can rank data based on multiple columns or measures. We can do this using the RANKX function.

Multi Level Rank =
RANKX (
    ALL ( FactSales ),
    [Total Sales],
    [Total Cost],
    DESC,
    Skip
)

These are a few examples of more complex DAX expressions and scenarios. Based on your familiarity with the basic and advanced DAX functions, you can expand from here to create more complex DAX expressions. Remember, DAX expressions can be as simple or as complex as you need to get the job done.