web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How Power Automate Des...
Power Automate
Answered

How Power Automate Desktop can dynamically identify the first row & last column with data from excel?

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi,

 

How I am new to Power Automate Desktop, and my only experience at the moment is doing recording. I have an excel file and I want to evaluate the file to get the first row and last column that has data.

 

Requirement:

1. Get First Row with data to get the cell address eg A1

2. Get the Last column with data to get the cell address eg M47

Note: Excel files from SharePoint/Onedrive has dynamic ranges for the requirement. 

 

My goal is put there cell address in a variable.  I wonder if this is even possible with Power Automate Desktop?

 

Cheers,

kbw

 

 

I have the same question (0)
  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    There is an Excel command for “Get Last Row/Column.”  I caution you that it does not look for a population in the cell.  It does the same as if you were to hit Control + End. Wherever that takes you is what this command will result in. That means if you have extra blank but formatted cells below your data, then you will have to find a way to clear out that formatting prior to running this command. 

  • Verified answer
    Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hello @Anonymous 

     

    Your requirement #1 is a bit difficult because there is no such preset action, while #2 is easy.

     

    If you set [Retrieve] as "All available values from worksheet" in action "Read from Excel worksheet", you will get Datatable which having basically the same layout as Excel worksheet.

     

    shindomo_5-1633993986287.png

     

    For example, my source Excel worksheet has data in between B3 and F6 as below:

    shindomo_0-1633993565591.png

     

    The result of Datatable as below if you use "All available values from worksheet" option to read from Excel:

    shindomo_1-1633993639711.png

     

    So far, you can retreive properties %ExcelData.RowsCount% and %ExcelData.Columns.Count% both get 6 in the example above.

     

    To identify the first cell with the data (which isn't empty), you have to create a nested For Each loop. One for row direction and one for column direction. As a result of loops below, you will get variable %MinDataRowIndex% = 3 and %MinDataColumnIndex% = 2 in this example.

     

    shindomo_2-1633993719658.png

    shindomo_3-1633993754028.png

     

    Then you can convert from row index and column index to cell address by using the ADDRESS() function in Excel formulas.

    ADDRESS function (microsoft.com)

     

    shindomo_4-1633993798339.png

     

    BEFORE:

    shindomo_6-1633995568978.png

     

    AFTER:

    shindomo_7-1633995581515.png

     

    You can copy & paste the example code below into your PAD Flow Designer.

     

    System.TerminateProcessByName ProcessName: $'''EXCEL'''
    Excel.LaunchAndOpen Path: $'''C:\\Users\\User01\\Documents\\Customers.xlsx''' Visible: False ReadOnly: True LoadAddInsAndMacros: False Instance=> ExcelInstance
    Excel.ReadAllCells Instance: ExcelInstance ReadAsText: False FirstLineIsHeader: False RangeValue=> ExcelData
    Excel.Close Instance: ExcelInstance
    # Get address of the first cell having data (is not empty)
    SET RowIndex TO 1
    SET ColumnIndex TO 1
    SET MaxDataRowIndex TO ExcelData.RowsCount
    SET MaxDataColumnIndex TO ExcelData.Columns.Count
    LOOP FOREACH CurrentDatarow IN ExcelData
     LOOP FOREACH CurrentItem IN CurrentDatarow
     IF IsNotEmpty(CurrentItem) THEN
     SET MinDataRowIndex TO RowIndex
     SET MinDataColumnIndex TO ColumnIndex
     EXIT LOOP
     END
     Variables.IncreaseVariable Value: ColumnIndex IncrementValue: 1 IncreasedValue=> ColumnIndex
     END
     IF (MinDataRowIndex <> 0 AND MinDataColumnIndex <> 0) = $'''TRUE''' THEN
     EXIT LOOP
     END
     Variables.IncreaseVariable Value: RowIndex IncrementValue: 1 IncreasedValue=> RowIndex
     SET ColumnIndex TO 1
    END
    Display.ShowMessage Message: $'''MIN : [%MinDataRowIndex%][%MinDataColumnIndex%]
    MAX : [%MaxDataRowIndex%][%MaxDataColumnIndex%]''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed
    # Get cell address using ADDRESS() formula in Excel
    Excel.Launch Visible: False LoadAddInsAndMacros: False Instance=> ExcelInstance
    Excel.WriteCell Instance: ExcelInstance Value: $'''=ADDRESS(%MinDataRowIndex%,%MinDataColumnIndex%,4)''' Column: 1 Row: 1
    Excel.WriteCell Instance: ExcelInstance Value: $'''=ADDRESS(%MaxDataRowIndex%,%MaxDataColumnIndex%,4)''' Column: 2 Row: 1
    Excel.ReadCell Instance: ExcelInstance StartColumn: 1 StartRow: 1 ReadAsText: False CellValue=> MinCellAddress
    Excel.ReadCell Instance: ExcelInstance StartColumn: 2 StartRow: 1 ReadAsText: False CellValue=> MaxCellAddress
    Excel.Close Instance: ExcelInstance
    Display.ShowMessage Message: $'''MIN : %MinCellAddress%
    MAX : %MaxCellAddress%''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed

     

    Thank you.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thanks @MichaelAnnis for your inputs 🙂

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thanks @Anonymous thanks I will apply this on my last column. 

    Perhaps I will just make sure that my data should be in A1.

  • FreeRangeDingo Profile Picture
    114 on at

    @Anonymous I don't seem to get these variables as outputs from the Read from Excel worksheet.  The only variable output from that action is %ExcelData%.  Do I need to do something to get these other outputs?  %ExcelData.RowsCount% and %ExcelData.Columns.Count%

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    If you use the "Get First Free Row/Column" action, it essentially does the same as you pressing Control + End, and then gives you the row and column for that cell.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hello @FreeRangeDingo 

     

    If you click {x} (Select variable) button on the right of configuration panel of actions, such like "Display message" for example, you can see properties such like %ExcelData.RowsCount% and %ExcelData.Columns.Count% under %ExcelData% as screenshot image as below.

     

    shindomo_0-1643240261733.png

     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Haque Profile Picture

Haque 592

#2
Valantis Profile Picture

Valantis 340

#3
11manish Profile Picture

11manish 284

Last 30 days Overall leaderboard