Power Automate Excel: Group Rows Automatically in a Flow

How-to basics · Updated November 21, 2026

Yes—but not with a built-in Power Automate action called “Group rows.” To create Excel’s collapsible row outlines automatically, use an Office Script that groups the required worksheet rows, then run it from a flow with the Excel Online (Business) action called Run script. If you mean grouping records by a shared value, such as totaling sales by region, use a PivotTable or another data-processing approach instead.

What “group rows” means in Excel

Excel row grouping is an outline feature. It adds expand and collapse controls beside row numbers so readers can hide or show detail rows. For example, a report might show a subtotal for each department while letting a reader collapse the employee-level detail underneath it. Grouping does not sort the data, combine records, or calculate subtotals by itself.

This distinction matters because “group rows” can also mean collecting records that share a category. If a list contains orders from several regions, arranging or summarizing those orders by region is a data task, not an outline task. A flow can handle either goal, but the right method differs: an Office Script can create an outline, while a PivotTable, formulas, or Power Query is usually a better fit for summaries.

What Power Automate can and cannot do

The Excel Online (Business) connector offers actions for working with workbook data, including adding or updating rows in tables. It does not provide a dedicated action for applying Excel’s Data > Group outline command. A flow that adds records to a table therefore does not automatically create collapsible sections.

Office Scripts provide the bridge. A script runs against a workbook and can use Excel’s scripting API to group a selected worksheet range by rows. Power Automate can call that script as part of a flow. This is useful when a report is refreshed on a schedule or when a file arrives in OneDrive or SharePoint and needs consistent formatting.

The distinction is between the flow’s orchestration and the script’s workbook editing. The flow can decide when to run and which file to target; the script contains the Excel-specific grouping operation. Availability depends on your Microsoft 365 plan, tenant settings, and administrator policies. If the Run script action or the Automate tab is missing, check those conditions before assuming the workbook is incompatible.

Create a simple Office Script

For a fixed report layout, first create a script that groups the rows you want. In Excel for the web, open the workbook, choose Automate > New Script, and replace the starter code with a script like this. This example groups worksheet rows 5 through 8 on a sheet named Report:

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheet("Report");
  sheet.getRange("A5:A8").group(ExcelScript.GroupOption.byRows);
}

The range identifies the rows to group; the byRows option tells Excel to create a row outline rather than a column outline. The script creates an outline level; it does not add a subtotal, label, or formula. Save it with a descriptive name, such as Group report details. Use a range that corresponds to the actual detail rows, not the header or subtotal rows.

This is a fixed-range example, not a complete reusable reporting system. If the report grows or shrinks, hard-coded row numbers may group the wrong records. A dynamic script needs to inspect the report, identify the relevant row boundaries, and handle cases such as an empty report or a missing category. Test that logic on a copy before putting it into a scheduled flow.

Connect the script to a flow

After saving the script, create a cloud flow in Power Automate and add a trigger that matches your process—for example, a scheduled trigger or a trigger for a file created in a supported OneDrive or SharePoint location. Then add the Excel Online (Business) action Run script, select the workbook, and choose the saved script.

  1. Confirm the workbook is stored in a location the Excel Online (Business) connector can access, such as OneDrive for Business or SharePoint.
  2. Choose a trigger and configure it so the flow runs only when the report is ready to be grouped.
  3. Add Run script, select the workbook and script, and supply any script parameters if your script defines them.
  4. Save the flow, run a controlled test, and open the workbook to verify the outline controls and grouped rows.

If the script is intended to work with more than one workbook, design it to accept parameters—such as a sheet name or range—and validate those inputs. The example above is deliberately simple and always targets one worksheet and range. Also consider whether a file-created trigger could fire before another process has finished writing the report; in that case, coordinate the steps so the script runs only after the workbook is complete.

Grouping rows based on changing data

Grouping rows whose positions change is more involved than grouping a fixed block. A script may need to read a category column, find consecutive runs of the same category, and group the detail rows within each run. For example, if a sorted report has a heading or subtotal row for each region followed by its orders, the script must determine which rows are details and which should remain visible.

Decide the layout rules before automating. Are categories already sorted together? Is there a header row for each section? Should blank rows be ignored? What happens when a category appears only once? These details affect the row boundaries. If the rows are not sorted, identical categories may appear in separate parts of the sheet, so the script could create several separate outlines rather than one group per category.

Repeated runs also deserve attention. A flow that applies grouping every time a file changes may encounter an outline created by an earlier run. Decide how the script should treat existing groups and test reruns, not just the first run. Avoid assuming that applying the same operation repeatedly leaves the worksheet unchanged; outline state and file changes should be checked in the workbook you actually use.

Choose between an outline, PivotTable, and other tools

Use an outline when the main goal is to let people collapse and expand detail rows in a worksheet report. Use a PivotTable when the goal is to summarize records by fields such as region, month, or product, with totals and interactive drill-down. Use sorting and filtering when readers mainly need to locate or view a subset of records.

  • Outline grouping: Best for a deliberately structured report with detail and summary sections. It changes how rows are displayed, not the underlying data.
  • PivotTable: Best for aggregating a changing list and exploring totals by category. It is often more robust than manually maintaining category sections.
  • Power Query: Best for repeatable data import and transformation steps, especially when the source needs cleaning or reshaping before analysis.
  • Sort or filter: Best when the requirement is to organize or temporarily narrow a list without building a hierarchy.

For example, if a manager wants to collapse each department’s employee rows beneath a department heading, an outline fits. If the manager wants a count of employees and total payroll by department, a PivotTable is generally the more direct reporting tool. These approaches can be combined: a flow can refresh or prepare data, while a separate report presents a PivotTable or a designed outline.

Tables, file locations, and reliability

Excel tables are useful for storing structured records and are commonly used with connector actions. Outline grouping, however, works on worksheet row ranges. If your source is a table, consider keeping that data in a dedicated sheet and building a separate report area or report sheet for grouped rows. This separation also helps avoid mixing a changing input list with presentation formatting.

The workbook must be reachable by the account and connector used in the flow. A file on a local computer is not automatically available to a cloud flow. Access permissions, organizational policies, and the location of the workbook can all affect whether the action can find and edit it. Use a test workbook in the same storage location and with the same connection before relying on a production flow.

Concurrent changes can also cause problems. If another flow, a person, or a connected process is editing the workbook while the script runs, the result may not reflect the final report or the action may fail. Avoid overlapping runs where practical, and ensure upstream actions finish before the script starts. For important reports, retain an original or recoverable copy and inspect flow run history when a run does not produce the expected outline.

Common problems and practical checks

  • Run script is unavailable: Check Microsoft 365 eligibility, administrator settings, the selected connector, and whether Office Scripts are enabled for your account.
  • The wrong rows are grouped: Confirm the worksheet name, the row numbering, and whether inserted title or header rows shifted the report.
  • The group appears but does not summarize: That is expected. Grouping creates expand-and-collapse controls; it does not calculate a subtotal.
  • The flow cannot find the script or workbook: Confirm that the script is saved and that the action uses the correct workbook location and connection.
  • A later run behaves differently: Test what happens when the workbook already contains an outline and when the report has a different number of rows.

For manual checking in Excel for Microsoft 365 on Windows, select the detail rows and use Data > Outline > Group. Excel displays outline controls beside the row numbers. Excel for the web has its own ribbon layout, and available commands can depend on the workbook and account. Manual grouping is useful for a one-off report, but it does not make a Power Automate flow repeat the task automatically.

Frequently Asked Questions

Can Power Automate group Excel rows without a script?
The Excel Online (Business) connector does not have a dedicated row-outline grouping action. For automatic outline grouping, use Run script with an Office Script, or choose a different reporting method if you need grouped summaries rather than collapsible rows.

Can a flow group rows with the same value?
Yes, a script can be designed to find matching values and group the corresponding row ranges. The data layout matters: sorting, headers, blank rows, and repeated categories determine how the script should identify each group.

Does grouping rows create subtotals?
No. Grouping only adds an outline that lets users hide or show rows. Use Excel’s subtotal/reporting features or a PivotTable if you need calculations such as counts or sums.

Can I group rows inside an Excel table?
Outline grouping is a worksheet-row feature, not a table grouping or aggregation feature. For predictable reporting, keep source records in a table and create the collapsible layout in a separate report area, then test the design in your target workbook.

Can the flow group a different number of rows each time?
Yes, but the script must determine the correct row boundaries at runtime instead of relying on fixed row numbers. Include checks for empty data, changing headers, and existing outlines, and test with reports of different sizes.

Can Excel for the web create and show these groups?
Excel for the web can work with many outline features, but available commands and Office Scripts access can vary by account and rollout. Create and test the script in an environment where the Automate tools and the Power Automate Run script action are enabled.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.