Dax Function: REPLACE

Category: Text Functions

The REPLACE function in Power BI is a DAX function used to replace a portion of a text string with a new string. It is useful for modifying text data by substituting specific characters or substrings.

Purpose

  • Text Manipulation: Replace specific portions of text within a string.
  • Data Cleansing: Standardize text fields or correct erroneous data.
  • Dynamic Substitutions: Modify text dynamically based on context or conditions.

Type of Calculations

  • Performs substring replacements within text fields.
  • Handles scalar (row-level) calculations efficiently.

Practical Use Cases

  1. Updating outdated codes or identifiers in product descriptions.
  2. Replacing unwanted characters or formatting in text (e.g., removing dashes from phone numbers).
  3. Customizing text fields by substituting placeholders with actual values.

REPLACE(<text>, <start_position>, <num_chars>, <new_text>)</new_text></num_chars></start_position></text>

 
ParameterTypeDescription
textText/ColumnThe original text string where the replacement will occur.
start_positionIntegerThe position (1-based) where the replacement begins.
num_charsIntegerThe number of characters to replace starting from the position specified.
new_textTextThe text that will replace the specified substring.


How Does REPLACE
 Dax Function Works

  • The REPLACE function identifies the start position in the input text (start_position).
  • It replaces a specific number of characters (num_chars) starting from the position with the new_text.
  • The remaining part of the text, outside the replacement range, remains unchanged.

What Does It Return?

The function returns a text string with the specified portion replaced by the new text. If the input is blank or invalid, it returns a blank.

When Should We Use It?

  • To clean or format text data (e.g., removing or replacing delimiters).
  • When customizing text fields for reporting or user-friendly outputs.
  • In scenarios where dynamic text substitutions are required.

Examples

Basic Usage – Replace Part of a String:


REPLACE("Power BI is fun", 8, 2, "Excel")

Result: "Power Excel is fun"

Column Usage – Standardize Phone Numbers:


StandardizedPhone = REPLACE(Customers[PhoneNumber], 1, 4, "(+1) ")

Replaces the first 4 characters of phone numbers with the standardized international code.

Advanced Usage – Combine with FIND for Dynamic Substitutions:


ReplaceDomain = REPLACE(Customers[EmailAddress], FIND("@", Customers[EmailAddress]), LEN(Customers[EmailAddress]) - FIND("@", Customers[EmailAddress]) + 1, "@newdomain.com")

Replaces the domain part of email addresses with a new domain.

Tips and Tricks

  • Use LEN to calculate dynamic lengths for replacement.
  • Combine with SEARCH or FIND to locate specific positions for replacements.
  • Be cautious of invalid start_position or num_chars values as they may result in unexpected behavior or blank outputs.

Performance Impact of REPLACE DAX Function:

  • Efficient for row-level text replacements in smaller datasets.
  • For large datasets, consider optimizing text transformations through calculated columns instead of calculated measures when replacements are static.

Related Functions You Might Need

  • SUBSTITUTE: Replace specific occurrences of a substring with another substring.
  • MID: Extract a specific portion of a text string.
  • LEFT/RIGHT: Extract characters from the start or end of a string.
  • LEN: Determine the total length of a text string.

Want to Learn More?
For more information, check out the official Microsoft documentation for REPLACE You can also experiment with this function in your Power BI reports to explore its capabilities.

Unlock the full capabilities of Power BI and elevate your data insights with our specialized consulting services. Whether you need guidance on advanced DAX functions like those highlighted here, support in designing interactive dashboards, or expertise in optimizing data models for enhanced performance, our experienced Power BI consultants are equipped to deliver customized solutions for your business. Explore our Power BI Consulting Services page to discover how we can help your organization make smarter, data-driven decisions.

1. What does the REPLACE function do in Power BI?

The REPLACE function substitutes a specified portion of text in a string with new text.

2. Is the start position in the REPLACE function 0-based or 1-based?

The start position is 1-based, meaning the first character is at position 1.

3. Can the REPLACE function handle dynamic replacements?

Yes, combining REPLACE with functions like FIND or LEN allows for dynamic substring replacements.

4. What happens if the starting position exceeds the text length?

If the starting position is beyond the text length, the function returns the original text unchanged.

5. How does REPLACE differ from SUBSTITUTE in Power BI?

REPLACE replaces text at a specified position and length, while SUBSTITUTE replaces all or specific occurrences of a substring.