top of page
gowrishankaritnp

TEXT Functions in Power BI (DAX)

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:


  1. Aggregation Functions

  2. Text Functions

  3. Filter Functions

  4. Logical Functions

  5. Relationship Functions

  6. Date and Time Functions

  7. Time Intelligence Functions


Let's see Text Functions in Detail:

Text Functions: Text functions in DAX (Data Analysis Expressions) are a set of functions designed to manipulate and analyze text strings within your data models. These functions allow users to perform various operations such as combining, extracting, formatting, and cleaning text data. They are particularly useful for data preparation, ensuring consistency, and enhancing the readability of reports and visualizations.


Type of Text Functions:


  • COMBINEVALUES: The COMBINEVALUES function in DAX is used to concatenate two or more text values into a single string, separating them with a specified delimiter. This is particularly useful when you need to create unique identifiers or combine multiple fields for analysis.


    Syntax: COMBINEVALUES(delimiter, value1, value2, ..., valueN)

    delimiter: The string used to separate the combined values.

    value1, value2, ..., valueN: The values to combine.


    Use Case: Let's create a calculated column to combine the customer name and their order ID to generate a unique identifier for each order. This can help in identifying orders easily when analyzing customer behavior.


    UniqueOrderID = COMBINEVALUES("_", Customers[customer_name],orders[total_price])

    The COMBINEVALUES function provides a straightforward way to concatenate values, which can be leveraged to create unique identifiers that enhance the ability to analyze customer behaviors and order patterns effectively.


  • CONCATENATE: The CONCATENATE function in DAX is used to join two text strings into one single string. Unlike COMBINEVALUES, which can concatenate multiple values with a delimiter, CONCATENATE works with just two strings and does not support delimiters.


    Syntax: CONCATENATE(<text1>, <text2>)

    text1: The first text string to concatenate.

    text2: The second text string to concatenate.


    Use Case: Let's create a  calculated column that combines the product name and its category to provide a more descriptive label for each product. This can help in reporting and understanding the product portfolio better.


    ProductDescription = CONCATENATE(Products[name], " _ " & Products[category])

The CONCATENATE function is useful for creating more descriptive text fields that enhance reporting capabilities. By using it to combine product names and categories, businesses can gain valuable insights into their sales performance and customer preferences, ultimately aiding in strategic decision-making.


  • CONCATENATEX: The CONCATENATEX function in DAX is used to concatenate values from a column or a table into a single string, with an option to specify a delimiter between each value. This function is particularly useful for creating aggregated text strings based on a specified grouping.


    Syntax: CONCATENATEX(table, expression, [delimiter], [order_by_expression], [order])

    table: The table or column containing the values to concatenate.

    expression: The expression that defines what to concatenate.

    delimiter (optional): The string used to separate the concatenated values.

    order_by_expression (optional): An expression to sort the values before concatenation.

    order (optional): Defines the sort order (ASC or DESC).


    Use Case: Let’s create a measure that concatenates the names of products ordered by a specific customer, separated by commas. This can provide a quick overview of what a customer has purchased.


    CustomerProductList = CONCATENATEX( FILTER( order_items, order_items[order_id] IN VALUES(orders[order_id]) ), RELATED(Products[name]), ", " )

    The CONCATENATEX function is a powerful tool for creating aggregated text strings in Power BI. By concatenating product names associated with customer purchases, businesses can gain deeper insights into customer behavior and preferences, enabling more effective marketing strategies and enhanced customer service.


  • EXACT: The EXACT function in DAX is used to compare two text strings to determine if they are exactly the same. This function is case-sensitive, meaning it will consider "Text" and "text" as different strings. It returns TRUE if the two strings are exactly the same, and FALSE otherwise.


    Syntax: EXACT(<text1>, <text2>)

    text1: The first text string to compare.

    text2: The second text string to compare.

  • Let's create a calculated column that checks if the customer's name is exactly the same as another string (e.g., "Sophia Martin"). This can be useful for identifying specific customers in your dataset.

  • IsSophiaMartin = EXACT(Customers[customer_name], "Sophia Martin")

By using the IsSophiaMartin calculated column, you can quickly filter or segment customers based on their names. For example, if you want to analyze the purchasing behavior of a specific customer, you can use this column to create a visual that focuses only on "Sophia Martin."


  • FIND: The FIND function in DAX is used to locate the position of a substring within a larger text string. It is case-sensitive, meaning it differentiates between uppercase and lowercase letters. The function returns the starting position of the substring if found; if not found, it returns an error unless a default value is specified.


    Syntax: FIND(find_text, within_text, [start_num], [not_found_value])

    find_text: The substring you want to find.

    within_text: The text string in which to search.

    start_num (optional): The position in within_text to start the search. Defaults to 1 if omitted.

    not_found_value (optional): The value to return if the substring is not found. If omitted, an error is returned.


    Let’s create a calculated column that checks if a customer’s email contains a specific domain, such as "gmail.com". This can be useful for identifying customers from a particular organization.


    IsExampleUser = IF( FIND("gmail.com", Customers[email], 1, 0) > 0, TRUE, FALSE )

    Use Case: Using the IsExampleUser calculated column, you can filter and analyze customers who use "gmail.com" as their email domain. This information can be valuable for targeted marketing campaigns or engagement strategies.


  • FIXED: The FIXED function in DAX is used to format a number as text, specifying a fixed number of decimal places. This function is particularly useful when you want to ensure that numeric values are displayed in a consistent format, especially in reports or dashboards where presentation is important.


    Syntax: FIXED(number, decimals, [no_commas])


    number: The numeric value you want to format.

    decimals: The number of decimal places to display.

    no_commas (optional): A Boolean value (TRUE or FALSE) that determines whether to include commas as thousands separators. The default is FALSE.


    Let’s create a measure that formats the total price of orders to display with two decimal places. This can enhance the readability of financial figures in reports.


    FormattedTotalPrice = FIXED(SUM(Orders[total_price]), 2,TRUE)


    Use Case: Using the FormattedTotalPrice measure, you can present total sales figures in a consistent, easy-to-read format. This is particularly useful in financial reports where clarity is critical.


    The FIXED function in DAX is valuable for ensuring that numerical data is presented clearly and consistently. By formatting financial figures, businesses can enhance their reporting and make it easier for stakeholders to understand key metrics. Utilizing formatting functions like FIXED is essential for effective data visualization in Power BI.


  • FORMAT: The FORMAT function in DAX is used to convert a value to text in a specified format. This function is particularly useful for displaying numbers, dates, or currency in a way that is more readable and appropriate for reporting purposes.


    Syntax: FORMAT(value, format_string)

    value: The numeric or date value you want to format.

    format_string: A string that specifies the format to apply. This can include predefined formats or custom formatting codes.


    Let’s create a measure that formats the total price of orders as currency, ensuring that it displays with the appropriate currency symbol and two decimal places.


    FormattedTotalPrice = FORMAT(SUM(Orders[total_price]), "Currency")

    Using the FormattedTotalPrice measure, you can present total sales figures in a clear currency format, which enhances the readability of financial reports.

    Utilizing formatting functions like FORMAT is essential for effective data visualization in Power BI.


  • LEFT: The LEFT function in DAX is used to extract a specified number of characters from the beginning (left side) of a text string. This function is helpful when you want to isolate specific parts of a string, such as prefixes or codes.


    Syntax: LEFT(text, num_chars)

    text: The text string from which you want to extract characters.

    num_chars: The number of characters to extract from the left side of the text string.


    Let’s create a calculated column to extract the first few characters of product names in the product table. This can help in categorizing products or creating short identifiers for display purposes.


    ShortProductName = LEFT(Product[name], 5)

Using the ShortProductName calculated column, you can create a more concise representation of product names, which is useful in visuals where space is limited.

The LEFT function in DAX is versatile and can be used in various contexts to manipulate text strings effectively. By creating a calculated column to generate short product names, businesses can enhance their data analysis and reporting capabilities in Power BI, allowing for clearer insights and informed decision-making.


  • LEN: The LEN function in DAX is used to calculate the length of a text string. It returns the number of characters in a given string, including spaces. This function is useful for validating data integrity, identifying anomalies, or creating derived metrics based on text length.


    Syntax: LEN(text)

    text: The text string for which you want to determine the length.


    Let’s create a calculated column in the customers table to determine the length of each customer's name. This can be useful for identifying unusually short or long names that might require further attention.


    NameLength = LEN(Customers[customer_name])

    Using the NameLength calculated column, you can analyze the lengths of customer names to identify potential data quality issues, such as missing or incomplete entries.


  • LOWER: The LOWER function in DAX is used to convert all uppercase letters in a text string to lowercase. This function is particularly useful for standardizing text data, ensuring consistency, especially when performing comparisons or analyses.


    Syntax: LOWER(text)

    text: The text string you want to convert to lowercase.


    Let’s create a calculated column in the customers table to store the lowercase version of each customer's email address. This can help with data normalization, especially when analyzing email domains.


    LowercaseEmail = LOWER(Customers[email])

    Using the LowercaseEmail calculated column, you can easily analyze and categorize customers based on their email domains without worrying about case sensitivity. The LOWER function in DAX is a useful tool for standardizing text data. By creating a calculated column to convert email addresses to lowercase, businesses can enhance their data quality and perform more accurate analyses in Power BI.


  • UPPER: The UPPER function in DAX is used to convert all lowercase letters in a text string to uppercase. This function is particularly useful for standardizing text data, ensuring consistency, especially when performing comparisons or analyses.


    Syntax: UPPER(text)

    text: The text string you want to convert to uppercase.


  • MID: The MID function in DAX is used to extract a specific number of characters from a text string, starting at a specified position. This function is useful for isolating parts of a string, such as extracting substrings from longer text entries.


    Syntax: MID(text, start_position, num_chars)

    text: The text string from which you want to extract characters.

    start_position: The position in the text string to start extracting characters (1-based index).

    num_chars: The number of characters to extract.


    Let’s create a calculated column to extract a product's category from product description. Assuming the product descriptions are formatted as "ProductName_Category ", we can extract the category from the end of the string.


    ProductCategory = MID( Product[ProductDescription], FIND("_", Product[ProductDescription]) + 1, LEN(Product[ProductDescription]) - FIND("_", Product[ProductDescription]) )


    • FIND("_", Product[ProductDescription]): This part finds the position of the underscore in the product description.

    • FIND("_", Product[ProductDescription]) + 1: This adds 1 to the position of the underscore to get the starting point for the category.

    • LEN(Product[ProductDescription]) - FIND("_", Product[ProductDescription]): This calculates the number of characters to extract, which is the total length of the string minus the position of the underscore.

    • MID(): Finally, the MID function extracts the substring starting from just after the underscore to the end of the string.

Using the ProductCategory calculated column, you can categorize products based on their descriptions, allowing for easier analysis of sales and inventory by category.


  • REPLACE: The REPLACE function in DAX is used to replace part of a text string with a different text string. This function is helpful for making specific changes to strings, such as correcting errors, updating formats, or modifying text data.


    Syntax: REPLACE(old_text, start_position, num_chars, new_text)

    old_text: The original text string where replacements will occur.

    start_position: The position in the string where the replacement begins (1-based index).

    num_chars: The number of characters in the original text string that you want to replace.

    new_text: The text string that will replace the specified characters in old_text.


    Let’s create a calculated column to standardize addresses by replacing any occurrences of "Ave" with "Avenue". This ensures consistency in address formatting.


    StandardizedAddress = IF( FIND("Ave", Customers[address], 1, 0) > 0, REPLACE(Customers[address], FIND("Ave", Customers[address]), LEN("Ave"), "Avenue"), Customers[address] )

Using the calculated column StandardizedAddress, we aim to replace "Ave" with "Avenue" to achieve a consistent format.

The use case for standardizing address formats demonstrates how a simple calculated column can significantly improve data quality, customer satisfaction, and operational efficiency. By ensuring that all addresses are formatted consistently, businesses can better serve their customers and make more informed decisions based on accurate data.


  • REPT: The REPT function in DAX is used to repeat a text string a specified number of times. This can be particularly useful for creating visual representations, such as progress bars or to enhance text display in reports.


    Syntax: REPT(text, number_of_times)

    text: The text string that you want to repeat.

    number_of_times: The number of times you want to repeat the text string.


    Let’s create a calculated column in the orders table that uses the REPT function to create a simple text-based progress bar based on the total price of each order. We will represent the total price as a series of asterisks (*), where each asterisk represents a certain dollar amount (e.g., $10).


    OrderProgress = REPT("*", ROUND(Orders[total_price] / 10,0))

    text: "*" - This is the text string that we want to repeat.

    number_of_times: ROUND(Orders[total_price] / 10, 0) - This calculates how many times to repeat the asterisk. We divide the total_price by 100 and round it to the nearest whole number to determine the number of asterisks.


    Create a table visual that displays customer_name, order_date, and OrderProgress. This can provide an at-a-glance understanding of the size of each order based on the number of asterisks.

The REPT function in DAX is a versatile tool for enhancing text visualization in reports. By creating a calculated column that generates a progress indicator based on order values, businesses can improve the interpretability of their data, making it easier for users to grasp key insights at a glance. Using text-based indicators like this can complement numerical analysis, leading to better decision-making and reporting in Power BI.


  • RIGHT: The RIGHT function in DAX is used to extract a specified number of characters from the end (right side) of a text string.


    Syntax: RIGHT(text, num_chars)

    text: The text string from which you want to extract characters.

    num_chars: The number of characters to extract from the end of the string.


    Let’s create a calculated column that extracts the domain part of email addresses from the customers table. This can help in analyzing which email domains are most commonly used by customers.


    EmailDomain = RIGHT(Customers[email], LEN(Customers[email]) - FIND("@", Customers[email]))

    text: Customers[email] - This refers to the email address from which we want to extract the domain.

    FIND("@", Customers[email]): This finds the position of the "@" character in the email address.

    LEN(Customers[email]) - FIND("@", Customers[email]): This calculates the length of the domain by subtracting the position of "@" from the total length of the email address.

    RIGHT: Finally, RIGHT uses the calculated length to extract the domain part from the email.

    The RIGHT function in DAX is effective for extracting specific components from text strings, such as the domain from email addresses.


  • SEARCH: The SEARCH function in DAX is used to find the position of a substring within a string. It is case-insensitive and can return the starting position of the substring if found, or an error if not found. This function is useful for locating specific text within larger strings.


    Syntax: SEARCH(find_text, within_text, [start_position], [not_found_value])

    find_text: The substring you want to find.

    within_text: The text string in which you want to search for the substring.

    start_position: (optional) The position in the string from which to start the search. Defaults to 1.

    not_found_value: (optional) The value to return if the substring is not found. Defaults to an error.


    Let’s create a calculated column in the product table that indicates whether the product name contains the word "Special." This can be useful for highlighting promotional items or unique products. Include word "Special" in any product name and create the below calculated column.

    IsSpecialProduct = IF( SEARCH("Special", Product[name], 1, 0) > 0, "Yes", "No" )

    SEARCH("Special", Product[name], 1, 0): This looks for the substring "Special" within the product name. It starts the search from the first character. If "Special" is found, it returns its position; otherwise, it returns 0 (due to the not_found_value parameter).

    IF(... > 0, "Yes", "No"): This checks if the result of SEARCH is greater than 0. If it is, it means "Special" was found in the product name, and it returns "Yes"; otherwise, it returns "No".

The SEARCH function in DAX is a powerful tool for locating substrings within text strings. By creating a calculated column to identify special products, businesses can enhance their product analysis and marketing strategies in Power BI, leading to more informed decision-making and improved sales performance.


  • SUBSTITUTE: The SUBSTITUTE function in DAX is used to replace occurrences of a specified substring within a string with another substring. This function is useful for cleaning up text data or standardizing formats in reports.


    Syntax: SUBSTITUTE(text, old_text, new_text, [instance_num])

    text: The original text string.

    old_text: The substring that you want to replace.

    new_text: The substring that will replace old_text.

    instance_num: (optional) Specifies which occurrence of old_text to replace. If omitted, all occurrences are replaced.


    Let’s create a calculated column in the customers table to standardize the word "St." to "Street" in the address field. This can help maintain consistency in address formatting.


    StandardizedAddress = SUBSTITUTE(Customers[address], "St.", "Street")

    text: Customers[address] - This is the original address string that we want to modify.

    old_text: "St." - This is the substring we want to replace.

    new_text: "Street" - This is the substring that will replace "St.".

Using the StandardizedAddress calculated column, businesses can ensure that all address entries are formatted consistently, which is crucial for any geographical analysis or mailings.


SUBSTITUTE is more forgiving when it comes to missing substrings, making it safer to use when you’re unsure if the text exists in the string.

REPLACE requires precise positioning, and if that position does not exist, it results in an error.

This behavior makes SUBSTITUTE generally more versatile for text replacements when the presence of the target substring is uncertain.


  • TRIM: The TRIM function in DAX is used to remove all leading and trailing spaces from a text string. This function is particularly useful for cleaning up text data, especially when data is imported from external sources where extra spaces may inadvertently be included.


    Syntax: TRIM(text)

    text: The original text string from which you want to remove spaces.

    Let’s create a calculated column in the customers table that removes any leading or trailing spaces from the customer_name field. This can help ensure consistency in how customer names are displayed and processed.


    CleanCustomerName = TRIM(Customers[customer_name])

    text: Customers[customer_name] - This refers to the original customer name that we want to clean up by removing spaces.

    By default power BI will take care of trim spaces.

    Using the CleanCustomerName calculated column, businesses can ensure that customer names are formatted consistently, which is crucial for any reporting, customer communications, or analysis.


  • VALUE: The VALUE function in DAX is used to convert a text string that represents a number into a numeric value. This is particularly useful when working with data imported from sources where numbers may be stored as text.


    Syntax: VALUE(text)

    text: The text string that you want to convert to a numeric value.

    Let’s create a calculated column in the order_items table that converts the quantity column, which might be stored as text, into a numeric value. This ensures that any calculations performed on this column are accurate.


    NumericQuantity = VALUE(Order_Items[quantity])

    text: Order_Items[quantity] - This refers to the quantity column that we want to convert to a numeric value.


    Using the NumericQuantity calculated column, businesses can ensure that any calculations involving order quantities are performed accurately, allowing for reliable reporting and analysis.


  • Use Cases:

    • Data Cleansing: Functions like TRIM, UPPER, and LOWER are useful for standardizing text data.

    • Dynamic Labels: Use CONCATENATEX to create dynamic titles or labels based on data context.

    • Substring Extraction: Functions like LEFT, RIGHT, and MID can be used to extract parts of a string, useful for parsing structured data.

    • Search and Replace: Use FIND, SEARCH, REPLACE, and SUBSTITUTE for modifying strings based on specific criteria.

    • Character Counting: LEN can help in validating string lengths, which is important for data integrity.


Conclusion

Text functions in DAX are powerful tools for managing and manipulating string data. They are critical for data preparation, analysis, and creating user-friendly reports in Power BI. Understanding these functions can significantly enhance your ability to work with text-based data effectively.


Thank you for reading. Happy learning!!! If this has been of immense value to you, do give me a clap.

80 views

Recent Posts

See All
bottom of page