In today's blog post, I will share the simple solutions for the problem that I posted here.

For the recursion approach, stay tuned for Part 2 of this post, or download the solution workbook here.

As you recall, we had a table of backlog items with three levels of hierarchies (Scenario, Feature and Backlog Item). In each row, the source data included the unique ID of the lowest level but had blank values in the columns that represented higher levels in the hierarchy, as shown at the left table.

The challenge was to fill down the table using Power Query in Excel or Power BI and reach the format of the right table above. I also asked if you can you take your solution one step further and write it in a way that will support dynamic number of levels and different header names (For example: Continent, Country, Country, City, Street). This approach allows you to have a dynamic solution that doesn't require any changes to the queries (except for one change - the number of hierarchies in the table).

Special Thank You

Andrew Lindfield, Alex Powers, Nick M., Mehdi Hamadi, James Fancke, Frank T, Cristian Angyal, Kunle Sopeju, Shungu, Stevej, and Jeremy Thiele - Thank you for sharing your solutions for the Fill Down Hierarchies challenge. Except one, you all nailed it. Few of you even addressed the bigger challenge and created a dynamic solution.

Let's start with the simple solution that fills down the specific three hierarchies.

The Simple Solution

Download the workbook here to start. If you use Power BI Desktop, start from a blank report, and load the Backlogs table using Get Data. If you prefer using Excel open the workbook above and select From Table/Range in the Data ribbon.

In the Power Query Editor, you can see that the first column should be filled down.

To do it, right click on Scenario column and select Fill and Down.

If you now proceed with another Fill Down step on the Feature column, you would get wrong results. The feature F4 will be filled down and will "leak" into scenario S2. The feature F7 will be wrongly filled down to scenarios S3 and S4. The wrong results happen because the basic Fill Down transformation cannot look at the context in the Scenario column and stop filling down when the scenario changes.

So, how can we perform a smart Fill Down? It's relatively easy. We will somehow mark the null values that should not be filled with temporary values, fill down the Feature column and then replace back the temporary values into nulls.

Let's start with the most direct approach to do it. But first, delete the previous Fill Down. We need to take advantage of the null values in the Scenario column.

After you delete the Filled Down step in Applied Steps, select Conditional Column in Add Column tab. In the Add Conditional Column dialog box, follow these steps:

  • Enter Feature - Temp in New column name.
  • Select Scenario in Column Name drop-down menu.
  • Enter null in Value.
  • Select Select a Column in the drop-down menu below Output.
  • Select Feature in the second Output drop down menu.
  • Enter Don't fill down in Otherwise and click OK.

You can now see that the Feature - Temp column has the values "Don't fill down" whenever the value in Scenario column is not null, and the original value in Feature when Scenario is null.

Now we are ready for the Fill down. Select both Scenario and Feature - Temp, and right click on of their headers. In the shortcut menu, select Fill, and then select Down. You can now remove the Feature column and rename Feature - Temp to Feature.

It's time to replace back the "Don't fill down" values into nulls. Right click on Feature column, and select Replace Values..

In the Replace Values dialog box, enter Don't fill down in the Value To Find, and null in Replace With. Then, click OK.

Finally, if you use Excel and intend to load the transformed table back into Excel, you would need to change the order of the columns, and reorder Feature as the second column.

The Simplest Solution - Conditional Replace Values

Now that we got the gist of it, I wanted to share the solution that was provided by Frank T. Instead of adding a new column, we could manipulate the null values using the Replace Values transformation. Here are the steps:

Import the Backlogs table into Power Query Editor. Right click on the Feature column, and select Replace Values..

In the Replace Values dialog box, enter a as Value To Find and b as Replace With. Then expand the Advanced options and select Match entire cell contents. When you are done, click OK.

We used the previous step to generate the base code we need for the conditional replace values step. Let's look at the code:

= Table.ReplaceValue(Source,"a","b",Replacer.ReplaceValue,{"Feature"})

We will replace "a" and "b" as follows:

= Table.ReplaceValue(Source, each [Feature], each if [Scenario]=null then [Feature] else "Don't fill down", Replacer.ReplaceValue, {"Feature"})

"a" was replaced to each [Feature] which returns the current value in the Feature column.

"b" was replaced with the same condition we used in the Conditional Column step earlier.

From here, you can apply the Fill Down on the first two columns and then replace the values Don't fill down with nulls, as we did earlier.

You can review the two solutions here.

In the next blog post we will move to the bigger challenge: How can we fill down multiple columns with any number of hierarchies and unknown column names?