Forum Discussion
Generating table from exiting table
- 6 years ago
Hi soniajs
with your dataset and your explanation - end of fiscal year is march - I get the following result with M-Code:
// Table let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUNbDQNTIwMlDSUXLMzSwBUp4hQMLQwMBAKVYHosISpiI4sSgxrzIRyHJOzMlRcE7NK0ktAvJMwKpjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Accounting Month" = _t, Name = _t, Service = _t, Sales = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Accounting Month", type date}, {"Name", type text}, {"Service", type text}, {"Sales", Int64.Type}}), #"Inserted End of Year" = Table.AddColumn(#"Changed Type", "End of Year", each if Date.Month([Accounting Month]) > 3 then #date(Date.Year([Accounting Month]) + 1,3,1) else #date(Date.Year([Accounting Month]),3,1), type date), #"Added Custom" = Table.AddColumn(#"Inserted End of Year", "Custom", each {Number.From([Accounting Month])..Number.From([End of Year])}), #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Custom", type date}}), #"Filtered Rows" = Table.SelectRows(#"Changed Type1", each (Date.Day([Custom])= 1)), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Accounting Month", "End of Year"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Custom", "Name", "Service", "Sales"}), #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "Accounting"}}) in #"Renamed Columns"With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut) - 6 years ago
soniajs
In Power BI, go to Modeling Tab > New Table and paste below code:
One clarification: In your example, Saranya should start from 1/9/2020 but you showed from 1/1/2021, any reason?
Sales Projected = SELECTCOLUMNS( GENERATE( SalesData, VAR CURRENTDATE = SalesData[Accounting Month] VAR TOTALMONTHS = DATEDIFF (CURRENTDATE, DATE ( IF ( MONTH ( CURRENTDATE ) > 3, YEAR ( CURRENTDATE ) + 1, YEAR ( CURRENTDATE ) ), 3, 1 ),MONTH) + 1 VAR MONTHS = GENERATE ( GENERATESERIES ( MONTH ( CURRENTDATE ), TOTALMONTHS + MONTH ( CURRENTDATE ) - 1 ), ROW ( "ACCMONTH", DATE ( YEAR ( CURRENTDATE ), [Value], 1 ) ) ) RETURN MONTHS ), "ACCOUNTING MONTH", [ACCMONTH], "NAME", SalesData[NAME], "SERVICE",SalesData[Service], "SALES", SalesData[Sales] )________________________
If my answer was helpful, please consider Accept it as the solution to help the other members find it
Click on the Thumbs-Up icon if you like this reply 🙂
Hi soniajs
with your dataset and your explanation - end of fiscal year is march - I get the following result with M-Code:
// Table
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUNbDQNTIwMlDSUXLMzSwBUp4hQMLQwMBAKVYHosISpiI4sSgxrzIRyHJOzMlRcE7NK0ktAvJMwKpjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Accounting Month" = _t, Name = _t, Service = _t, Sales = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Accounting Month", type date}, {"Name", type text}, {"Service", type text}, {"Sales", Int64.Type}}),
#"Inserted End of Year" = Table.AddColumn(#"Changed Type", "End of Year", each if Date.Month([Accounting Month]) > 3 then #date(Date.Year([Accounting Month]) + 1,3,1) else #date(Date.Year([Accounting Month]),3,1), type date),
#"Added Custom" = Table.AddColumn(#"Inserted End of Year", "Custom", each {Number.From([Accounting Month])..Number.From([End of Year])}),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Custom", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type1", each (Date.Day([Custom])= 1)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Accounting Month", "End of Year"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Custom", "Name", "Service", "Sales"}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "Accounting"}})
in
#"Renamed Columns"
With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)