Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
imsushantjain
Partner - Creator
Partner - Creator

How to find days between two dates for each month of a year?

Hi Guys,

We have data that currently exist in this format:

S.NoStart Date End Date
115-Jan-202020-Feb-2020
210-Jan-202025-Mar-2020
315-Feb-202020-Jun-2020

 

My user is interested in looking at Number of Days between Start and End Date by Month for each Year. How can we achieve the below  output using formulas or any other approch:

S.NoStart Date End DateJan-20Feb-20Mar-20Apr-20May-20Jun-20
115-Jan-202020-Feb-20201620    
210-Jan-202025-Mar-2020212925   
315-Feb-202020-Jun-2020 1431303120

 

Thanks

Sushant

Labels (2)
1 Solution

Accepted Solutions
Kushal_Chawda

@imsushantjain  try below

Data:
LOAD [S.No],
     [Start Date], 	
     [End Date],
     date([Start Date]+IterNo()-1) as Date ,
     date(monthstart([Start Date]+IterNo()-1),'MMM YYYY') as Month,
     RowNo()&num([Start Date]+IterNo()-1) as Key
FROM table
while [Start Date]+IterNo()-1<=[End Date];

 

Below is the pivot table view with expression  count(distinct Date)

Screenshot 2020-09-14 164020.png

If you don't want the Day level data in table the you can aggregate it to month level  and calculate days in load script to reduce the number of records. Script will look something like below

Data:
LOAD [S.No],
     [Start Date], 	
     [End Date],
     date([Start Date]+IterNo()-1) as Date ,
     date(monthstart([Start Date]+IterNo()-1),'MMM YYYY') as Month
FROM table
while [Start Date]+IterNo()-1<=[End Date];

Final:
load [S.No],
     [Start Date], 	
     [End Date],
     Month,
     count(distinct Date) as Days
resident Data
group by [S.No],
     [Start Date], 	
     [End Date],
     Month;

 

Now you can simply use sum(Days) in your expression

View solution in original post

2 Replies
Kushal_Chawda

@imsushantjain  try below

Data:
LOAD [S.No],
     [Start Date], 	
     [End Date],
     date([Start Date]+IterNo()-1) as Date ,
     date(monthstart([Start Date]+IterNo()-1),'MMM YYYY') as Month,
     RowNo()&num([Start Date]+IterNo()-1) as Key
FROM table
while [Start Date]+IterNo()-1<=[End Date];

 

Below is the pivot table view with expression  count(distinct Date)

Screenshot 2020-09-14 164020.png

If you don't want the Day level data in table the you can aggregate it to month level  and calculate days in load script to reduce the number of records. Script will look something like below

Data:
LOAD [S.No],
     [Start Date], 	
     [End Date],
     date([Start Date]+IterNo()-1) as Date ,
     date(monthstart([Start Date]+IterNo()-1),'MMM YYYY') as Month
FROM table
while [Start Date]+IterNo()-1<=[End Date];

Final:
load [S.No],
     [Start Date], 	
     [End Date],
     Month,
     count(distinct Date) as Days
resident Data
group by [S.No],
     [Start Date], 	
     [End Date],
     Month;

 

Now you can simply use sum(Days) in your expression

imsushantjain
Partner - Creator
Partner - Creator
Author

Thank You @Kushal_Chawda , your solution works like a charm. 😀