Apisero is now part of NTT DATA - Learn more.

Search
Close this search box.

Calculate “Room Nights” in DataWeave

Author: Sanket Kangle

We will see in this article how to create a payload corresponding to each date between the given starting date and End date in Dataweave. This scenario can be found in many industries across different business use cases. We will focus on the Hospitality industry in this article.

In the hospitality industry, there is a slang called “Room Nights”. It means the total number of rooms a planner commits to occupy at the hotel in exchange for a specific contracted rate. The number of room nights does not equal the number of attendees/is not concerned with the number of attendees but the rooms occupied. One attendee could occupy three room nights for a three-night hotel stay.

Our task here is to create a payload for each individual room night for an attendee.

Sample Input:

{ “guest_name”: “Mule Max”, “room”: “433”, “start_date”: “05/22/2020”, “end_date”: “05/25/2020”}

Expected Output:

[
{
“guest_name”: “Mule Max”,
“room”: “433”,
“date”: “05/22/2020”
},
{
“guest_name”: “Mule Max”,
“room”: “433”,
“date”: “05/23/2020”
},
{
“guest_name”: “Mule Max”,
“room”: “433”,
“date”: “05/24/2020”
},
{
“guest_name”: “Mule Max”,
“room”: “433”,
“date”: “05/25/2020”
}]

The approach to solve this use case is can be thought of a two step process:

  1. Creating an array of all the dates from the start date and end date
  2. Map over this array of dates and add other required fields

Step 1: Creating array of all the dates from start date and end date

We will define a function that will recursively call until we reach the last date. Then we will use create a variable where we will pass the input dates and get the required array of dates

Following is the dataweave script for the same

%dw 2.0output application/jsonfun noOfDays(startDate , endDate)={   y: startDate as String {format: “MM/dd/yyyy”},   x: if(startDate == endDate) startDate else noOfDays(startDate + |P1D|, endDate)} var dates = noOfDays(payload.start_date as Date {format: “MM/dd/yyyy”}, payload.end_date as Date {format: “MM/dd/yyyy”})..y—dates

The output that you get in dates variable is as below

[ “05/22/2020”, “05/23/2020”, “05/24/2020”, “05/25/2020”]

Step 2: Map over this array of dates and add other required fields

%dw 2.0output application/jsonfun noOfDays(startDate , endDate)={   y: startDate as String {format: “MM/dd/yyyy”},   x: if(startDate == endDate) startDate else noOfDays(startDate + |P1D|, endDate)} var dates = noOfDays(payload.start_date as Date {format: “MM/dd/yyyy”}, payload.end_date as Date {format: “MM/dd/yyyy”})..y—dates map(object, index) -> {   “guest_name”: payload.guest_name,   “room”: payload.room,   “date”: object}

With the above script, we get the required output.

This was all about creating a payload corresponding to each date between the given starting date and End date in Dataweave.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.