Expanding Rows Using Power Query - Excelerator BI

Expanding Rows Using Power Query

I had an interesting question from one of my book readers and I thought it would make a good blog article. There are many ways of solving this problem, and I blogged about solving a similar problem Fill Table with Last Survey Result using DAX here. The solution I am providing in this blog post is using Power Query.

The Problem

Suppose you have data in the form of dates (not consecutive) with a value for each of the dates (see the table below left side). You need to expand the rows of the table (create the missing rows) so that you will have all the consecutive dates in the given range and each of the dates has the previous updated value (see the table below right side).

Power Query Online Training

The Solution

The way I arrived at the solution was to do the following steps in Power Query.

  1. Load the data table (table on the left side above) into Power Query.
  2. Extract the min date or the first date (12/04/2019 in this example). You can use the Power Query UI to generate the code to do this.
  3. Extract the max date or the last date (23/04/2019 in this example). You can use the Power Query UI to generate the code to do this.
  4. Find the total number of days from min date to max date. This is the duration in days (12 days in this example). You can use M Duration.days function for this.
  5. Create a list of dates from min date to max date, both inclusive. You can use M List.Dates function for this.
  6. Convert the list to table.
  7. Merge this query with the data table query (from step 1).
  8. Sort the resulting table by Date column in ascending order.
  9. Use Fill Down option to fill the null values with the previous non-null value in the column.

Close & Apply the query changes and your resulting solution table gets loaded into Power BI Desktop.

YouTube Video

I recorded how to do all the steps mentioned above in the following YouTube video.

Want to Learn Power Query in a Structured Way?

My Power Query Online training course consists of 7.5 hours of online video training material that will teach you everything you need to know about using this fabulous tool for both Power BI and Excel. You can watch some free sample videos and find out more about registration following the link above.

8 thoughts on “Expanding Rows Using Power Query”

  1. A couple of questions. I was about to buy your book Learn to Write DAX as this was recommended by someone. Has this been superceded by one of your other books.

    This example is exactly what I need to do, but the dates that I have are different for each Customer. I think I need to store the min and max date for each customer and then generate a table showing the customer and a list of dates for each. But I’m not sure how to start?

    Plus I can’t see any prices for your online videos. I have a lot to learn!!

    1. Yes, my book has been updated twice since the edition you refer to. https://exceleratorbi.com.au/supercharge-power-bi-book/

      You won’t find coverage of this topic in my book as it is Power Query. All my video courses are for sale at https://skillwave.training/ including a comprehensive Power Query course. All courses include access to a private forum where Ken and I answer student questions.

      Regarding your problem, it’s best to ask for help on a forum. Post as much detail as you can including a sample file. I prefer community.powerbi.com

  2. Hi,

    I’ve a query similar to this but the value is dependent on previous month.

    1. Wish to get the value of the previous month till the end of financial Year for forecasting.

    Example: Today is June’19, till may, i have actual Figures till May’19 and on May’19 the value is 50, so from June till March 20 i need to forecast track the number as 50.

    Please help.

  3. Thanks Matt, your video inspires me to solve a similar problem by using List.Generate where I need to repeat items according to the number next to it.
    I love Power Query! 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top