cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ogend
Helper I
Helper I

inserting records for missing months

Hi Power Query Experets. 

I have a table with monthly values, i would like to enter 0 dollar records for all month not in the table 

Can you please help?

Data: 

accountyearmonth amount
1232021310
1232021615
1232021925
12320211230
1232022710
1232022815
2352021110

 

needed output:

accountyearmonth amount
123202110
123202120
1232021310
123202140
123202150
1232021615
123202170
123202180
1232021925
1232021100
1232021110
12320211230
123202210
123202220
123202230
123202240
123202250
123202260
1232022710
1232022815
123202290
1232022100
1232022110
1232022120
2352021110
235202120
235202130
235202140
235202150
235202160
235202170
235202180
235202190
2352021100
2352021110
2352021120

 

Thank you!

1 ACCEPTED SOLUTION
Rocco_sprmnt21
Community Champion
Community Champion

 

let
    months=Record.FromList(List.Repeat({0},12), {"1".."9", "10","11","12"}),
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjIwMgRSIKahgVKsDpq4GUjcFFPcEsTEIm5oBDIMzSCQmDkWC0DiFggLjIxNkQyCqo8FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [account = _t, year = _t, month = _t, amount = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"account", Int64.Type}, {"year", Int64.Type}, {"month", type text}, {"amount", Int64.Type}}),
    #"Raggruppate righe" = Table.Group(#"Modificato tipo", {"account", "year"}, {"all", each Record.ToTable(months&Record.FromList([amount],[month]))}),
    #"Tabella all espansa" = Table.ExpandTableColumn(#"Raggruppate righe", "all", {"Name", "Value"}, {"Month", "Amount"})
in
    #"Tabella all espansa"

 

View solution in original post

3 REPLIES 3
Rocco_sprmnt21
Community Champion
Community Champion

 

let
    months=Record.FromList(List.Repeat({0},12), {"1".."9", "10","11","12"}),
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjIwMgRSIKahgVKsDpq4GUjcFFPcEsTEIm5oBDIMzSCQmDkWC0DiFggLjIxNkQyCqo8FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [account = _t, year = _t, month = _t, amount = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"account", Int64.Type}, {"year", Int64.Type}, {"month", type text}, {"amount", Int64.Type}}),
    #"Raggruppate righe" = Table.Group(#"Modificato tipo", {"account", "year"}, {"all", each Record.ToTable(months&Record.FromList([amount],[month]))}),
    #"Tabella all espansa" = Table.ExpandTableColumn(#"Raggruppate righe", "all", {"Name", "Value"}, {"Month", "Amount"})
in
    #"Tabella all espansa"

 

Vijay_A_Verma
Community Champion
Community Champion

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjIwMgRSIKahgVKsDpq4GUjcFFPcEsTEIm5oBDIMzSCQmDkWC0DiFggLjIxNkQyCqo8FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [account = _t, year = _t, month = _t, amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"account", Int64.Type}, {"year", Int64.Type}, {"month", Int64.Type}, {"amount", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"month", "amount"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
    #"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "month", each {1..12}),
    #"Expanded month" = Table.ExpandListColumn(#"Added Custom", "month"),
    #"Merged Queries" = Table.NestedJoin(#"Expanded month", {"account", "year", "month"}, #"Changed Type", {"account", "year", "month"}, "Expanded month", JoinKind.LeftOuter),
    #"Expanded Expanded month" = Table.ExpandTableColumn(#"Merged Queries", "Expanded month", {"amount"}, {"amount"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Expanded month",{{"Index", Order.Ascending}, {"year", Order.Ascending}, {"month", Order.Ascending}}),
    #"Replaced Value" = Table.ReplaceValue(#"Sorted Rows",null,0,Replacer.ReplaceValue,{"amount"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Replaced Value",{"Index"})
in
    #"Removed Columns1"

 

First create a calendar table. It can be a month level table, containing all the unique months, including the missing months. https://exceleratorbi.com.au/power-bi-calendar-tables/

You must have a unique id column. I suggest something like 202101 for Jan, 202102 for Feb, etc. this is the primary key

create the same key in your existing table show above - year *100 + month

join the 2 tables. Note, it will create a 1:1 relationship. Change it to a 1:many relationship where calendar filters the table above. 
use the year and month column from the calendar table in your final visual on the report

write a measure total amount =sum(table[amount])+0

put the measure in your visual. 

 



* Matt is a Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

Helpful resources

Announcements
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 24 without aka link and time 768x460.jpg

Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Kudoed Authors