Imagine you have a paragraph of text and you want to replace all occurrences of {four, normal, mysterious, nonsense} with {six, casual, confounding, handbags}. **How would you do that?**

You could use SUBSTITUTE() formula, but you need to nest four of them (as we need to replace four values with another four). But what if you have larger set of find / replacements?

Worry not, you can use Power Query to transform original text to new one by replacing all matching values.

In this page, learn how to do that with the excellentÂ **List.Accumulate()Â **Power Query function.

## The set up for multiple find / replace

Let’s start with two tables –Â **mytextÂ **with the original text values andÂ **replacementsÂ **with rules for replacement.

I have included a snapshot of these tables below.

## Power Query transformations

Start by loading both of these tables to Power Query. We will *transformÂ *mytext table to add a column with replaced text.

Before we could do the transformations, **you must learn 3 key concepts**.

They are,

- List.Accumulate() Function
- Getting Lists from Table columns
- How to access individual list items

### List.Accumulate() Function

**List.Accumulate()** function allows us to perform same action on items on a list and return a combined value.

A simple example for this is,

Imagine you have a list of 10 numbers {1..10}

You want to calculate the total of these numbers.

You can use List.Accumulate() to add them up.Â *Of course, you can also use List.Sum() function, but this is a demo of List.Accumulate() dear.*

Assuming the original list is inÂ **source,** we can use List.Accumulate() to add up items like this:

=List.Accumulate(Source, 0, (state, current) => state+current)

The output of this step will be 55, sum of list with first 10 numbers.

ðŸ¤¯ðŸ¤¯ðŸ¤¯

**What’s going on here?**

List.Accumulate() is aÂ **cumulative function. **It takes a list and a starting value (in our case these areÂ **sourceÂ **andÂ **0**) and applies a function on each of the list items while modifying theÂ **initial value.**

Let’s see what that means for our list.

- We start with 0 (call this starting
**state**) - For each item in the list
- Update state to state + current (
*ie*0+1 initially, then 1+2, 3+3, 6+4…45+10)

- Update state to state + current (
- At the end of the list, it returns the final state value,Â
*ieÂ*55.

**How to write the third argument of List.Accumulate()?**

The third argument of List.Accumulate() is a function with two parameters – state, current.Â

**State**:Â this will be initially set to 2nd parameter of the function and changes every time List.Accumulate() moves down the list.**Current**: this will the value of current list item

Power Query functions are written in this fashion.

(parameter1, parameter2…) => function definition

So, for example (state, current) => state+current is a function that takes 2 parameters and returns their sum.

**To learn more about List.Accumulate():**

- Watch the video that accompanies this article. You will get clarity about List.Accumulate()
- Read Datachant’s excellent List.Accumulate examples page.

### Getting Lists from Table columns

If you have a table namedÂ **replacementsÂ **with columns [Find] and [Replace] in Power Query, you can use below syntax to extract a table column as list.

**replacements[Find]**

tablename[Column name]

### How to access individual list items

If you have a list named **source **in Power Query with 10 items, you can access 5th item of the list with this syntax.

**source{4}**

So, for example, if you want to get 3rd item of the [Find] column in replacements table, use:

=replacements[Find]{2}

*Note: Power Query uses 0 base for lists. So first item of the list will be list{0}*

## Using List.Accumulate for mass Find / Replace

Now that you are familiar with key concepts necessary, let’s do some replacements.

Go toÂ **mytextÂ **query and insert a custom column. In this column, we will generated replaced text.

Write this formula:

List.Accumulate( List.Numbers(0, Table.RowCount(replacements)), [Text], (state, current) => Text.Replace(state, replacements[Find]{current}, replacements[Replace]{current}))

**How does this formula work?**

- We create a list of numbers from 0. The size of this list will be same as number of rows in replacements table.Â
*For our sample data, we get {0,1,2,3}*

- We start with input [Text] column valueÂ
- We
replace first replacements[Find] value with replacements[Replace] in [Text] - We repeat 3 step three, while updating theÂ
**stateÂ** - At the end of this process, we end up with [Text] that
replaced all words in replacements table.**successivelyÂ**

I know this formula is tricky to get your head around, but once you understand it, you will see the potential for other cool applications.

Please click here to download the multiple find replace example file

## List.Accumulate explained - Video

If you are still ðŸ˜• and not sure how List.Accumulate magic works, then please watch this video. It goes in to greater detail about thisÂ *beautiful M function. *See it below or visit my YouTube channel for it.

## More on Power Query...

Please check out below pages to learn more about List.Accumulate() in particular and PQ in general.

**List.Accumulate examples:**

- List.Accumulate unleashed – several examples [Datachant]
- Running totals with List.Accumulate [ExcelGuru blog]
- List.Accumulate syntax and reference page [PowerQuery Doc on MS]

**Power Query examples for data cleanup:**

## 5 Responses to “Multiple Find Replace with Power Query List.Accumulate()”

Note: The text-formula above miss a -1. The video is correct.

ðŸ˜€ sorry, I made the exact same mistake as you did - initially - in the video. {0..3} is one thing, and Table.RowCount(replacements) -->3 items ={0,1,2} is another thing.

1st question : you've created a new column to put the replacements in. how ca we replace in the original column without creating a new one ?

2nd question : how can we replace the value in the entire cell and not only the text (Using ReplaceValue instead of text.Replace) ?

Thanks you in advance

Thank you for this! I was just doing an assignment where I was having to replace words with other words in my Excel sheet. We are starting with the basics in my class, so I know I don't have a huge list that I would need to find and replace, but this is something that could be useful down the road for me!

Hi

This is nearly perfect for my needs thank you, however I would like just the "replace" to be the result if possible please, I have tried in vain adjusting the formula without success.

Thank you in advance