Hi @Synergetic,
Didn’t have any data for this file so I worked of the previous one…
In the Transformed step I’ve added some additional logic to create a “Reminder” flag: so if last consecutive number of years >=3 AND the max year = this year-1 then it returns true else false.
let
Source = Excel.Workbook(File.Contents(FileLocation), null, true),
AllRecordedDonationsTable_Sheet = Source{[Item="AllRecordedDonationsTable",Kind="Sheet"]}[Data],
PromoteHeaders = Table.PromoteHeaders(AllRecordedDonationsTable_Sheet, [PromoteAllScalars=true]),
ChangeType = Table.TransformColumnTypes(PromoteHeaders,{{"Donor ID", Int64.Type}, {"Receipt Amount", Int64.Type}, {"Receipt Date", type datetime}, {"Constuency Codes", type text}, {"Column5", type text}, {"Column6", type text}}),
ExtractYear = Table.TransformColumns(ChangeType,{{"Receipt Date", Date.Year, Int64.Type}}),
RenameColumn = Table.Buffer( Table.RenameColumns(ExtractYear,{{"Receipt Date", "Receipt Year"}})),
GroupRows = Table.Group(RenameColumn, {"Donor ID"}, {{"AllRows", each _, type table [Donor ID=nullable number, Receipt Amount=nullable number, Receipt Year=number, Constituency Codes=text]}}),
Transformed = Table.AddColumn(GroupRows, "Temp", each
let
myTable = [AllRows],
cYear = Date.Year( DateTime.FixedLocalNow()),
GroupRows = Table.Group(myTable, {"Donor ID", "Receipt Year"}, {}),
SortRows = Table.Sort(GroupRows,{{"Receipt Year", Order.Ascending}}),
AddIndex = Table.Buffer( Table.AddIndexColumn(SortRows, "Index", 0, 1, Int64.Type)),
AddCustom = Table.AddColumn(AddIndex, "Custom", each
try if [Index] <> List.Max(AddIndex[Index])
then [Receipt Year] = AddIndex[Receipt Year]{[Index]+1}-1
else [Receipt Year] = AddIndex[Receipt Year]{[Index]-1}+1
otherwise false),
NewGroup = Table.Group(AddCustom, {"Donor ID", "Custom"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"AllRows", each _, type table [Donor ID=nullable number, Receipt Year=number, Index=number, Custom=logical]}}, GroupKind.Local),
RemindTest = Table.AddColumn(NewGroup, "RemindTest", each let LastDonation = if [Custom] = true then List.Max( List.Buffer([AllRows][Receipt Year] )) else null in if [Custom] = true and LastDonation = cYear-1 and [Count] >= 3 then true else false, type logical),
RemindResult = Table.AddColumn(RemindTest, "Remind", each List.AnyTrue(RemindTest[RemindTest]), type logical ),
ConsecutiveYears = Table.AddColumn(RemindResult, "Consecutive Years", each if [Custom] = true then [Count] else 1, Int64.Type)
in
ConsecutiveYears, type table [Donor ID=Int64.Type, Custom=logical, Count=Int64.Type, AllRows=table, RemindTest=logical, Remind=logical, Consecutive Years=Int64.Type])[[Temp]],
ExpandTable = Table.ExpandTableColumn(Transformed, "Temp", {"Donor ID", "Consecutive Years", "Remind"}, {"Donor ID", "Consecutive Years", "Remind"}),
MaxConsecutiveYears = Table.Group(ExpandTable, {"Donor ID", "Remind"}, {{"Max Consecutive Years", each List.Max([Consecutive Years]), type nullable number}})
in
MaxConsecutiveYears
.
I hope performance wise it’s still okay… let me know.