Mulesoft: remove duplicate entries by checking dateField using dataweave
I have a csv file with the following data:
Id,Name,Type,date 1,name1,employee,25/04/2017 2,name2,contrator,26/04/2017 3,name3,employee,25/04/2017 4,name4,contrator,26/04/2017 5,name5,employee,24/04/2017 6,name6,contrator,24/04/2017 7,name7,employee,25/04/2017 8,name8,contrator,24/04/2017 9,name9,employee,24/04/2017 10,name10,contrator,26/04/2017 6,name6,employee,27/04/2017 11,name11,employee,27/04/2017 12,name12,contrator,27/04/2017
If it has two lines with the same id number. One of the lines should be removed by checking the last date. The line with the old date should be removed. For example, above the data entry, there are two rows of data with ID # 6. The line with date 24/04/2017 should be deleted. The result should look like this:
Id,Name,Type,date 1,name1,employee,25/04/2017 2,name2,contrator,26/04/2017 3,name3,employee,25/04/2017 4,name4,contrator,26/04/2017 5,name5,employee,24/04/2017 6,name6,employee,27/04/2017 7,name7,employee,25/04/2017 8,name8,contrator,24/04/2017 9,name9,employee,24/04/2017 10,name10,contrator,26/04/2017 11,name11,employee,27/04/2017 12,name12,contrator,27/04/2017
I need to achieve this using Dataweave. Please provide me with a solution or suggestions.
+3
source to share
1 answer
here is the information you are looking for:
%dw 1.0
%output application/csv
%var toDate = (str) -> str as :date { format: "dd/MM/yyyy" }
%var maxDate = (a, b) -> a when toDate(a.date) > toDate(b.date) otherwise b
---
payload groupBy $.Id
pluck $ map ($ reduce ((val, acc) -> maxDate(val, acc)))
+4
source to share