Syncing is one of the most complex challenges in all platforms. We all suffered from issues where our files don’t sync appropriately on OneDrive, or our phones are out of sync with our email servers, but that doesn’t mean it’s impossible. Today, I want to tackle an interesting question from the Power Automate community: they want to sync Excel to a SharePoint list.

Let’s try to build the template, but as always, I’ll leave the finished template so that you can download it and try it for yourself. You can also check other templates in my cookbook section.

Synchronize Excel and a SharePoint list

This template provides a full Flow that looks for new Excel files created in a folder, reads the information, and then synchronizes it to a SharePoint. 

You can also find an example of the Excel file here.

Strategy

As always, let’s start with the strategy and the assumptions for this exercise.

Syncing strategy

First, we’ll always sync Excel to a SharePoint List, but not the other way around. In the next version of the template, we can think about syncing data both ways but, for now, let’s keep things simple. We will trigger the Flow, check Excel’s data, match it with the items in the SharePoint list, and update (or create) the values.

We’ll override changes in SharePoint.

We need to define who is the “master” of the information. We could check by date and other elements, but this would make things extremely complex, so let’s start with the simple first. Excel will always contain the “source of truth,” meaning we’ll eventually override the data regardless of what’s done in the SharePoint List. So all changes need to be in Excel first, and only then will they be migrated to SharePoint.

Also, we won’t check if the values are different before updating them, so we’ll update all values even if they don’t change.

We will create a list of items if they don’t exist.

If the value doesn’t exist in SharePoint, we’ll create them. This will help us keep things similar on both sides.

The comparison key

We need a comparison key that is unique and identifies things on both sides. For the sake of our exercise, we’ll use the ID, but you can change it to be any field. The fields don’t even need to be named the same way, so feel free to change that.

The trigger

The trigger is currently “Manually trigger a Flow,” but you can easily change it for any other that makes sense for you. For example, you can have a “Recurrence” trigger that periodically fetches the information and updates the SharePoint list items.

When create we need to update the Excel ID

Since SharePoint creates the IDs automatically, we can’t force them when inserting the data. It’s a problem because we use the ID as the comparison key, so when we create a new item, we need to update the Excel ID. Otherwise, we’ll keep creating new items for the same row in Excel, and we don’t want that.

You don’t need to fetch all items.

If you have items that you don’t want to sync, you can filter them using OData queries. Please be sure that you have equivalent queries on both Excel and SharePoint. If you only set up e, the values will be synced to the other, and we don’t want that.

It’s pretty helpful to limit the values that you want to sync. The fewer values, the faster your Flow runs, so think of this if you can.

The template

Now that we have all the strategies in place, let’s check the template. We’ll start by defining the Excel and SharePoint lists. Both have the same structure, but this is not mandatory. You can have different names for the fields as long as you map them in the template. Also, you can have more columns on both sides; make sure that they are optional; otherwise, the sync will fail with not enough data. Here’s the full view of the template.

Let’s look at some sections and things you should know.

Get all items to sync

One would be tempted to do the following:

  1. Fetch all Excel elements
  2. Do a SharePoint “Get Items” or Get Item action and search for the corresponding ID for each.
  3. Insert or update depending on if the item exists or not.

The strategy works, but it will generate a lot of calls to SharePoint. If there are 100 rows in Excel, we’ll call SharePoint at least 100 times. But how about calling it only once and filter the data that we have? Much more efficient. That’s what we’re doing here. At the start of the Flow, we’ll get all elements from SharePoint and then filter them when we need them in the Flow. To do that, we have the Apply to Each action with a Filter Array action.

OK, there are many arrows here, but I wanted you to understand where things come from. By looking at the structure, we can understand what’s happening. If you want to understand in more detail, I wrote another article focus on how to “How to parse an array more efficiently“.

For Each element in the Excel (apply to each), filter the array (items from the SharePoint list) comparing the ID of the SharePoint item with the ID from the Excel.

There’s a small detail that we have to take into consideration. The ID comes from Excel as a string, so we need to use the “int” function to convert it to an integer. Here’s the formula:

int(items('For_each_item_in_Excel')?['ID'])

Nothing too complex.

Do we have an item in the SharePoint list?

Now that we filter the array, we need to check if we have an item or not. We need to use the Length function that will return the number of items found. Since we are comparing by ID, we will either find one or zero. With this, we can decide to create (if we get zero) or update the item (if we find one).

Excel dates

Now that we know what to do, we need let’s understand Excel dates. Excel stores the dates internally as numbers, and they are returned from the action as numbers. I detail how to convert excel numbers to date and date to the number on other articles and explain why 1900 is not a leap year. Here’s the short explanation. Microsoft has a “feature” in Excel that considers 1900 as a leap year. It’s not a leap year, and this is a well-known bug that Microsoft won’t be able to fix. But since we need to parse the numbers to dates, we need to consider this. Long story short, the number represents the number of days since the 1st of January 1900, so the formula is the following:

addDays(outputs('Reference_Date'),sub(int(item()?['Date']),2))

We convert the date of the item that we’re parsing now and convert it to an integer since Excel returns it as a string. After that, we use the “sub” function to take two from the number (one for the “bug” and another because the start date is one, not 0). After that, use the addDays function to generate the final date.

We calculate this before the “Condition” action because it will be used in both cases.

Update the data

If we can find an item in the SharePoint list, then we need to update it. To do it, it’s simple. We only need a SharePoint “Create item” action to add the values from Excel:

As I explained above, we need to update Excel’s ID since we can’t control the ID that SharePoint creates in the previous step. We can, however, get that ID and update Excel with the “Update a row” action.

With this, we “link” the items together.

Update the data

Updating the data is much more straightforward. We only need a SharePoint “Update item” action with the data from Excel and the converted date.

Final thoughts

There is a lot to improve here, and I’m always looking for ways to improve. Since we’re doing a sync Excel to a SharePoint list, things can get tricky, but I think this is a nice compromise between being straightforward and efficient. I’m sure I’ll improve this template over time, but please use it and submit changes if you think other things should be done.

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and be sure to check out other Power Automate-related articles here.

Photo by Nick Fewings on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

2 thoughts on “Power Automate: Sync Excel to a SharePoint list

  1. Hello Manual,
    Thanks for the sync Excel<>SPO.
    Q: how much time does this solution take for 8K rows?
    Gr. Lex

    1. Hi Lex,
      Unfortunately, it will take some time. The actual bottleneck here is Excel. SharePoint deals with parallel actions quite well, but Excel was not built for things like this.
      We could enable parallel executions, but then we would run the risk of incorrectly inserting or updating the data since we would not have control of the order.
      Can you please find a timeslot to do the sync and let it run for a while. Then, check how much time it takes, and then we have more information to make decisions.
      If it’s too long, please let me know, and we’ll do some testing and see where we can optimize.
      Sounds good?
      Cheers
      Manuel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: