Power Query: Sum of every 2 weeks

I have a column with id, it has repeated items and columns with values. Data are like week 1, week 1, week 2, week 2, etc. I need to create a table where for week 3 I have an average value between 2 whole previous weeks (week 1, week 1 week 2, week 2), for week 4 I have an average value between week 2, week 2, week 3, week 3 from the original table etc. How can I do it in Power Query.

0

Oh ouch. First off, I’d fix your design. I would do it, but I don’t understand completely what it means. (could you explain?)

ID column has two bits of information in it, and it should only have one. So split the column on the semi-colon delimiter so you get two separate columns.

Then unpivot the Value1 and Value2 columns so you end up with just
(Vehicle, WeekNumber, Value)

Then sums and averages etc are really simple.

4

You can go to Data(Power Query)->Blank Query, and paste below code in the advance editor.

let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
  groupedbyId = Table.Group(Source, {"id"}, {{"values", (g)=> g[value 1] & g[value 2]}}),
  splitId = Table.SplitColumn(groupedbyId, "id", Splitter.SplitTextByDelimiter(";"), {"Name", "Week"}),
  extractWeekNum = Table.TransformColumns(splitId, {{"Week", (x) =>Text.AfterDelimiter(x, " ", {0, RelativePosition.FromEnd})}}),
  changeToNumber = Table.TransformColumnTypes(extractWeekNum, {{"Week", type number}}),
  filterRows = Table.AddColumn(changeToNumber, "PreviousTwoWeeks", (row) => Table.SelectRows(changeToNumber, (r)=> r[Name] =row[Name] and r[Week] >= row[Week]-2 and r[Week] <= row[Week]-1)),
  listPrevious2WeeksValues = Table.AddColumn(filterRows, "Previous2WeeksValues", (row)=>List.Combine(row[PreviousTwoWeeks][values])),
  average = Table.AddColumn(listPrevious2WeeksValues, "Previous2WeeksAverage", (row)=> List.Average(row[Previous2WeeksValues])),
  displayValues = Table.AddColumn(average, "Display Values", (row)=> Text.Combine(List.Transform(row[Previous2WeeksValues], (x)=>Number.ToText(x)), ", ")),
  removeUnused = Table.RemoveColumns(displayValues, {"values", "PreviousTwoWeeks", "Previous2WeeksValues"})
in
  removeUnused

The main logic is in Table.SelectRows(), it filters out data from previous two weeks.

Results look like this:

This should do it (same answer as I provided to your identical question in the Fabric community)
Note that Week 2 is included here as the average of the two preceding weeks (Week 0 and Week 1). I don’t understand why it was excluded in your example, but if you need to exclude it also for some reason, it is only a minor code change.

let 
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY85DoAgEEWvQqgtWF3iUQiFGio6G+PtZQsyA8Vv3s8szxh6HfdOHuc8YXSiPGSmdmp5ZDJEDbgOEYiLwNY00/PcQS7LXT7gWwrkKv3S71dlf/7nfL1rxFgVaIp4danGqNCjVdANFXx0A9qh4tdrCuiHiiJoPw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, #"value 1" = _t, #"value 2" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", type text}, {"value 1", Int64.Type}, {"value 2", Int64.Type}}),
    
//Group by id and return a list of all the values
    #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {
        {"Values", each [value 1] & [value 2],type {Int64.Type}}
        }),

//Split of the type (car or byke) to group on this
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "id Type", each List.First(Text.Split([id],";")), type text),

//Create a list of the rolling two week average, and add it as a colum
    #"Grouped Rows1" = Table.Group(#"Added Custom", {"id Type"}, {
        {"2 wk avg", (t)=>
            let 
                RA = List.Generate(
                    ()=>[ra=null, idx=1],
                    each [idx] <= Table.RowCount(t),
                    each[ra=if [idx]<2 then null
                            else List.Average((t{[idx]-1}[Values] & t{[idx]-2}[Values])), idx=[idx]+1],
                    each [ra])
            in Table.FromColumns(
                Table.ToColumns(t)
                & {RA}, {"id","a","b","value"}
            ), type table [id=nullable text, a=any, b=any,value=nullable number]}}),
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows1",{"id Type"}),
    #"Expanded 2 wk avg" = Table.ExpandTableColumn(#"Removed Columns", "2 wk avg", {"id", "value"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded 2 wk avg", each ([value] <> null))
in
    #"Filtered Rows"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật