Skip to main content

Notifications

Power Automate - Building Flows
Answered

Compare 2 excel and highlight the row with different quantity in the second excel

(0) ShareShare
ReportReport
Posted on by

Hi I am very new to power automate please help!!

 

I have 2 excels with delivery schedule. I would like to identify the same row and see if the 'Qty' column is the same, if not, highlight that row.

 

For example, 

 

The schedule will include the delivery requirement for today and tomorrow. That is, 

Delivery schedule on 06-30 will have schedule for 06-30 and 07-01

caratsweet_0-1688974969852.png

Delivery schedule on 07-01 will have schedule for 07-01 and 07-02

caratsweet_1-1688974988021.png

 

I would like to find out the changes of Qty column during the overlap date, in this example is 07-01, and see if it change compare with the previous day. If yes, highlight.

 

The ideal result would be below:

caratsweet_2-1688975527381.png

 

how can I achieve this with power automate?

 

My current workflow is

caratsweet_4-1688975775547.png

My idea is to locate each row using columns (Material code, Time, Date) with same data other than Qty first. Then, we check if Qty is the same in these 2 excel. If not, highlight the row.

 

Thank you so much for the help in advance.

 

 

 

  • HC-14122317-0 Profile Picture
    HC-14122317-0 2 on at
    Compare 2 excel and highlight the row with different quantity in the second excel
    Hi rzaneti, 
     
    Thank you for your solution that I really need it. 
     
    I have one more question need your assistance. The second script requires a parameter of "deliveries". You posted an image about how to pass the parameter. However, the image is no longer accessible. Would you post either the express or the image for me. Much appreciated.
     
  • Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @rzaneti ,

     

    I have had another solution for the code, your detailed explanation helps a lot too!! 

     

    Thank you so much!!

  • rzaneti Profile Picture
    rzaneti 3,321 on at
    Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @Anonymous ,

     

    Here are your answers:

     

    rowIndex, i and ind

    The "++" operator in JavaScript means "value+1". So when we use rowIndex++, it is the same thing of use rowIndex = rowIndex + 1. It is just a quicker way to write it 🙂

     

    We are using it to make sure that, in each 'For loop' run, the value from rowIndex change. When we are declaring the 'For loop', it's syntax already require us to declare the index variable (in our case, "i" and "ind") and to determine what action will be taken after each loop iteration (in our case, "i++" and "ind++").

     

    For loop syntax in Javascript:

    for([variable]; [condition]; [action_after_iteration]) {

    [Action to be repeated in each loop iteration] 

    }

     

    Our for loop:  

    for (let i = 0i < 100i++){

    //code

    }

     

    So we are saying that "i" values zero, but after each iteration, it will values "i+1", and it will repeats while "i" is lower than 100 (actually we have other conditions inside the curling brackets that exit the loop, but if we did not have them, the loop would run only until i=99).

     

    Error in if statement

    In your if statement, you are comparing the whole row from rangeValue and parsedArr. If I remember properly about the code, you are repeating this task for all rows from old and new table, and basically testing "all rows against all rows". If the rows match entirely, you paint them in white, if not (which will be the most part of the iterations), you paint them in yellow. 

     

    Try the to use the following code. Notice that the the red part is out of the for(ind) loop, but inside the for(i). 

     

    let isDifferent = false

    ws.getRangeByIndexes(rowIndex, 0110).getEntireRow().getFormat().getFill().setColor("white")

     

    for (let ind = 0; ind < parsedArr.length; ind++) {

     

    if (rangeValue[0]!== parsedArr[ind]){

    isDifferent = true

    }

    }

    }

    if(isDifferent){

    ws.getRangeByIndexes(rowIndex, 0, 1, 10).getEntireRow().getFormat().getFill().setColor("yellow")

     

     

    --

     

    If it not work, I will run some additional tests. 

  • Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @rzaneti ,

     

    Thank you so much for the help again!

     

    can I ask for the highlight_row script, why rowIndex needs to ++ at the end,

     }
     }
    
    
     rowIndex++
     }
    }

    but both ind & i don't need to ++ in the For Loop?

     

    Thank you so much in advance!

     

    Also, I would like to highlight the different quantity record in a reverse way. In If condition, i will 'highlight' the same record with white color.

    In Else condition, I will highlight the ones not fulfilling the first condition (which are the diff records) with yellow.

    Edited the code a bit as below.

     

    function main(workbook: ExcelScript.Workbook, deliveries: object) {

     

        const parsedArr = deliveries['deliveries']

     

        console.log(parsedArr)
        const ws = workbook.getWorksheet('DeliverySchedule')

     

        let rowIndex = 1

     

        for (let i = 0; i < 100; i++) {
            const rangeValue = ws.getRangeByIndexes(rowIndex, 0110).getValues()
            if (rangeValue[0][0] === "") {
                break
            }



          for (let ind = 0; ind < parsedArr.length; ind++) {



            if (rangeValue[0]=== parsedArr[ind]) {
              ws.getRangeByIndexes(rowIndex, 0110).getEntireRow().getFormat().getFill().setColor("white")
              console.log(rangeValue[0])
            }
            else {ws.getRangeByIndexes(rowIndex, 0110).getEntireRow().getFormat().getFill().setColor("yellow")}
          }
          rowIndex++
      }
    }

     

    When I run this code, I am having all the rows highlighted in yellow no matter it is the same or diff records. I could not find out the bug. Could you please help look through the codes? Thank you so much! and is there a way good for debugging for Office Script?

     

    function main(workbook: ExcelScript.Workbook, deliveries: object) {

     

        const parsedArr = deliveries['deliveries']

     

        console.log(parsedArr)
        const ws = workbook.getWorksheet('DeliverySchedule')

     

        let rowIndex = 1

     

        for (let i = 0; i < 100; i++) {
            const rangeValue = ws.getRangeByIndexes(rowIndex, 0110).getValues()
            if (rangeValue[0][0] === "") {
                break
            }



          for (let ind = 0; ind < parsedArr.length; ind++) {



            if (rangeValue[0]=== parsedArr[ind]) {
              ws.getRangeByIndexes(rowIndex, 0110).getEntireRow().getFormat().getFill().setColor("white")
              console.log(rangeValue[0])
            }
            else {ws.getRangeByIndexes(rowIndex, 0110).getEntireRow().getFormat().getFill().setColor("yellow")}
          }



          rowIndex++
      }
    }
  • rzaneti Profile Picture
    rzaneti 3,321 on at
    Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @Anonymous ,

     

    I'm happy to hear that everything worked fine!!

     

    If you need any help to integrate it into the loop, let me know and it will be my pleasure to help.

     

    About your questions:

     

    1. You are correct. I did not include details about the code in the previous post, but the 100 inside the 'for (let i = 0; i < 100; i++) ' is limiting the quantity of loops to 100 rows. You can replace the '100' for a higher number, this is not a problem. The code already have a condition to escape from the loop if it achieves an empty cell in column "A", but it is a good practice to keep a limit of number of times that the code block will run to prevent any infinite loops.

     

    2. The current code is capturing the range between "A1" and "E?", being "?" the last occupied row in the sheet. The first thing that you need to do is to change the "5" in "const rangeValue = ws.getRangeByIndexes(rowIndex, 0, 1, 5).getValues()" for the quantity of occupied columns that you actually have in your file. Assuming that you have 13 columns, this line of code would look like this: "const rangeValue = ws.getRangeByIndexes(rowIndex, 0, 1, 13).getValues()"

     

    The if statement that you shared is exactly comparing if the material code, the date, the time and the vendor are the same (in JavaScript, we use the '===' notation to do equality comparisons), and if the quantity is different (in JavaScript, we use the '!==' notation to do inequality comparisons).

     

    To define the compared columns, we change the value in the second square brackets of each statement: it corresponds to a zero-indexed position of the column. So when we have rangeValue[0][0] === parsedArr[ind][0], we are comparing the values from the first column (zero is the first index), which is the column "A"; when we have rangeValue[0][3] === parsedArr[ind][3], we are comparing the values from the forth column (3 is the forth index), which is the column "D", and so on.

     

    So after you change the rangeValue constant to include more columns, you can compare them by changing this second index in the if statement. To include new conditions inside the if statement, you just need to add the "&&" notation. 

     

    Where to learn more about Office Scripts?

    Office Scripts is based in TypeScript, which is a programming language based in JavaScript. Specifically for looping purposes, the syntax used in Office Scripts is exactly the same from JavaScript, and fortunately there is a lot of online tutorials and courses about JavaScript. 

     

    I would recommend you the following links as reference about these topics, but have in mind that you can find a ocean of other sources for JavaScript/TypeScript learning (blogs, YouTube channels, forums...):

     

    Office Scripts docs: https://learn.microsoft.com/en-us/office/dev/scripts/

    JavaScript docs in @W3Schools: https://www.w3schools.com/js/

    JavaScript docs in Mozilla: https://developer.mozilla.org/en-US/docs/Web/JavaScript 

     

    Away from the official docs, there are not so much references specifically about Office Scripts. However, the Power Automate Community is very active and can help you with any issue with this tool!!

  • Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @rzaneti 

     

    I have applied your solution and it runs quite well.

    caratsweet_0-1689234412594.png

    Thank you so much for all your help and support. 

     

    I will try to integrate this process into a loop workflow to make it apply to each vendor.

     

    Just more for a learning purpose, can i ask some questions?

    1. 

    for (let i = 0; i < 100; i++) 
    does this specify how many rows we would like to compare?
     
    2.
                if (rangeValue[0][0] === parsedArr[ind][0] && rangeValue[0][2] === parsedArr[ind][2] && rangeValue[0][4] === parsedArr[ind][4] && rangeValue[0][5] === parsedArr[ind][5] && rangeValue[0][6] === parsedArr[ind][6] && rangeValue[0][7] === parsedArr[ind][7] && rangeValue[0][8] === parsedArr[ind][8] && rangeValue[0][3] !== parsedArr[ind][3]) {

     

                    ws.getRangeByIndexes(rowIndex, 0110).getEntireRow().getFormat().getFill().setColor("yellow")
                    console.log(rangeValue[0])
     
    Since I am actually comparing more than 5 columns to locate the same row in different date's file
    , (make sure the other 8 columns are the same) is there some reference for me to learn how to do loop in Office Script for this purpose?
     
    Thanks so much for all the help again!!
  • Verified answer
    rzaneti Profile Picture
    rzaneti 3,321 on at
    Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @Anonymous ,


    Thanks for the additional context!!

     

    To try to make things less complex, I will share in this post only a part of the solution (the Office Scripts creation and the integration with Power Automate for only one vendor). After you have this first part solved, we can move forward to the recurrent trigger and the vendors file mapping.

     

    It is possible that you find some errors when replicating it in your environment: don't worry, you can share those errors and we will help you to solve them. Also, after we finish this solution, I can share more detailed instructions about what each line of code does. 

     

    Overview on the files:

    I have a folder called 'Delivery Schedule' (highlighted in blue). Inside it, there are subfolders by date, in the 'yyyy-MM-dd' format (highlighted in yellow). Those subfolders contains Excel files from each vendor (highlighted in green). For this first part of the example, I'm using 2 subfolders only ('2023-07-10' and '2023-07-11'), with 2 vendor files inside it (called 'File1' and 'File2').

     

    rzaneti_1-1689163845380.png

     

     

    rzaneti_0-1689163767327.png

     

    For now, we will be using only the 'File1' from both dates, which structure is represented below (first image from July 10th, second from July 11st):

    rzaneti_2-1689164032328.png

     

    rzaneti_3-1689164055825.png

     

    Note: During this post, I will be referring to the '2023-07-10' Excel file as 'Old file' and to the '2023-07-11' Excel file as 'New file'

     

    Step 1 - Create your "reading" Office Script:

    We will first create a Office Script to read the whole data from the Old File and return it to Power Automate. So the first step is to create an Office Script - you can do it in any Excel file, but I recommend you to do it in your Old File. 

     

    To create it, go to 'Automate' tab in your Excel (highlighted in yellow), click 'New Script' (highlighted in green) and then a sidebar may appear (highlighted in blue):

    rzaneti_4-1689164505834.png

     

    If you have any trouble to open the Code Editor, you can ask here or check for more detailed instructions in the following links: 

     

    http://digitalmill.net/2023/06/19/get-started-with-office-scripts/

    https://learn.microsoft.com/en-us/office/dev/scripts/overview/excel

     

    Your code editor may look different from mine: don't worry, it is just a style configuration 🙂

     

    You can replace the whole text inside the Code editor for the following code:

    function main(workbook: ExcelScript.Workbook) {
     const ws = workbook.getWorksheet('Sheet1')
    
    
     let rowIndex = 1
    
    
     for(let i = 0; i< 50; i++){
     const rangeValue = ws.getRangeByIndexes(rowIndex, 0, 1, 1).getValue()
     if (rangeValue === ""){
     break
     }
     
     console.log(rangeValue)
     rowIndex++
     }
    
    
     const allValues = ws.getRangeByIndexes(0, 0, rowIndex+1, 4).getValues()
    
     console.log(allValues) //you can remove this line after the first test
    
     return {"deliveries": allValues}
    }

     

    Click in 'Run'. A dropdown may appear in the 'Output' below the 'Code editor'. Expand the dropdown to make sure that the data is being captured, specially the Material Code and Qty. If the data is being correctly displayed, you can remove the code line 'console.log(allValues)'.

     

    Note: in the code above, we are assuming that your occupied range starts in cell 'A1', and your sheet name is 'Sheet1'. Let me know if something is different from your file template, as it will require some changes in the code. 

     

    Step 2 - Create the "highlight" Office Script:

    Now you will repeat the same process in the New File, but entering the following code:

    function main(workbook: ExcelScript.Workbook, deliveries:object) {
     
     const parsedArr = deliveries['deliveries']
    
     console.log(parsedArr)
     const ws = workbook.getWorksheet('Sheet1')
    
     let rowIndex = 1
    
     for (let i = 0; i < 10; i++) {
     const rangeValue = ws.getRangeByIndexes(rowIndex, 0, 1, 5).getValues()
     if (rangeValue[0][0] === "") {
     break
     }
     
    
    
     for (let ind = 0; ind < parsedArr.length; ind++){
    
    
     if (rangeValue[0][0] === parsedArr[ind][0] && rangeValue[0][2] === parsedArr[ind][2] && rangeValue[0][3] === parsedArr[ind][3] && rangeValue[0][1] !== parsedArr[ind][1]) {
     
     ws.getRangeByIndexes(rowIndex, 0, 1, 5).getEntireRow().getFormat().getFill().setColor("yellow")
     console.log(rangeValue[0])
     }
     }
    
    
     rowIndex++
     }
    }

     

    Here you do not need to 'run' the code, as it will need the data from the other file as input to work. 

     

    Step 3 - Creating Power Automate flow:

    Create a flow with a manual trigger (we will replace the trigger later, to make it run in a daily basis). Add a 'Run Script' action from 'Excel' connector, and enter the Old File path (highlighted in yellow) and select the "reading" script from the dropdown (highlighted in green):

     

    rzaneti_5-1689166273005.png

     

    You can save the flow and run it just for test purposes. If everything is running correctly, you may receive this output from the action:

    rzaneti_6-1689166398733.png

     

    Then you can add a second 'Run script' action, but this time selecting the New File and the "highlight" script. Different from the first 'Run script', in the New File one you will see an additional field called 'deliveries', where you may enter the input for your Office Script. Enter the 'result' from the first 'Run script' action as dynamic content (highlighted in blue): 

    rzaneti_7-1689166548595.png

     

    Step 4 - Running the flow:

    Save your flow and run it. It may take some seconds to run. After that, your New File will look like this: 

    rzaneti_8-1689166622114.png

     

     

    Conclusion

    As mentioned before, it is possible that you find some trouble during this process, specially in this initial part. Please, do not hesitate and posting your questions/errors here. Once you achieve the result from Step 4, I will send a new answer with the flow structure to run all vendors files and apply the scripts in a daily basis.

  • Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @rzaneti 

     

    To add on, yes, the data is getting from SAP daily which is not a table format originally.

     

    To clarify a bit, the date for a specific row will not change, for example, delivery schedule for Material 111 is on Jul 11, and it will stick with this date Jul 11 until it is delivered. 

     

    However, since the time range included in each file will be always from today's date to the following 3 months, the date will be changed slightly. For example, on Jul 11, the date will be Jul 11 to Oct 11. On Jul 12, the date will be Jul 12 to Oct 12.

     

    But at the end, only the following 7 days delivery need to compare to see if Qty changes. That is, on Jul 11, will only compare Jul 11-18 on yesterday's excel with Jul 11-18 on today's excel. The reason is these are the most urgent delivery.

     

    Hope this is clear.. Thank you so much in advance again!

     

     

  • Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @rzaneti 

     

    Thank you so much for the support! That really means a lot to me!

     

    About your question, actually the only changing part will be 'Qty'. The row will be highlighted when Qty changes.

     

    For example, in the delivery schedule on Jul 10, I plan to have 20 Qty for Material 111 to be delivered on Jul 13. However, on Jul 11, I change my mind, I would like to deliver only 15 Qty for Material 111 on Jul 13. Then this delivery of 15 Qty for Material 111 should be highlighted.

     

    If the Qty for Material 111 is still 20, we dont need to highlight.

     

    My idea is that, using all the columns other than Qty, including material code, time, date and vendor name, to locate the specific row, and then compare the Qty. 

     

    I am not sure if this is achievable.

     

    Thank you so much for the help again!

     

    I hope I explain it clearly.. I am more than happy to explain more if needed.

     

  • rzaneti Profile Picture
    rzaneti 3,321 on at
    Re: Compare 2 excel and highlight the row with different quantity in the second excel

    Hi @Anonymous ,

     

    About the file location, this a good news. We can access the files from today and yesterday based on the folder name. 

     

    About the table, I imagined that this file had any manual process before this Power Automate task, or was based on a template, and would recommend format as a table during this previous step. As it is automatically generated from SAP, it seems to be better work without the table. 

     

    About your solution, I intend to draft a solution in the next few hours connecting Power Automate and Office Scripts and share it here. Just to make sure that I'm understanding it correctly: the only fields that can change are the 'Material Code' and the 'Qty,' correct? There is no possible change in time, date and vendor name. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Kickstarter Events…

Register for Microsoft Kickstarter Events…

Tuesday Tip #12 Start your Super User…

Welcome to a brand new series, Tuesday Tips…

Tuesday Tip #13 Writing Effective Answers…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 144,940

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,523

Leaderboard