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).
The Solution
The way I arrived at the solution was to do the following steps in Power Query.
- Load the data table (table on the left side above) into Power Query.
- 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.
- 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.
- 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.
- Create a list of dates from min date to max date, both inclusive.Β You can use M List.Dates function for this.
- Convert the list to table.
- Merge this query with the data table query (from step 1).
- Sort the resulting table by Date column in ascending order.
- 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.
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!!
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
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.
Please create a sample workbook in Excel showing the input, and also a manual example of the output you expect. if you post the workbook and question here https://powerpivotforum.com.au/viewforum.php?f=5 and then drop a note back here once it is done, I will take a look for you.
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! π
Me too π
It works on he website. There was an issue when I published it that may have affected RSS feeds, but the website is fine.
Great piece.
FYI…the YouTube video link is not working.