按出生年份分组

group by year of birth

我有 table 销售额

idcustomer  year of birth   amount  salesdate
112 1970    200 12/02/2022
12  1980    400 12/03/2012
122 1990    600 12/04/2012
300 1977    20  12/06/2012
500 1996    250 12/04/2012

我需要了解不同年龄组的表现如何,每月和每年的销售额是多少,按 5 年的出生年份分组,例如 1980-1984、1985-1989。例如,我希望将年龄组动态创建为 powerquery 中的新列。

您的问题描述不当,但下面的 powerquery 示例代码设置了年、月和桶的列。然后,您可以对存储桶列和其他列之一进行分组,以执行您想要的任何数学运算

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"idcustomer", Int64.Type}, {"year of birth", Int64.Type}, {"amount", Int64.Type}, {"salesdate", type date}}),

// add columns for year, month and year/month
// you could probably do some math on the year instead, but too lazy to figure it out

#"Added Custom1" = Table.AddColumn(#"Changed Type", "SalesYear", each Date.Year([salesdate])),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "SalesMonth", each Date.Month([salesdate])),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "MonthYear", each #date(Date.Year([salesdate]),Date.Month([salesdate]),1)),

// generate list of buckets from 1940 through 2050, every 5 years
List = Table.FromList(List.Transform(List.Generate(() => 1940, each _ < 2050, each _ + 5), each Text.From(_)), null, {"Bucket"}),
#"Added Custom" = Table.AddColumn(List, "Year", each {Number.From([Bucket]) .. Number.From([Bucket])+4 }),
#"Expanded Year" = Table.ExpandListColumn(#"Added Custom", "Year"),

 // add bucket column
#"Merged Queries" = Table.NestedJoin(#"Added Custom3", {"year of birth"}, #"Expanded Year", {"Year"}, "Table2", JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Bucket"}, {"Bucket"})
in #"Expanded Table2"

不太清楚你想要什么。

我假设你总是希望你的分组从五的倍数开始。

  • 根据四舍五入到最接近的五的倍数的最早出生日期创建分组列表。
  • 添加一个包含分组起始年份的列
  • 添加另一个包含该年份的列并创建您想要用于分组的文本

原始数据

M码

let
    Source = Excel.CurrentWorkbook(){[Name="Table24"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"idcustomer", Int64.Type}, {"year of birth", Int64.Type}, 
        {"amount", Int64.Type}, {"salesdate", type date}}),

//add grouping column depending on min/max year of birth

    //round firstYear down to a multiple of 5
    firstYear =Number.IntegerDivide(List.Min(#"Changed Type"[year of birth]),5)*5,
    lastYear = List.Max(#"Changed Type"[year of birth]),

    //create list of groupings
    groupings = List.Numbers(firstYear, Number.IntegerDivide(lastYear-firstYear,5)+1,5),

    //group for first year selected from the list
    #"First Year" = Table.AddColumn(#"Changed Type","firstYear",
        each List.Last(List.Select(groupings, (li)=> li <= [year of birth])), Int64.Type),

    //grouping column added as text
    #"Add Grouping Column" = Table.AddColumn(#"First Year","Grouper", 
        each Text.From([firstYear]) & "-" & Text.From([firstYear]+4),type text),

    //remove first year column
    #"Removed Columns" = Table.RemoveColumns(#"Add Grouping Column",{"firstYear"})
in
    #"Removed Columns"

结果