DataWeave Magic: Identifying Special Weekdays πŸ“…

Upendra Venkata Muralidhar Thunuguntla
Another Integration Blog
4 min readNov 13, 2023

Hey there, tech enthusiasts! Today, we’re diving into DataWeave and exploring a nifty little trick to identify some special weekdays. 🎩✨

Have you ever found yourself in a situation where you needed to figure out if today is the (last_working_day β€” 2) and a weekday? Maybe you wanted to automate a process that only runs on such specific days, or perhaps you were just curious about the magic of date manipulation in your data workflows. For these and other scenarios, DataWeave has got your back! πŸ€–

Imagine you’re working on a project where you need to trigger a particular action on the day two days before the last working day of the week, and that day also needs to be a weekday. Sounds like a bit of a puzzle. But with DataWeave, we can make this seemingly complex task a piece of cake! 🍰

Here’s a simple DataWeave logic that does the heavy lifting for you:

%dw 2.0
output application/json
import * from dw::core::Dates

var currentDate = now() as Date

var ld = do {
var lwd = (now() + |P1M|) as Date
var lwdMon = lwd as String {format: "MM"}
var lwdYr = lwd as String {format: "yyyy"}
---
(atBeginningOfMonth(lwd) - |P1D|) - |P2D|
}

fun weekday(date: Date) =
[1, 2, 3, 4, 5] contains date.dayOfWeek

fun getLwd(lastDay) =
if (isWeekday(lastDay))
last day
else
getLwd(lastDay - |P1D|)
---
getLwd(ld) == currentDate

This DataWeave script is like a little wizard that checks if today is the special day we’re looking for. It combines data manipulation and logical checks to give us a clear answer. πŸ§™β€β™‚οΈ

Let’s break down the script step by step:

%dw 2.0
output application/json
import * from dw::core::Dates

The script specifies the DataWeave version and the output format as JSON. It then imports the Dates module from the DataWeave core, which provides functions for working with dates.

var currentDate = now() as Date

The current date is captured and stored in the currentDate variable as a Date data type.

var ld = do {
var lwd = (now() + |P1M|) as Date
var lwdMon = lwd as String {format: "MM"}
var lwdYr = lwd as String {format: "yyyy"}
- -
(atBeginningOfMonth(lwd) - |P1D|) - |P2D|
}

This section defines a new variable, ld, using the do block. It calculates the second-to-last working day of the current month. It first calculates the last working day of the current month ( lwd) by adding one month to the current date and then finding the beginning of the month and subtracting one day to get Last working day of current month. Then subtracts 2 Days.

fun isWeekday(date: Date) =
[1, 2, 3, 4, 5] contains date.dayOfWeek

This section defines a function isWeekDay that checks if a given date is a weekday (Monday to Friday) by comparing its dayOfWeek property with the array of weekday numbers.

fun getLwd(lastDay) = 
if (isWeekday(lastDay))
lastDay
else
getLwd(lastDay - |P1D|)

Here, a recursive function getLwd is defined to find the last working day of the month. It checks if the given date is a weekday. If it is, the function returns that date. If not, it calls itself with the previous day until it finds a weekday.

getLwd(ld) == currentDate

Finally, the script checks if the last working day of the month is equal to the current date, indicating that today is the second-to-last working day of the month and a weekday.

We can use the Boolean TRUE | FALSE output to determine subsequent actions in the flow.

In summary, this script uses DataWeave to calculate the second-to-last working day of the month and then checks if it’s a weekday. This provides apowerful way to identify specific special weekdays in your data workflows.

Now, let’s discuss some cool use cases for this magic trick! 🌟

  1. Automated Reports: You can use this logic to automate the generation of reports on the specific day you’re interested in. For example, maybe you want to generate a weekly summary report on the second-to-last working day of the week.
  2. Scheduled Tasks: If you have tasks that need to run on a particular weekday, two days before the end of the workweek, this logic can help you schedule them effortlessly.
  3. Special Notifications: Want to send out special notifications to your team on a specific weekday that falls just before the weekend? This logic can help you identify that day with ease.

So, there you have it! With a little DataWeave magic, you can identify those special weekdays and make your data workflows even more powerful. 🌈✨

Stay curious, keep coding, and may your weekdays be as special as you need them! πŸš€

--

--

Upendra Venkata Muralidhar Thunuguntla
Another Integration Blog

πŸ‘¨β€πŸ’» Creating Solutions πŸš€ Bridging the Gap Between Technology and Imagination πŸ’‘ using π™Όπšžπš•πšŽπš‚πš˜πšπš | π™ΉπšŠπšŸπšŠ | π™Ώπš’πšπš‘πš˜πš— | πš‚πšŠπš•πšŽπšœπšπš˜πš›πšŒπšŽ