excel convert data into multi rows from single row - excel

I have below sample insurance data contains family details in single row for each ID.
ID Enrollment date Area Full Name Gender DOB Sum Insured Spouse Name Gender DOB Kid1_Name Gender DOB Kid2_Name Gender DOB
29348 24-01-2021 17 NAINAR M Male 17-Mar-1982 500000 SUBBULAKSHMI FEMALE 21-Jun-1988 GOKULSRIRAM MALE 31-Oct-2007 SRIDHAR MALE 19-Feb-2009
23434 19-04-2020 17 Kishore Male 12-Jun-1986 200000 A Savitha Female 10-Jun-1991 Sathvik Male 4-Mar-2014 A Saketh male 13-Feb-2015
46565 01-05-2020 5 Ragu Male 6-Aug-1996 300000
I'm trying to convert data like below format, so that family details are shown in rows
Tried using PivotTable option and power query option in excel but no luck.
Is it possible in excel ?
Thanks

Here's one kludgy way to do it in powerquery
(a) Merge groups of columns together (b) unpivot (c) split those columns again
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Self", each "Self"),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Spouse", each "Spouse"),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Child1", each "Child1"),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Child2", each "Child2"),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Added Custom3", {{"DOB", type text}, {"Sum Insured", type text}}, "en-US"),{"Full Name", "Gender", "DOB", "Self","Sum Insured"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m1"),
#"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns", {{"DOB3", type text}}, "en-US"),{"Spouse Name", "Gender2", "DOB3", "Spouse"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m2"),
#"Merged Columns2" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns1", {{"DOB5", type text}}, "en-US"),{"Kid1_Name", "Gender4", "DOB5", "Child1"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m3"),
#"Merged Columns3" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns2", {{"DOB7", type text}}, "en-US"),{"Kid2_Name", "Gender6", "DOB7", "Child2"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m4"),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Merged Columns3", { "ID", "Enrollment date", "Area"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByDelimiter("::", QuoteStyle.Csv), {"Name", "Gender", "Date of Birth", "Relation", "Insured amount"}),
#"Filtered Rows" = Table.SelectRows(#"Split Column by Delimiter", each ([Name] <> "")),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Date of Birth", type datetime}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Date of Birth", type date}, {"Enrollment date", type date}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Attribute"})
in #"Removed Columns"
Note if you load multiple columns with same column headers into powerquery, then the titles will change to have numbers after them. You probably will have to update code to fix the column names for Date Birth and Gender

Here is another power query method.
Original Data
Read the code comments and explore the Applied steps to get a better idea of the algorithm.
Select the ID column and Unpivot other columns
Group by ID
Create a custom aggregation that creates a List of records for each family
Expand the records into a table
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//Unpivot all except ID column
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID"}, "Attribute", "Value"),
//Group by ID then custom aggregation
//Column Names for final report
colNames = {"Date of Enrollment", "Area", "Relation", "Name", "Gender", "Date of Birth", "Sum Insured"},
#"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"ID"}, {
{"Records", (t)=>
List.Generate(
()=>[ed=t[Value]{0}, a=t[Value]{1}, r="Self", n=t[Value]{2}, g=t[Value]{3}, dob=t[Value]{4}, si=t[Value]{5}, idx=5],
each [idx] < Table.RowCount(t),
each [ed=null, a=null, r=Text.SplitAny(t[Attribute]{[idx]+1}," _"){0},
n=t[Value]{[idx]+1}, g=t[Value]{[idx]+2}, dob=t[Value]{[idx]+3}, si=null, idx=[idx]+3],
each Record.FromList(
{[ed],[a],[r],[n],[g],[dob],[si]},
colNames)
)}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"ID"}),
#"Expanded Records" = Table.ExpandListColumn(#"Removed Columns", "Records"),
#"Expanded Records1" = Table.ExpandRecordColumn(#"Expanded Records", "Records",
colNames,colNames),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Records1",{
{"Date of Enrollment", type date}, {"Area", Int64.Type}, {"Relation", type text}, {"Name", type text},
{"Gender", type text}, {"Date of Birth", type date}, {"Sum Insured", Currency.Type}})
in
#"Changed Type1"
Results

Related

Excel Unpivot Multiple Columns to a Single Column

What I want to achieve:
As the title says, is there any way to convert my table structure? I've tried using Power Query but it didn't work. Any kind of help is greatly appreciated. Thanks!
So, I was trying to pivot type and type value, but it seems impossible if I maintain the current table structure since it would cause duplicates when I wanted to aggregate on type.
Should I remake the table structure or there is any way to get around this problem?
Thanks in advance!
In Power Query, the following is adaptable to any number of type/value column pairs.
Unpivot all except the ID column
Add a custom column to define if the unpivoted value is a Type or a Type Value
Add an Index column and then do an integer/divide by 2 so things will sort in the desired order
Pivot with no aggregation, using a custom function as the "built-in" function will error with multiple items.
Custom function to Pivot with No aggregation
Rename as noted in comments
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
//Rename: fnPivotAll
(Source as table,
ColToPivot as text,
ColForValues as text)=>
let
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"
Regular Query
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"ID", type text}, {"Type 1", type text}, {"Type 1 Value", Int64.Type}, {"Type 2", type text}, {"Type 2 Value", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
#"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom",
each if Text.EndsWith([Attribute],"Value") then "Value" else "Type"),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute"}),
#"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),
#"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "Integer-Division", each Number.IntegerDivide([Index], 2), Int64.Type),
#"Removed Columns1" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),
Pivot = fnPivotAll(#"Removed Columns1","Custom","Value"),
#"Removed Columns2" = Table.RemoveColumns(Pivot,{"Integer-Division"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns2",{{"Type", type text}, {"Value", Int64.Type}})
in
#"Changed Type1"
More generically to stack vertically in powerquery while keeping certain columns
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
base_columns=1, groupsof=2, //stack them
Combo = List.Transform(List.Split(List.Skip(Table.ColumnNames(Source),base_columns),groupsof), each List.FirstN(Table.ColumnNames(Source),base_columns) & _),
#"Added Custom" =List.Accumulate(Combo, #table({"Column1"}, {}),(state,current)=> state & Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, current)),1)),
#"Rename"=Table.RenameColumns(#"Added Custom",List.Zip({Table.ColumnNames(#"Added Custom"),List.FirstN(Table.ColumnNames(Source),base_columns+groupsof)}))
in #"Rename"
What seems to be fastest method of those I've tested
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
leading=1, groupsof=2,
#"Added Custom" = Table.AddColumn(Source, "Custom", each List.Split( List.RemoveFirstN(Record.ToList( _),leading), groupsof) ),
#"Added Custom0" = Table.AddColumn(#"Added Custom", "Custom0", each Text.Combine(List.FirstN(Record.ToList(_),leading),"|")),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom0",{"Custom0", "Custom"}),
#"Expanded Custom" = Table.ExpandListColumn( #"Removed Other Columns", "Custom"),
#"Extracted Values" = Table.TransformColumns(#"Expanded Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), "|"), type text}),
#"Merged Columns" = Table.CombineColumns(#"Extracted Values",{"Custom0", "Custom"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Custom"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Merged Columns", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), List.FirstN(Table.ColumnNames(Source),leading+groupsof))
in #"Split Column by Delimiter"
There is probably a better way, but if you first concat the 4 columns with specific unique delimiter to split on later in a custom column, you have a work-around in PQ:
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Type1", type text}, {"Type1 Val", Int64.Type}, {"Type2", type text}, {"Type2 Val", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom1", each [Type1]&"|"&Number.ToText([Type1 Val])&"$"&[Type2]&"|"&Number.ToText([Type2 Val])),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Type1", "Type1 Val", "Type2", "Type2 Val"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Removed Columns", {{"Custom1", Splitter.SplitTextByDelimiter("$", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Custom1"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Custom1", type text}}),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "Custom1", Splitter.SplitTextByEachDelimiter({"|"}, QuoteStyle.Csv, false), {"Custom1.1", "Custom1.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Custom1.1", type text}, {"Custom1.2", Int64.Type}})
in
#"Changed Type2"
Just in case you tagged 'Excel-Formula' and you have access to ms365:
Formula in H1:
=REDUCE({"ID","Type","Val"},ROW(A2:A5),LAMBDA(X,Y,VSTACK(X,INDEX(A:E,Y,{1,2,3}),INDEX(A:E,Y,{1,4,5}))))
Or formula:
=SORT(VSTACK(A2:C5,HSTACK(A2:A5,D2:E5)))

how to count occurrences of values in a specific column in excel power query

I want to calculate the running count of each value based on column SF ID. In Excel power query , I am trying to apply countif in the following table but i cant find this equation here.
I would like to get the same result in excel Power query. Can you please advise.
i've used to group the date like below but this isn't the result that i want.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bcy5CQAwDASwXVwHzm+eWUz2XyOQzuBWhTJJIFCWoEFizCx0R5JCi+pXgxW1rw5vhkA0w8RshoXVDBu7GQ5OUad7Hw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"SF ID" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"SF ID", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"SF ID"}, {{"Count", each _, type table [Date=nullable date, SF ID=nullable number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Index", 1)),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Date", "SF ID", "Index"}, {"Date", "SF ID", "Index"})
in
#"Expanded Custom"
CALCULATE(
COUNTROWS(tbl)
,ALLEXCEPT(tbl,tbl[SF ID])
,tbl[Date]<=MAX(tbl[Date])
)
If I understood correctly, try this :
Select the two columns Date and SFID an make a groupby.
EDIT :
Open the Advanced Editor and put the code below :
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"SF ID", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"SF ID", Order.Ascending}, {"Date", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"SF ID"}, {{"AllData", each _, type table [Date=nullable datetime, SFID=nullable number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Status", each Table.AddIndexColumn([AllData], "Status", 1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Status", {"Date", "Status"}, {"Date", "Status"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"AllData"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Date", "SF ID", "Status"})
in
#"Reordered Columns"
Make sure that your table is named "Table1". Otherwise, you have to rename it.

How to sum N columns in Power Query

My data gets updated every month so I'm trying to create a power query table that would show the sum of the pivoted (N) columns that I created but I can't seem to figure out how to do it in power query.
I have this code currently:
After Pivoting:
Create a list of the columns to sum
Add an Index column to restrict to each row
Add a column which Sums the columns for just that row
Remove the Index colum
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Month Yr", Date.Type}, {"Attribute", type text}, {"Value", Currency.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "MonthYear", each Date.ToText([Month Yr],"MMMM yyyy")),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Month Yr"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[MonthYear]), "MonthYear", "Value", List.Sum),
//NEW code added after your Pivoted Column line
//Get List of columns to sum
// Assumes this list all columns **except the first** in the Pivot table
// There are other methods of generating this list if this assumption is incorrect
colToSum = List.RemoveFirstN(Table.ColumnNames(#"Pivoted Column"),1),
//Add Index Column
IDX = Table.AddIndexColumn(#"Pivoted Column","Index",0,1),
//Sum each row of "colToSum"
totals = Table.AddColumn(IDX, "Sum", each List.Sum(
Record.ToList(
Table.SelectColumns(IDX,colToSum){[Index]})
), Currency.Type),
#"Removed Columns1" = Table.RemoveColumns(totals,{"Index"})
in
#"Removed Columns1"
You can group and then merge into the table after pivoting
#"Grouped Rows" = Table.Group(#"Changed Type", {"Atribute"}, {{"Sum", each List.Sum([Value]), type number}}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type", {{"Month Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Changed Type", {{"Month Year", type text}}, "en-US")[#"Month Year"]), "Month Year", "Value", List.Sum),
#"Merged Queries" = Table.NestedJoin(#"Pivoted Column",{"Atribute"}, #"Grouped Rows",{"Atribute"},"Table2",JoinKind.LeftOuter),
#"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Sum"}, {"Sum"})
in #"Expanded Table"
Or you can group, add it to the table, then pivot the combined new set
#"Grouped Rows" = Table.Group(#"Changed Type", {"Atribute"}, {{"Value", each List.Sum([Value]), type number}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Month Year", each "Sum"),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Month Year", "Atribute", "Value"}),
combined = #"Reordered Columns" & #"Changed Type",
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(combined, {{"Month Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(combined, {{"Month Year", type text}}, "en-US")[#"Month Year"]), "Month Year", "Value", List.Sum)
in #"Pivoted Column"

Merging and formating Excel Worksheets with power query

I'm trying to format an excel file using Power Query in order to be able to pivot it, but I haven't been able to do it right.
I'm merging many worksheets from different workbooks into one. Every sheet has the data of the specific workbook in the first 3 rows (column 1= Title; and Column 2= Value), and then I have the table well formatted starting in row 5 (with headers and all, but different amount of rows each)
How can I transform the data in the first 3 rows of every sheet into columns, so I can get a Pivotable table?
Here an example of what I get when merging 2 files.
Based on the picture I assume you would like to transform such kind of file
to something like that
I used the following M-Code for that
let
Source = Table.FromColumns({Lines.FromBinary(File.Contents("D:\tmp\Files\file1.txt"), null, null, 1252)}),
#"Kept First Rows" = Table.FirstN(Source,3),
#"Split Column by Delimiter" = Table.SplitColumn(#"Kept First Rows", "Column1", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Col1", "Col2"}),
step = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Col1", type text}, {"Col2", type text}}),
#"Transposed Table" = Table.Transpose(step),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
Hdr = Table.TransformColumnTypes(#"Promoted Headers",{{"Nummer", type text}, {"Id", type text}}),
Custom1 = Source,
#"Removed Top Rows" = Table.Skip(Custom1,4),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Removed Top Rows", "Column1", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}, {"Column1.4", type text}}),
#"Promoted Headers1" = Table.PromoteHeaders(#"Changed Type1", [PromoteAllScalars=true]),
#"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers1",{{"F1", Int64.Type}, {"F2", Int64.Type}, {"F3", Int64.Type}, {"F4", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type2", "Nummer", each Hdr[Nummer]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "ID", each Hdr[Id]),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Export", each Hdr[Export]),
#"Expanded Export" = Table.ExpandListColumn(#"Added Custom2", "Export"),
#"Expanded ID" = Table.ExpandListColumn(#"Expanded Export", "ID"),
#"Expanded Nummer" = Table.ExpandListColumn(#"Expanded ID", "Nummer"),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Nummer",{{"Export", type date}})
in
#"Changed Type"

Replicate Excel formula logic in power query

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! :)
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"

Resources