Skip to content

Stock price thresholds

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.

An Excel table with a Time column running from 09 in one-minute steps and a Stock Price column with values close to 50

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.

  • 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.
  • On the Add Column tab, click Custom Column.
  • Name the new column Above50, and enter this formula:
[Stock Price]>=50

The new column holds TRUE where the price is at or above 50, and FALSE where it is below.

The query with a new Above50 column showing TRUE or FALSE for each minute, depending on whether the stock price is at least 50

  • 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

The Group By dialog in Advanced mode, grouping by Above50, with three aggregations: Start as Min of Time, EndTemp as Max of Time and Minutes as Count Rows

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

The grouped table with two rows, one for TRUE and one for FALSE, each with the earliest time, the latest time and the number of minutes

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.

  • 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.

The locally grouped table with 28 rows alternating between TRUE and FALSE, each showing the start time, end time and length in minutes of one stretch

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.

The table with an End column between Start and Minutes, where each stretch now ends at the time the next one starts

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.

The filter menu for the Above50 column, with TRUE ticked and FALSE cleared

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

The menu for the Minutes column, with Sort Descending highlighted

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 Keep Top Rows dialog with 1 entered as the number of rows

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.

The final query with one row: Above50 TRUE, Start 09, End 09 and Minutes 6

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).