Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
beck_bakytbek
Master
Master

Differences in Years and in Months

Hi Folks, i have a question:

my table does look like:

Project     ReportDate     EndDate      Difference in Months

A            31.03.2018     30.06.2021          39 Months

I am using this function:

step1 : SET MonthDiff = Num(((year($2) * 12) + month($2))-(((year($1) * 12) + month($1))) + 1)

step2: $(MonthDiff(ReportDate, EndDate)) AS MonthsDifference

with this Function i can calculate only differences as interval 40 Months.

My expected Output is

Project  

A                

Restmonth this Year   = 9

Restmonth this Year + 1 Year = 12

Restmonth this Year + 2 Years = 18

Sum: 39 Months

Does anybody have any idea how to resolve this issue?

Thanks a lot

Beck

1 Solution

Accepted Solutions
sunny_talwar
MVP
MVP

May be this

test:

LOAD *,

$(MonthDiff(Start, EndDate)) AS MonthsDifference;

load *,

MonthStart(ReportDate-1, 1) as Start

inline [

Projekt, ReportDate, EndDate

A, 31.03.2018, 30.06.2021

B, 20.02.2017, 30.06.2021

C, 20.03.2016, 30.06.2021

];


Capture.PNG

View solution in original post

10 Replies
sunny_talwar
MVP
MVP

Is this right?

Capture.PNG

Shouldn't it be 12 for Year + 2 and then 6 for Year + 3?

sunny_talwar
MVP
MVP

Something like this

SET MonthDiff = Num(((year($2) * 12) + month($2))-(((year($1) * 12) + month($1))) + 1);


Table:

LOAD *,

$(MonthDiff(Start, End)) AS MonthsDifference;

LOAD *,

Date(RangeMax(ReportDate + 1, YearStart(ReportDate, IterNo() - 1))) as Start,

Date(RangeMin(EndDate, YearStart(ReportDate, IterNo())-1)) as End

While YearStart(ReportDate, IterNo() - 1) <= EndDate;

LOAD * INLINE [

    Project, ReportDate, EndDate

    A, 31.03.2018, 30.06.2021

];

beck_bakytbek
Master
Master
Author

Hi Suny,

thanks a lot for your responce and your time,

yes you right, but if you see the Monthdifference between is

ReportDate     EndDate      Difference in Months

31.03.2018     30.06.2021          39 Months

9 + 12 + 18 = 39

beck_bakytbek
Master
Master
Author

Hi Sunny,

i have implemented this, but it does look a bit strange. i attached the qfv, can you take a look?

sunny_talwar
MVP
MVP

Can you point out what is not right here?

Capture.PNG

beck_bakytbek
Master
Master
Author

Hi Sunny,

i've analysed, everything is correct, my only question is, what if, if i have more then 100 projects, then the field project will be multiplied. is that correct?

sunny_talwar
MVP
MVP

Yes it will... but isn't that what you wanted to see?

beck_bakytbek
Master
Master
Author

i wanted only the calculation (differences in year and month), is that possible to avoid the multiplication of field: project?

sunny_talwar
MVP
MVP

May be this

test:

LOAD *,

$(MonthDiff(Start, EndDate)) AS MonthsDifference;

load *,

MonthStart(ReportDate-1, 1) as Start

inline [

Projekt, ReportDate, EndDate

A, 31.03.2018, 30.06.2021

B, 20.02.2017, 30.06.2021

C, 20.03.2016, 30.06.2021

];


Capture.PNG