What is DAX?
DAX is a collection of functions, operators, and constants that can be used in formulas or expressions to calculate and return one or more values.
It helps create new information from data already in our model.
Why is DAX Important?
DAX enables complex calculations and data analysis that go beyond the capabilities of standard aggregation functions.
It allows for the creation of custom measures and calculated columns, enhancing the analytical power of our reports.
Basic Concepts in DAX
Syntax: The structure of a DAX formula, including functions, operators, and references to columns or tables.
Functions: Predefined formulas that perform specific calculations or actions on data.
Context: The environment in which a DAX formula is evaluated, including row context and filter context.
Download the four CSV files below to gain hands-on experience with DAX functions in Power BI, along with some insightful information.
DAX Functions in Power BI
DAX functions are categorized into several types, each serving different purposes. Here are some common categories and examples:
Aggregation Functions
Text Functions
Filter Functions
Logical Functions
Relationship Functions
Date and Time Functions
Time Intelligence Functions
Let's see Aggregation Functions in Detail:
Aggregation Functions:
Aggregation functions in Power BI are essential tools for summarizing and analyzing data. They allow you to perform mathematical operations on a set of values to produce a single result, which can be used to gain insights and make data-driven decisions.
Aggregation functions help simplify complex data sets by summarizing large volumes of data into meaningful metrics. This makes it easier to analyze trends, compare performance, and make informed decisions.
Types of Aggregation Functions
SUM: The SUM function in DAX is used to add all the numbers in a column.
Syntax: SUM(<column>)
Use case: A retail company wants to track its overall sales performance to understand how well it is meeting its revenue goals.
TotalSalesAmount = SUM(orders[total_price])
When you use this measure in a report, Power BI will calculate the total sales amount by adding up all the values in the total_price column of the orders table. This allows you to see the total revenue generated from all orders at a glance.
SUMX: The SUMX function in Power BI is a powerful DAX function that performs row-by-row calculations on a specified table and then sums the results.
Syntax: SUMX(table, expression)
SUMX evaluates the expression for each row in the specified table. This means you can create complex calculations that depend on the values in multiple columns of that row. After evaluating the expression for all rows, SUMX sums up all the resulting values.
Use Case: A company wants to analyze the performance of its products to identify top-selling items and inform inventory management.
TotalRevenuePerProduct = SUMX( order_items, order_items[quantity] * RELATED(product[price]) )
This iterator goes through each row in the order_items table.
For each row, it multiplies the quantity of items sold (order_items[quantity]) by the corresponding product price, retrieved using the RELATED function from the product table.
SUMX then sums up all these individual revenue calculations to give the total revenue for all products.
This measure provides valuable insights into product revenue that drive strategic decisions, enhance inventory management, and improve overall business performance.
AVERAGE: The AVERAGE function in Power BI is used to calculate the arithmetic mean of a set of values in a specified column. This function is particularly useful for analyzing numeric data and gaining insights into overall trends and performance.
Syntax: AVERAGE(<column>)
Use Case: A retail company wants to analyze its sales performance and understand customer purchasing behavior through average revenue per order.
AverageRevenuePerOrder = AVERAGE(orders[total_price])
By analyzing the average revenue per order, the company can assess overall sales performance.
Trend Line Chart: A line chart visualizes the AverageRevenuePerOrder over time (e.g., monthly or quarterly). This helps identify trends and seasonal fluctuations in customer spending.
AVERAGEA: The AVERAGEA function in Power BI is used to calculate the average of a set of values, including both numeric and non-numeric data types. Unlike the AVERAGE function, AVERAGEA can evaluate logical values and text representations of numbers, treating them as follows:
For numbers: It computes the average of the numbers.
For logical values: TRUE is treated as 1 and FALSE is treated as 0.
For text values: Text representations of numbers (like "5") are treated as their numeric equivalents. Other text values are treated as 0.
For blank values: Blank values are ignored in the calculation.
Syntax: AVERAGEA(<column>)
Use Case: A retail company wants to assess the average value of items sold across different orders to better understand purchasing behavior and inform inventory decisions.
AverageSubtotalPerOrderItem = AVERAGEA(order_items[subtotal])
Identifying products with consistently high average subtotals can guide inventory management decisions. The company may choose to stock more of these high-value items.
By drilling down by year, we can observe the average subtotal over time.
AVERAGEX: The AVERAGEX function in Power BI is used to calculate the average of an expression evaluated over a table. Unlike the AVERAGE function, which only works on a single column, AVERAGEX allows you to perform calculations on rows of a table and then average the results.
Syntax: AVERAGEX(<table>, <expression>)
Use Case: A retail company wants to analyze the average revenue generated per order item to assess product performance and customer purchasing behavior.
AverageRevenuePerOrderItem = AVERAGEX( order_items, order_items[quantity] *
RELATED(product[price]) )
The average revenue per order item helps the company identify which products are generating the most revenue. High-performing products can be highlighted for promotions or increased stock.
COUNT: The COUNT function in Power BI is used to count the number of rows that contain non-blank values in a specified column. It is particularly useful for analyzing data and understanding the frequency of occurrences in a dataset.
Counts only the number of non-blank values in a specified column.
Syntax: COUNT(<column>)
Use Case: A retail company wants to track the total number of orders placed over a specific period to evaluate sales performance.
TotalOrdersCount = COUNT(orders[order_id])
The measure TotalOrdersCount counts the total number of orders by counting the non-blank order_id values in the orders table.
Trend Line Chart: A line chart visualizes the total orders over time (e.g., monthly or quarterly), helping to identify trends in customer demand.
COUNTA: The COUNTA function in Power BI counts the number of non-blank values in a specified column, including both text and numeric values. This function is useful when you want to count all entries in a column, regardless of their data type.
Counts all non-blank values in a specified column, regardless of data type (numeric, text, boolean, or logical).
Useful when you want to count all entries, including boolean values.
Syntax: COUNTA(<column>)
Use Case: A retail company wants to count how many entries exist in the customer table, including those with boolean values (like valid_customer).
Before creating valid_cutomer column replace any email id as null in power query editor.
Then create valid_customer column valid_customer= IF(NOT(ISBLANK (customers[email])),TRUE(),FALSE())
Since the valid_customer column contains Boolean values (TRUE and FALSE), the COUNT function will return error because it does not recognize Boolean values as numeric. It only counts numeric entries.
TotalCustomerEntries = COUNTA(customers[valid_customer])
This measure counts all non-blank entries in the valid_customer column.
COUNTAX: The COUNTAX function in Power BI is used to count the number of rows that meet specific criteria, based on an expression evaluated over a table. Unlike the standard COUNT function, which only counts non-blank values in a single column, COUNTAX allows you to count based on calculations or conditions applied to each row in a table.
Syntax: COUNTAX(<table>, <expression>)
Let’s create a measure to count the number of valid customers in the customer table, based on the valid_customer column, which contains TRUE or FALSE.
CountValidCustomers = COUNTAX( customers, IF(customers[valid_customer] = TRUE(), 1, BLANK()) )
COUNTBLANK: The COUNTBLANK function in DAX is used to count the number of blank (empty) values in a column.
Syntax: COUNTBLANK(column)
Use Case: You want to understand how many customers have not provided their phone numbers. This insight can help in assessing the completeness of customer data, which is crucial for communication and marketing strategies.
Before creating measure replace any phone number as null in power query editor.
Let’s create a measure that counts the number of blank phone numbers in the customers table:
CountBlankPhoneNumbers = COUNTBLANK(customers[phone]
COUNTROWS: The COUNTROWS function in DAX is used to count the number of rows in a specified table or in a table returned by a DAX expression.
Syntax: COUNTROWS(table)
table: The table for which you want to count the rows.
Let’s create a measure that counts the total number of orders in the orders table:
TotalOrders = COUNTROWS(orders)
You want to track the total number of orders placed over a certain period to assess sales activity and performance use COUNTROWS function.
DISTINCTCOUNT: The DISTINCTCOUNT function in DAX is used to count the number of unique (distinct) values in a column.
Syntax: DISTINCTCOUNT(column)
column: The column for which you want to count the distinct values.
Use Case: You want to track how many unique customers have placed orders over a certain period. This insight helps you understand customer engagement and the reach of your business.
Let’s create a measure that counts the number of unique customers who have placed orders in the orders table:
UniqueCustomers = DISTINCTCOUNT(orders[customer_id])
DISTINCTCOUNTNOBLANK: The DISTINCTCOUNTNOBLANK function in DAX counts the number of unique values in a column, excluding any blank values.
Syntax: DISTINCTCOUNTNOBLANK(column)
column: The column from which you want to count the distinct values, ignoring any blank entries.
Before creating below measure replace any phone number with any existing phone number using power query editor.
Let’s create a measure that counts the number of unique customers phone number:
UniquePhoneNoBlank = DISTINCTCOUNTNOBLANK(customers[phone])
You want to determine how many unique customers have provided valid phone numbers. This insight is important for understanding your ability to reach customers for promotions, support, or feedback.
Using the DISTINCTCOUNTNOBLANK function on the phone column provides valuable insights into customer contact information, helping businesses enhance communication strategies and improve data quality.
MIN: The MIN function in DAX returns the smallest value from a specified column or a set of values.
Syntax: MIN(column)
column: The column from which you want to find the minimum value.
Let’s create a measure that calculates the minimum total price from the orders table:
MinTotalPrice = MIN(orders[total_price])
You want to identify the lowest order value within a specified period. This insight can help assess the performance of lower-value transactions and understand pricing strategies. The MIN function is a valuable tool for analyzing data in Power BI. By tracking the minimum values in key metrics such as order totals, businesses can gain insights into sales performance, refine pricing strategies, and optimize their product offerings to enhance revenue growth.
MINA: The MINA function in DAX returns the smallest value in a column, considering numbers, text, and logical values (TRUE/FALSE). If the column contains text, the function treats it as zero when calculating the minimum.
Syntax: MINA(column)
column: The column from which you want to find the minimum value, including numeric, text, and logical values.
Change the datatype of column subtotal from table order_items and change few subtotal values as N/A or TRUE. Then create the below two measures.
Min_Subtotal = MIN(order_items[subtotal])
MinA_Subtotal = MINA(order_items[subtotal])
Min Returns 10 because it ignores N/A and MinA returns 0 because it considered N/A as 0.
Non-Numeric Handling:
MIN ignores "N/A" and returns only numeric values.
MINA treats "N/A" as 0 and includes it in calculations.
Logical Values:
MIN does not consider TRUE/FALSE values at all.
MINA treats TRUE as 1 and FALSE as 0, affecting the minimum value if they are included in the context.
Blank Values:
Both functions ignore NULL values in their calculations.
Using MINA can help identify issues with data quality. If you notice that MINA returns a value that seems unexpectedly low due to text being treated as zero, it might prompt a review of data entry practices or the need for data cleaning.
MINX: The MINX function in DAX returns the smallest value from a table or a table expression evaluated for each row. This function is particularly useful when you need to calculate the minimum of an expression that cannot be directly evaluated as a single column.
Syntax: MINX(table, expression)
table: The table or table expression that you want to iterate over.
expression: The expression to be evaluated for each row in the table.
Let’s create a measure that calculates the minimum quantity sold for items that have a price greater than $20. This helps understand how low the sales are for higher-priced products.
MinQuantityAbovePrice =
MINX( FILTER( order_items, RELATED(product[price]) > 20 ), order_items[quantity] )
The minimum quantity ordered for the product price > 20 is 1 from our dataset.
MAX: The MAX function in DAX returns the largest numeric value from a specified column or expression. It is a simple but powerful function often used in data analysis.
Syntax: MAX(column)
column - The column from which you want to find the maximum value.
Let’s create a measure that calculates the maximum quantity of a product sold in a single order. This helps understand the highest demand for any item in your inventory.
MaxQuantitySold = MAX(order_items[quantity])
By knowing the maximum quantity sold, you can gain insights into customer demand for specific products, which can inform inventory management and marketing strategies.
MAXA: The MAXA function in DAX returns the largest value in a column, considering numbers, text, and logical values (TRUE/FALSE). If the column contains text, the function treats it as zero when calculating the maximum.
Syntax: MAXA(column)
column: The column from which you want to find the maximum value, including numeric, text, and logical values.
MAXX: The MAXX function in DAX returns the largest value from a table or a table expression evaluated for each row. This function is particularly useful when you need to calculate the maximum of an expression that cannot be directly evaluated as a single column.
Syntax: MAXX(table, expression)
table: The table or table expression that you want to iterate over.
expression: The expression to be evaluated for each row in the table.
Let’s create a measure that calculates the maximum quantity sold for items that have a price lesser than $20. This helps understand how high the sales are for higher-priced products.
MaxQuantityAbovePrice =
MAXX( FILTER( order_items, RELATED(product[price]) > 20 ), order_items[quantity] )
The maximum quantity ordered for the product price > 20 is 3 from our dataset.
Use Cases:
Sales Analysis: Use SUM to calculate total sales and AVERAGE to find the average sale amount.
Performance Metrics: Employ MIN, MAX, and DISTINCTCOUNT to analyze performance metrics such as minimum and maximum sales, or unique customers.
Data Quality: Use COUNTA and COUNTBLANK to assess the completeness of data.
Custom Calculations: Use SUMX or AVERAGEX for calculations that involve iterating over tables and applying specific expressions, such as calculating total profit per product.
Conclusion
Aggregate functions in DAX are fundamental for summarizing data and providing insights in Power BI. By understanding and effectively using these functions, you can derive meaningful analyses from your data, enabling better decision-making and reporting.
Thank you for reading. Happy learning!!! If this has been of immense value to you, do give me a clap.