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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
IF
Post Prodigy
Post Prodigy

3 month rolling - new column (not measure) in power query

Hi,

I checked the forum before writing this question, but I couldn't find an answer. I want to have a new column in power query editor. I don't want a measure. is it possible to have two more columns to see the 3M data. I have the following example data:

DateMonth
04.20222
03.20223
02.20224
01.20224
12.20215
11.20213
10.20211

 

I would like to see it as in power query:

Date

DateValue3M -current+previous+preprevious3M - previous + preprevious + 4 months ago
04.20222911
03.202231113
02.202241312
01.20224129
12.2021594
11.2021341
10.202111 

 

I know how to write the dax, i am interested in only the new column in power query.

THanks in advance.

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

Hi @IF ,

 

Please create the following custom columns:

 

3M -current+previous+preprevious =
let 
   myfunction = (CurrentIndex) => 
     let 
       IndexList = {[Index]-2..[Index]}
     in 
       IndexList,
   Data = List.Sum(Table.SelectRows(#"Added Custom",each List.Contains(myfunction([Index]),[Index]))[Month])

in 
   Data
3M - previous + preprevious + 4 months ago =
let 
   myfunction = (CurrentIndex) => 
     let 
       IndexList = {[Index]-3..[Index]-1}
     in 
       IndexList,
   Data = List.Sum(Table.SelectRows(#"Added Custom",each List.Contains(myfunction([Index]),[Index]))[Month])

in 
   Data

vkkfmsft_1-1651824311396.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
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

7 REPLIES 7
v-kkf-msft
Community Support
Community Support

Hi @IF ,

 

Please create the following custom columns:

 

3M -current+previous+preprevious =
let 
   myfunction = (CurrentIndex) => 
     let 
       IndexList = {[Index]-2..[Index]}
     in 
       IndexList,
   Data = List.Sum(Table.SelectRows(#"Added Custom",each List.Contains(myfunction([Index]),[Index]))[Month])

in 
   Data
3M - previous + preprevious + 4 months ago =
let 
   myfunction = (CurrentIndex) => 
     let 
       IndexList = {[Index]-3..[Index]-1}
     in 
       IndexList,
   Data = List.Sum(Table.SelectRows(#"Added Custom",each List.Contains(myfunction([Index]),[Index]))[Month])

in 
   Data

vkkfmsft_1-1651824311396.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1651649644923.png

= #table(Table.ColumnNames(Source)&{"3M -current+previous+preprevious","3M - previous + preprevious + 4 months ago"},List.Accumulate(List.Reverse(Table.ToRows(Source)),{{},{}},(x,y)=>let a={y{1}}&x{1} in {{y&List.Transform({0,1},each List.Sum(List.Range(a,_,3)))}&x{0},List.FirstN(a,3)}){0})

 

Hi,

Thanks for the quick answer. I tried it, but it is populating the rows, which is making a problem for other calculations. Below is the screenshot. Maybe I do smth wrong. As you see the 04.2022 row is populated 8 times, which should be only one row.

102.jpg

Vijay_A_Verma
Super User
Super User

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("XcvBCQAgDATBXu4t4p2xmpD+21AS8vE5LOsOm1oSBoQYjt3cSTUtyY+Z+XzKbNfN1SYiLg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Month = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type number}, {"Month", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "3M -current+previous+preprevious", each List.Sum(List.Range(#"Added Index"[Month],[Index],3))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "3M - previous + preprevious + 4 months ago", each try #"Added Custom"[#"3M -current+previous+preprevious"]{[Index]+1} otherwise null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index"})
in
    #"Removed Columns"

 

 

Hi,

Thanks for the quick answer. I already use an index but vice versa. 10.2021 is 0 and 4.2022 is 6. I tried to use -3 but didn't work. Can I use -3 with this logic?

  #"Added Custom" = Table.AddColumn(#"Added Index", "3M -current+previous+preprevious", each

List.Sum(List.Range(#"Added Index"[Month],[Index],-3))),

Regards

In both, Index has to be replaced with 6-[Index]. If you want to dynamically determine this number 6 i.e. highest, then you replace 6 with List.Max(#"Added Index"[Index])

Hence, below is the revised query.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcvBCQAgDATBXu4t4p2xmpD+21AS8vE5LOsOm1oSBoQYjt3cSTUtyY+Z+XzKbNfN1SYiLg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Month = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type number}, {"Month", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 6, -1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "3M -current+previous+preprevious", each List.Sum(List.Range(#"Added Index"[Month],6-[Index],3))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "3M - previous + preprevious + 4 months ago", each try #"Added Custom"[#"3M -current+previous+preprevious"]{7-[Index]} otherwise null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index"})
in
    #"Removed Columns"

 

 

 

 

Sorry to say that I have repeated values. It really depends on the month. it is sometimes 10 sometimes 20. I have also some other columns. In this regards, I should put the index on month. My purpose is to have 3 month rolling data. I tried and it didn't work in this case. Here is an example:

asa.jpg

 

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors