Power Query Lists

Mynda Treacy

April 14, 2017

In Excel we often need to work with lists. List of numbers, letters, dates, countries, products, you get the idea.

With Power Query* we can generate lists of standard items like numbers, letters and dates quickly and easily.

We can use one of the many Power Query List Functions, or we can use a shortcut to create a list of consecutive numbers or letters. Let’s look at some examples.

Warning: Power Query functions and formulas are case sensitive.

*Power Query is available for Excel 2010 onwards.

Download the Workbook

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.

Power Query Lists - Quick Links

Power Query List Consecutive Numbers

Let’s start by creating a list of consecutive numbers. First we need to create a new blank query:

power query lists of consecutive numbers

Note: in Excel 2016 go to the Data tab > New Query > From Other Sources > Blank Query.

This opens the Query Editor and in the formula bar type:

={1..10}

open Query Editor

Press ENTER and Power Query will return a list of numbers from 1 through 10:

power query lists of numbers from 1 through 10

Notice how we surround our list arguments in curly braces, and the ellipsis points . . instructs Power Query to fill in the blanks.

Power Query List Consecutive Letters

We can also generate lists of letters like this:

power query lists of consecutive letters

Notice the A and Z are surrounded by double quotes, as they are text, just like we would in an Excel formula. And again, the list arguments are surrounded by curly braces.

Power Query List Dates

Dates are a little more complicated. Just like in Excel, Power Query recognises date serial numbers.

We can convert dates into their data serial number to more easily create a list of dates using the Number.From function, like so:

convert dates to data serial numbers

The list formula:

= { Number.From( #date(2017,1,1) ) . . Number.From( #date(2017,12,31) ) }

Is made up of the following elements:

#date(2017,1,1) is a literal date function that creates a data value from (year as a number, month as a number, day as a number).

The Number.From functions convert the dates into their date serial number equivalent, which is why the list starts at 42736, i.e. the date serial number for January 1st, 2017.

And again, the whole formula is wrapped in curly braces to return a list.

Convert Lists to Tables

The above examples are all in a list format. We can load lists (Home tab > Close & Load) into our worksheet in an Excel Table, or load it to the data model (Power Pivot where it’s converted to a Table), but in their list form in Power Query they’re restricted to being a single column of data.

If we want to add columns or merge the list query with other query tables, then we’ll want to convert the list to a Table.

It’s easy to do; with the list selected, the contextual List Tools: Transform tab is open. Click on the ‘To Table’ icon:

convert list to table 1

Now our list is in a table:

convert list to table 2

From here we can change the data type to Date. Select Column1 > Home tab or Transform tab > Data type: Date

change data type to date

Double click in the Column header and change the column name to something more useful:

change column name

Give your query a name. This will help you identify it and work with it later on:

change query name

Now you can Close & Load into a Table in a worksheet, or load it into the data model (Power Pivot):

Close & Load into a table

So, that is the basics of generating lists with Power Query. Let’s look at some more advanced examples.

List Dates with List.Dates Function

Another way we can generate a list of dates is using the List.Dates function like so:

= List.Dates(#date(2017,1,1), 365, #duration(1,0,0,0))

The formula above lists the dates starting at January 1st, 2017 (#date(2017,1,1)), increment 365 times, incrementing by 1 day (#duration(1,0,0,0))

List.Dates function 1

Tip: if you type = List.Dates in the formula bar and press enter, you’ll get a nice GUI that you can enter the parameters in, similar to the Excel Function wizard (click Invoke to enter the formula):

List.Dates function 2

List Even Numbers

=List.Numbers(2, 20, 2)

Returns a list of numbers starting at 2, 20 numbers long, which increment by 2.

list even numbers

List of Decimal Numbers

= List.Numbers( 1, 11, 0.1)

Returns a list of numbers starting at 1, 11 numbers long, which increment by 0.1.

list of decimal numbers

List Numbers in Reverse

=List.Reverse( {1..100})

list numbers in reverse

Repeating List of Numbers

= List.Repeat( {1..5}, 3 )

Generates a list of numbers from 1 to 5 and repeats it 3 times.

repeating list of numbers

List Dates YTD

Let’s say we want to generate a list of dates from January 1st, 2017 up to today. We can then refresh the query each day and it will automatically add another date.

Using the List.Dates formula (below) that we looked at earlier:

= List.Dates(#date(2017,1,1), 365, #duration(1,0,0,0))

We replace the 365 with:

Duration.Days(DateTime.Date(DateTime.FixedLocalNow())-#date(2017,1,1))

Like so:

= List.Dates(#date(2017,1,1), Duration.Days(DateTime.Date(DateTime.FixedLocalNow())-#date(2017,1,1)), #duration(1,0,0,0))

The formula above uses our PC clock to find the date portion from the local time and subtracts that from January 1st 2017 (#date(2017,1,1)) to find the number of days to increment:

list dates YTD

If you prefer to list dates up to yesterday, then simply subtract 1 like so:

= List.Dates(#date(2017,1,1), Duration.Days(DateTime.Date(DateTime.FixedLocalNow())-#date(2017,1,1))-1, #duration(1,0,0,0))

Tip: If you’re going to use this date table in Power Pivot it’s a good idea to generate dates up to the end of the calendar year so that DAX time intelligence functions like PARALLELPERIOD return the correct results when working with year comparisons.

List Dates using Start and End Date Parameters from Named Ranges

Let’s say you’ve got a start and end date in your workbook that you want to use to generate your list of dates. These start and end dates are in Named Ranges:

list dates using start date

We can create a blank query to get the named ranges: Power Query > From Other Sources > Blank Query.

Then with the Excel.CurrentWorkbook function we can get a list of the file contents:

Excel.CurrentWorkbook function

And then filter to select the StartDate named range:

filter StartDate

Click the double arrow on the Content column to expand the Table and expose the date (deselect ‘Use original column name as prefix’):

expand the table

Set the data type for Column1 to ‘Date’ to remove the time component from the column:

set data type to Date

Right-click the cell containing the date > Drill Down:

Drill Down

Give the query a name; ‘StartDate’:

name the query

We now have a date parameter for the StartDate that we can use in other queries.

Repeat for the EndDate named range so you have both StartDate and EndDate parameters in your queries list:

list dates using end date 1

Now create a new blank query: Power Query tab > From Other Sources > Blank Query. Here we can use the same Number.From function we used earlier (below):

= { Number.From( #date(2017,1,1) ) . . Number.From( #date(2017,12,31) ) }

And replace the literal #dates with the StartDate and EndDate parameters:

= {Number.From(StartDate)..Number.From(EndDate)}

This formula returns a list of date serial numbers based on the start and end dates in our named ranges:

list of date serial numbers

We can go ahead and convert the list to a table, change the data type to ‘Date’, rename the column and give the query a name:

list dates using end date 2

When your date parameters are in the worksheet we don’t need to open or edit the queries to make changes to the list of dates. This makes it easy for users who are unfamiliar with Power Query.

Get Power Query

For Excel 2010 and 2013 users you can download Power Query here:

https://www.microsoft.com/en-us/download/details.aspx?id=39379

If you have Excel 2016 then you already have Power Query on the Data tab of the ribbon in the Get & Transform group:

Power Query Ribbon 2016

Learn More Power Query

Click here for more Power Query tutorials.

And if you want to get up to speed quickly please check out my Power Query course.

Print Friendly and PDF
AUTHOR Mynda Treacy Co-Founder / Owner at My Online Training Hub

CIMA qualified Accountant with over 25 years experience in roles such as Global IT Financial Controller for investment banking firms Barclays Capital and NatWest Markets.

Mynda has been awarded Microsoft MVP status every year since 2014 for her expertise and contributions to educating people about Microsoft Excel.

Mynda teaches several courses here at MOTH including Excel Expert, Excel Dashboards, Power BI, Power Query and Power Pivot.

23 thoughts on “Power Query Lists”

  1. Thanks Lynda,
    Really appreciate the section on parameterised start and end dates. Screenshots are invaluable.
    Regards

    Reply
    • Hi Ibrahima,
      The easiest way from user interface, without having to write M code, is to create the daily list, then extract in new columns the month number and Year. Select Month and Year columns and remove duplicates and that’s it, you have the list of months between 2 dates.
      It should look like this:

      let
      Source = List.Dates(#date(2017,1,1), 365, #duration(1,0,0,0)),
      #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
      #"Inserted Month" = Table.AddColumn(#"Converted to Table", "Month", each Date.Month([Column1]), Int64.Type),
      #"Inserted Year" = Table.AddColumn(#"Inserted Month", "Year", each Date.Year([Column1]), Int64.Type),
      #"Removed Duplicates" = Table.Distinct(#"Inserted Year", {"Month", "Year"})
      in
      #"Removed Duplicates"

      Reply
    • Hi Jeff,

      A list contains a single column, whereas a Table can contain many columns. There are many functions for working with lists, which you can see here.

      Mynda

      Reply
  2. Thank you. I think I may just have to take the Power Query course, after I finish my Excel VBA course and my Power BA course and my PowerPoint course……… too much too learn!

    Reply
    • Hi Abid,

      Everything is complex when you don’t know how 😉 Do you remember your first Excel formula? I do. It was like learning a foreign language, but slowly it became more familiar and now I have the confidence to try new formulas.

      I hope my tutorial has encouraged you to give Power Query lists a go by starting with the easy ones first.

      Mynda

      Reply
    • You’re welcome, Joan.

      I like MSDN too (I also linked to it in the blog post), but it is still a bit light on examples and layman’s terms.

      Mynda

      Reply

Leave a Comment

Current ye@r *