0
votes

If this does not belong here please let me know! or if any more information is needed

providing link to sample data file here - https://www.dropbox.com/s/mz3f24ciby7qthv/example%20PBI%20Community.xlsx?dl=0

Context: rows 18 to 25 contain FORECAST data at the country-customer-grouping grain... and we need to allocate to the finer grain of Label1 and Label2 Rows 2 to 17 contain 2020 data that has the grain we need, so we use the 2020 data to create an apportionment base for the 2021 data which is done by the formulas in columns I and J

Formula/Logic Explanation:

  • This is in a "database" format. it needs to stay this way even with the addition of new columns
  • FormulaPart1 is and INDEX of "Base Figure" data based on The Index Key in column N
  • FormulaPart2 is a SUMIF of "Base Figure" data based on The Index Key in column O
  • FormulaPart3 is and INDEX of "To be Allocated" data based on The Index Key in column P

Hopefully this is all clear...i would like to move this logic to PQ for efficiency and error minimisation purposes so any guidance in the right direction would be super useful :) the real dataset i work with is much much larger and having all these formulas and index key columns in excel sheet is problematic :)

Thank You for your guidance! :)

1

1 Answers

1
votes

Assuming range A1:H25 is named Table1, this seems to do what you want

//assumes range A1:H25 from sample data is loaded as named range Table1
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Label1", Int64.Type}, {"Label2", type text}, {"Type", type text}, {"Country", type text}, {"Customer", type text}, {"Grouping", type text}, {"Original Value", Int64.Type}}),

// add index for re-sorting later
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),

// pull Base Figure rows This starts us with Part 1 of formula
#"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Type] = "Base Figure")),

// formula part 2
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Country", "Customer", "Grouping"}, {{"Original Value 2", each List.Sum([Original Value]), type number}}),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows",{"Country", "Customer", "Grouping"},#"Grouped Rows" ,{"Country", "Customer", "Grouping"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Original Value 2"}, {"Original Value 2"}),

// formula part 3
#"Filtered Rows2" = Table.SelectRows(#"Added Index", each ([Type] = "To be Allocated")),
#"Grouped Rows2" = Table.Group(#"Filtered Rows2", {"Country", "Customer", "Grouping"}, {{"Original Value", each List.Sum([Original Value]), type number}}),
#"Merged Queries1" = Table.NestedJoin(#"Expanded Table2",{"Country", "Customer", "Grouping"},#"Grouped Rows2",{"Country", "Customer", "Grouping"},"Table2",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries1", "Table2", {"Original Value"}, {"Original Value 3"}),

// Add math for new column based on Formula / Formula2 * Formula3
#"Added Custom" = Table.AddColumn(#"Expanded Table1", "Custom", each [Original Value]/[Original Value 2]*[Original Value 3]),

// Replace year with following year, replace Base Figure with Allocated Figure, re-sort on original sort
#"Replaced Value" = Table.ReplaceValue(#"Added Custom", #"Filtered Rows"{0}[Year], #"Filtered Rows2"{0}[Year],Replacer.ReplaceValue,{"Year"}),
#"BlankOriginalValue" = Table.TransformColumns(#"Replaced Value",{{"Original Value", each null}}),
#"Replaced Value1" = Table.ReplaceValue(#"BlankOriginalValue","Base Figure","Allocated Figure",Replacer.ReplaceText,{"Type"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value1",{"Original Value 2", "Original Value 3"}),
#"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Index", Order.Ascending}}),

//combine new Allocated Figure data with original range and remove extra columns
#"Combine" = Table.Combine({#"Added Index" , #"Sorted Rows" }),
#"Added Custom1" = Table.AddColumn(Combine, "Forumla", each if [Custom]=null then [Original Value] else [Custom]),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Index"})

in #"Removed Columns1"

Second version that should work for multiple To Be Allocated years off a base year. Comments omitted. Refer to first version for explanations

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Label1", Int64.Type}, {"Label2", type text}, {"Type", type text}, {"Country", type text}, {"Customer", type text}, {"Grouping", type text}, {"Original Value", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Type] = "Base Figure")),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Country", "Customer", "Grouping"}, {{"Original Value 2", each List.Sum([Original Value]), type number}}),
#"Filtered Rows2" = Table.SelectRows(#"Added Index", each ([Type] = "To be Allocated")),
#"Merged Queries3" = Table.NestedJoin(#"Filtered Rows2",{"Country", "Customer", "Grouping"},#"Filtered Rows" ,{"Country", "Customer", "Grouping"},"Table3",JoinKind.LeftOuter),
#"Removed Columns1" = Table.RemoveColumns(#"Merged Queries3",{"Label1", "Label2"}),
#"Expanded Table3" = Table.ExpandTableColumn(#"Removed Columns1", "Table3", {"Label1", "Label2", "Original Value"}, {"Label1", "Label2", "Formula1"}),
#"Merged Queries" = Table.NestedJoin(#"Expanded Table3" ,{"Country", "Customer", "Grouping"},#"Grouped Rows" ,{"Country", "Customer", "Grouping"},"Table3",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table3", {"Original Value 2"}, {"Formula2"}),
#"Grouped Rows1" = Table.Group(#"Filtered Rows2" , {"Year", "Country", "Customer", "Grouping"}, {{"Part3", each List.Sum([Original Value]), type number}}),
#"Merged Queries1" = Table.NestedJoin(#"Expanded Table2",{"Year", "Country", "Customer", "Grouping"},#"Grouped Rows1",{"Year", "Country", "Customer", "Grouping"},"Table3",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries1", "Table3", {"Part3"}, {"Formula3"}),
#"Added Custom" = Table.AddColumn(#"Expanded Table1", "New", each [Formula1]/[Formula2]*[Formula3]),
#"Sorted Rows" = Table.Sort(#"Added Custom",{{"Index", Order.Ascending}, {"Label1", Order.Ascending}}),
#"Replaced Value" = Table.ReplaceValue(#"Sorted Rows","To be Allocated","Allocated Figure",Replacer.ReplaceText,{"Type"}),
#"BlankOriginalValue" = Table.TransformColumns(#"Replaced Value",{{"Original Value", each null}}),
#"Removed Columns" = Table.RemoveColumns(BlankOriginalValue,{"Index", "Formula2", "Formula3", "Formula1"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Year", "Label1", "Label2", "Type", "Country", "Customer", "Grouping", "Original Value", "New"}),
#"Combine" = Table.Combine({#"Changed Type",#"Reordered Columns"  })
in #"Combine"