Stock price thresholds
How long did a price stay above a level?
Section titled “How long did a price stay above a level?”This page finds the longest unbroken stretch of time in which a value stayed at or above a threshold. The technique rests on a local grouping, which groups only neighbouring rows that share a value, rather than every row in the table that shares it.
The example uses a company’s stock price, recorded once a minute for an hour from 09:00.

The price moves up and down around $50. The question is how many minutes in a row, at most, the price was at or above $50.
1. Query the table
Section titled “1. Query the table”- Click inside the table, and on Excel’s Data tab click From Table/Range. The Power Query Editor opens with a new query.
- Select the Time column, and set its data type to Time from the Data Type list on the Home tab.
- Select the Stock Price column, and set its data type to Decimal Number.
2. Flag the minutes at or above $50
Section titled “2. Flag the minutes at or above $50”- On the Add Column tab, click Custom Column.
- Name the new column Above50, and enter this formula:
[Stock Price]>=50The new column holds TRUE where the price is at or above 50, and FALSE where it is below.

3. Group the rows by the flag
Section titled “3. Group the rows by the flag”- Select the Above50 column, and on the Home tab click Group By.
- Choose Advanced, and check that Above50 is the only grouping column.
- Click Add aggregation until there are three output rows, and set them up as follows:
- Start, the minimum of the Time column
- EndTemp, the maximum of the Time column
- Minutes, a count of the rows

When you click OK, the table shrinks to two rows.

One row covers every minute at or above 50, and the other every minute below it. This is not yet the answer, because the question is about each separate stretch of time above 50, not about all such minutes taken together.
The fix is a local grouping, which starts a new group each time the value in Above50 changes. It is set by adding GroupKind.Local as the fourth argument of the Table.Group function.
4. Make the grouping local
Section titled “4. Make the grouping local”- In the formula bar, change the code for the Grouped Rows step from this:
Table.Group(#"Added Custom",{"Above50"},{{"Start", each List.Min([Time]), type time},{"EndTemp", each List.Max([Time]), type time},{"Minutes", each Table.RowCount(_), type number}})- to this:
Table.Group(#"Added Custom",{"Above50"},{{"Start", each List.Min([Time]), type time},{"EndTemp", each List.Max([Time]), type time},{"Minutes", each Table.RowCount(_), type number}}, GroupKind.Local)The table now has one row for each unbroken stretch, alternating between TRUE and FALSE.

5. Correct the end times
Section titled “5. Correct the end times”Each time in EndTemp is the start of the last minute in its stretch, so a one-minute stretch has the same start and end. Adding one minute to each end time makes a stretch end where the next one starts, which is easier to read.
- On the Add Column tab, click Custom Column, name the column End, and enter this formula:
= [EndTemp] + #duration(0,0,1,0)- Right-click the EndTemp column header, and choose Remove.
- Drag the End column so that it sits between Start and Minutes.

6. Keep the stretches above $50, and sort them
Section titled “6. Keep the stretches above $50, and sort them”Only the stretches at or above $50 matter, so the next step filters out the rest.
- Click the arrow in the Above50 column header, clear the tick next to FALSE, and click OK.

- Click the arrow in the Minutes column header, and choose Sort Descending, so that the longest stretch comes first.

7. Keep only the longest stretch
Section titled “7. Keep only the longest stretch”The longest stretch is now the top row, so the last step removes the others.
- On the Home tab, open the Keep Rows list and choose Keep Top Rows.
- Enter 1 as the number of rows, and click OK.

The query returns a single row. It shows that the price stayed at or above $50 for at most 6 minutes in a row, from 09:26 to 09:32.

8. The M code
Section titled “8. The M code”let //Load data from Excel Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], //Set column data types #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type time}, {"Stock Price", type number}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Above50", each [Stock Price]>=50), //Aggregate time ranges #"Grouped Rows" = Table.Group(#"Added Custom", {"Above50"}, {{"Start", each List.Min([Time]), type nullable time}, {"EndTemp", each List.Max([Time]), type nullable time}, {"Minutes", each Table.RowCount(_), Int64.Type}}, GroupKind.Local), //Add one minute to the values in the EndTemp column #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "End", each [EndTemp] + #duration(0,0,1,0)), //Remove the EndTemp column #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"EndTemp"}), //Move the End column in between Start and Minutes #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Above50", "Start", "End", "Minutes"}), //Filter to show only ranges where stock price is greater than or equal to 50 #"Filtered Rows" = Table.SelectRows(#"Reordered Columns", each ([Above50] = true)), //Sort by Minutes in descending order #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Minutes", Order.Descending}}), //Keep first row of the table #"Kept First Rows" = Table.FirstN(#"Sorted Rows",1)in #"Kept First Rows"This example follows one in Chris Webb’s Power Query for Power BI and Excel (Apress).