Table.Group 分组
上题:如果行与行之间有重复值则分为一组:
= Table.Group(更改的类型, {"列1","列2"}, {{"计数", each _ }},0,
(x,y)=>Number.From(
[fx=(a)=>Table.PositionOf(更改的类型,a) ,
e = Record.FieldValues(更改的类型{fx(y)}),
b = Record.FieldValues(更改的类型{fx(y)-1}),
c = List.IsEmpty(List.Intersect({e,b}))][c] ))
结果如下:
上题:条件再缩小点,难度会更大点
结果:
代码:
= Table.Group(更改的类型, {"列1","列2"}, {{"计数", each _}},0,
(x,y)=>Number.From(
[fx=(x)=>Table.PositionOf(更改的类型,x) ,
a=更改的类型{fx(y)}[列1]<>更改的类型{fx(y)-1}[列2] and
更改的类型{fx(y)}[列1]<>更改的类型{fx(y)-1}[列1]][a]))
案例1
核心代码:
解法1
= Table.Group(更改的类型, "列1", {{"计数", each Table.Skip(_,1)}},0,(x,y)=>Number.From(Text.At(x,0)<>Text.At(y,0)))
解法2:
let
源 = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
自定义1 = List.Accumulate(Table.ToRows(源),{{},{}},(x,y)=>if x{1}={} then {x{0},y}
else if Text.Start(y{0},1)=Text.Start(x{1}{0},1) then {x{0}&{List.FirstN(x{1},1)&y},x{1}}
else {x{0},y}){0},
自定义2 = Table.FromRows(自定义1)
in
自定义2
解法3:
let
源 = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
自定义1 = List.Accumulate(Table.ToRecords(源),[p={},m=[]],(x,y)=>if x[m]=[] then [p =x[p],m=y]
else if Text.At(y[列1],0)= Text.At(x[m][列1],0) then [p=x[p]&{[列=x[m][列1]]&y},m=x[m]] else [p=x[p],m=y])[p],
自定义2 = Table.FromRecords(自定义1)
in
自定义2
自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)