Now that the brave new world of self-service BI is upon us, old-school corporate BI types like me need to sharpen our Excel skills – and anyone learning Excel will, sooner or later, end up on Bill Jelen (aka Mr Excel)’s site. I found his latest podcast on splitting the value of a contract over N months particularly interesting not only because I had to deal with a similar problem with a client only a few weeks ago but also because the problem of allocation in Power Query is something I’ve been meaning to blog about for a while. In this post I’m going to take the same data that Bill and Mike Girvin (whose book on Excel array formulas I got for Christmas!) used in the podcast and show how to achieve the same results they did but in Power Query and Power Pivot.
My starting point is an Excel sheet with two tables named Contract and Month, shown below:
It’s not exactly the same layout as in the podcast but that’s deliberate – I want to keep my source data and my output (which could be a PivotTable, cube formulas or a Power View sheet) separate.
Next, I import the Month table into Power Query using the From Table button and then click on the Add Index button to add an index column, so that the query output is as follows:
I don’t need to load this anywhere though, even though I’m going to use its output in the next query, so I leave both of the boxes in the Load Settings section of the Query Editor unchecked and go back to the worksheet:
Next, I import the Contract table and add an index column in the same way:
I can now add a custom column to calculate the monthly amount by dividing Contract Amount by Months In Contract:
Now comes the interesting bit. I insert another custom column and this time the M expression to paste into the dialog is:
Table.FirstN(Month, [Months In Contract])
In each row this column contains a table containing the first N rows of the Month table, where N is the value from the [Months In Contract] column. The output is this:
I then just need to click on the expand icon next to the column header of the Custom column to repeat each contract row for all the months it applies to, rename the columns and set the column types appropriately, and I’m ready to load into the Excel Data Model:
Here’s the full M code for both queries:
--Month Query
let
Source = Excel.CurrentWorkbook(){[Name="Month"]}[Content],
InsertedIndex = Table.AddIndexColumn(Source,"Index"),
ReorderedColumns = Table.ReorderColumns(InsertedIndex,{"Index", "Month"})
in
ReorderedColumns
--Contract Query
let
Source = Excel.CurrentWorkbook(){[Name="Contract"]}[Content],
InsertedIndex = Table.AddIndexColumn(Source,"Index"),
RenamedColumns = Table.RenameColumns(InsertedIndex,{{"Index", "ContractID"}}),
ReorderedColumns = Table.ReorderColumns(RenamedColumns,{"ContractID",
"Months In Contract", "Contract Amount"}),
InsertedCustom = Table.AddColumn(ReorderedColumns, "Allocated Amount",
each [Contract Amount]/[Months In Contract]),
InsertedCustom1 = Table.AddColumn(InsertedCustom, "Custom",
each Table.FirstN(Month, [Months In Contract])),
#"Expand Custom" = Table.ExpandTableColumn(InsertedCustom1, "Custom",
{"Index", "Month"}, {"Custom.Index", "Custom.Month"}),
RenamedColumns1 = Table.RenameColumns(#"Expand Custom",{{"Custom.Index", "MonthID"},
{"Custom.Month", "Month"}}),
ChangedType = Table.TransformColumnTypes(RenamedColumns1,{{"Allocated Amount", type number},
{"Contract Amount", type number}, {"MonthID", type number},
{"Months In Contract", type number}, {"ContractID", type number}})
in
ChangedType
Last of all, I need to go into the Power Pivot window and do two things:
- Use the Sort By Column functionality to sort my Month column by MonthID
- Format my Allocated Amount column using a dollar sign
And I’m ready! I can now create a PivotTable containing my allocated values:
Maybe it’s a little bit more long-winded than Bill or Mike’s examples but I don’t think it’s any more complex. And of course, now the data is in the Excel Data Model I have a lot more flexibility on how to present the data. For example I can use Power View with no remodelling or formula changes necessary:
One last point: I know it’s good practise to use a separate Date table with Power Pivot. I didn’t do so here because I wanted to keep as close to the original example as possible. And because I’m lazy.
You can download the demo workbook here.