Skip to content

Growth percentages from previous days

This page adds a column that shows the percentage change in sales from one day to the next. The technique is to work out each row’s previous date, and then merge the query with itself to bring the previous day’s sales onto the same row.

The example starts from this Excel table of daily sales.

An Excel table with Date and Sales columns, holding six days of sales from 1 to 6 January 2020

  • Click inside the table, and on Excel’s Data tab click From Table/Range. The Power Query Editor opens with a new query.

Data from an Excel table does not always arrive with a date type, so set it explicitly.

  • Select the Date column, and set its data type to Date from the Data Type list.

Next, add a column that holds the day before each date.

  • On the Add Column tab, click Custom Column.
  • Type PreviousDate as the new column name, and enter this formula, which subtracts a duration of one day from the date:
= [Date] - #duration(1,0,0,0)

The Custom Column dialog with the new column named PreviousDate and the formula = [Date] - #duration(1,0,0,0)

  • Click OK. The table now has a PreviousDate column beside each date.

The query with Date, Sales and PreviousDate columns, where each previous date is one day before the date, starting from 31/12/2019

The previous day’s sales are in the same table, on another row. Merging the query with itself, matching PreviousDate to Date, brings them onto the current row.

  • On the Home tab, click Merge Queries. The Merge dialog opens with the current query in the upper half.
  • In the list for the second table, choose the current query again, which is marked (Current). In this example it is called PercentageGrowthPrevDayInput.
  • Select PreviousDate in the upper table and Date in the lower table, leave the join kind as Left Outer, and click OK.

The Merge dialog joining PercentageGrowthPrevDayInput to itself, with PreviousDate selected in the upper table and Date in the lower, matching 5 of 6 rows

The merge adds a column of tables, named Added Custom after the step it merged with.

The query with a new Added Custom column whose cells each contain the word Table

  • Click the expand icon in the Added Custom column header, clear every tick except Sales, and click OK.

The expand menu for the Added Custom column, with only Sales ticked

A new column called Sales.1 shows the sales for the date in PreviousDate.

  • Double-click the Sales.1 header and rename it PreviousDateSales.

The first date has no previous day in the table, so its PreviousDateSales is null. The merge also changes the order of the rows, which is put right later.

The query with a PreviousDateSales column holding the previous day’s sales, and null on the row for 01/01/2020, which now appears last

The query now holds both figures that the growth calculation needs.

  • On the Add Column tab, click Custom Column again.
  • Name the column PercentageSalesGrowth, and enter this formula:
= ([Sales] - [PreviousDateSales]) / [PreviousDateSales]

The Custom Column dialog with the new column named PercentageSalesGrowth and the formula dividing the change in sales by PreviousDateSales

  • Click OK. The new column holds each day’s growth as a decimal fraction.
  • Select the PreviousDate and PreviousDateSales columns, right-click and choose Remove Columns.
  • Select the Date column, and on the Home tab click Sort Ascending to put the rows back in date order.

The query with Date, Sales and PercentageSalesGrowth columns in date order, with null on the first day and values such as 1, 0 and 0.5 on later days

  • Click Close & Load.
  • In the worksheet, format the PercentageSalesGrowth column as a percentage, by selecting the column, right-clicking and choosing Format Cells.

The loaded Excel table with PercentageSalesGrowth shown as percentages, blank on the first day and 100%, 0%, 50%, 67% and 40% on the following days

let
//Load data from Excel workbook
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//Set the type of the Date column to be date
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}}),
//Calculate the date one day before the current date
#"Added Custom" = Table.AddColumn(#"Changed Type", "PreviousDate", each [Date] - #duration(1,0,0,0)),
//Join the table to itself to look up the previous date value
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"PreviousDate"}, #"Added Custom", {"Date"}, "Added Custom", JoinKind.LeftOuter),
//Expand the Sales column from the joined table
#"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"Sales"}, {"Sales.1"}),
//Rename the new column to PreviousDateSales
#"Renamed Columns" = Table.RenameColumns(#"Expanded Added Custom",{{"Sales.1", "PreviousDateSales"}}),
//Calculate the percentage growth
#"Added Custom1" = Table.AddColumn(#"Renamed Columns", "PercentageSalesGrowth", each ([Sales] - [PreviousDateSales]) / [PreviousDateSales]),
//Remove the PreviousDate and PreviousDateSales columns
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"PreviousDate", "PreviousDateSales"}),
//Sort the table in ascending order by the Date column
#"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Date", Order.Ascending}})
in
#"Sorted Rows"

This example follows one in Chris Webb’s Power Query for Power BI and Excel (Apress).