Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
bgonen7
Helper I
Helper I

Add new columns with recent 13 months (EOM key dates) in PBI Editor

Hi

1. I am trying to add 13 new columns in a power query to an existed table. 

2. Each column represent end of month (of recent 13 months with key dates 1/31/22, 2/28/22, 3/31/22, 4/30/22  etc)

3. Then I am trying to add the following logic:

 

bgonen7_4-1673474453584.png

 

 

 

 

 

1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi @bgonen7 ,

 

Please try:

First add a new column [key date] and then expand it:

=List.Generate(()=>[x=Date.EndOfMonth( Date.AddMonths( Date.From(DateTime.LocalNow()),-12)),i=0], each [i]<13, each [i=[i]+1,x=Date.EndOfMonth(Date.AddMonths([x],1))], each [x])

Then add a new column [value]:

=if [key date]>=[Post Date] and [key date]<=[Cleared Date]
then [Amount] else ""

Then Pivot the two columns:

vjianbolimsft_0-1673492523288.png

Here is the M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcxLCsAgDIThu2TtQDLpy2VrbyHe/xpNLUo3gfn5SK1ySpJN41BJmMLtGw4DKS1VuaLkafKbhzd4JyWKryMf+AvmTu5Iy/yywyYhqNLaAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Invoice = _t, Amount = _t, #"Post Date" = _t, #"Cleared Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Invoice", type text}, {"Amount", Int64.Type}, {"Post Date", type date}, {"Cleared Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "key date", each List.Generate(()=>[x=Date.EndOfMonth( Date.AddMonths( Date.From(DateTime.LocalNow()),-12)),i=0], each [i]<13, each [i=[i]+1,x=Date.EndOfMonth(Date.AddMonths([x],1))], each [x])),
    #"Expanded key date" = Table.ExpandListColumn(#"Added Custom", "key date"),
    #"Added Custom1" = Table.AddColumn(#"Expanded key date", "value", each if [key date]>=[Post Date] and [key date]<=[Cleared Date]
then [Amount] else ""),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Custom1", {{"key date", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Custom1", {{"key date", type text}}, "en-US")[#"key date"]), "key date", "value")
in
    #"Pivoted Column"

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
bgonen7
Helper I
Helper I

I love the code. I get the most recent 13 months but I still need to play with the formula because right now I keep getting the result of "1" on all 13 new columns.

Also, I keep getting refresh errors. (I think it got to do with the Date columns).

Overall, I think this is a great solution but I still need to do tests on my end.

Thank you.

v-jianboli-msft
Community Support
Community Support

Hi @bgonen7 ,

 

Please try:

First add a new column [key date] and then expand it:

=List.Generate(()=>[x=Date.EndOfMonth( Date.AddMonths( Date.From(DateTime.LocalNow()),-12)),i=0], each [i]<13, each [i=[i]+1,x=Date.EndOfMonth(Date.AddMonths([x],1))], each [x])

Then add a new column [value]:

=if [key date]>=[Post Date] and [key date]<=[Cleared Date]
then [Amount] else ""

Then Pivot the two columns:

vjianbolimsft_0-1673492523288.png

Here is the M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcxLCsAgDIThu2TtQDLpy2VrbyHe/xpNLUo3gfn5SK1ySpJN41BJmMLtGw4DKS1VuaLkafKbhzd4JyWKryMf+AvmTu5Iy/yywyYhqNLaAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Invoice = _t, Amount = _t, #"Post Date" = _t, #"Cleared Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Invoice", type text}, {"Amount", Int64.Type}, {"Post Date", type date}, {"Cleared Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "key date", each List.Generate(()=>[x=Date.EndOfMonth( Date.AddMonths( Date.From(DateTime.LocalNow()),-12)),i=0], each [i]<13, each [i=[i]+1,x=Date.EndOfMonth(Date.AddMonths([x],1))], each [x])),
    #"Expanded key date" = Table.ExpandListColumn(#"Added Custom", "key date"),
    #"Added Custom1" = Table.AddColumn(#"Expanded key date", "value", each if [key date]>=[Post Date] and [key date]<=[Cleared Date]
then [Amount] else ""),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Custom1", {{"key date", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Custom1", {{"key date", type text}}, "en-US")[#"key date"]), "key date", "value")
in
    #"Pivoted Column"

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors