11 Replies Latest reply: Nov 26, 2014 12:55 AM by anbu cheliyan

Database contains: FromDate, ToDate: Show correct values for month in between?

Hello everyone,

I need help showing the correct data from database.

The structure of the database is as following:

ID     FromDate     ToDate               Costs

1     10.02.2014     30.06.2014         500€

The problem is:

How can I show the correct data when I want to see the cumulated values for May in example 1?

As QV does not recognize there are any data in there. The correct costs for that month should be 500€ / 140 * 31 = 110,71€.

• Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Initial:

Load ID,Date#(FromDate,'DD.MM.YYYY') As FromDate,Date#(ToDate,'DD.MM.YYYY') As ToDate,Costs Inline [

ID,FromDate,ToDate,Costs

1,10.02.2014,30.06.2014,500

2,25.05.2014,28.05.2014,22 ];

Final:

Load YearMth,If(MthDiff=1,Costs,If(FromDate >= MthStrt And FromDate <= MthEnd, Costs * Interval(MthEnd-FromDate,'D'),If(ToDate >= MthStrt And ToDate <= MthEnd, Costs * Interval(ToDate-MthStrt,'D'),Costs * Interval(MthEnd-MthStrt,'D'))));

\$(MonthDiff(FromDate,ToDate)) As MthDiff

Resident Initial While IterNo() <= \$(MonthDiff(FromDate,ToDate));

• Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Thank you a lot for your work!

Where do I have to put these formulas? (Especially what does Initial and Final mean?)

Putting these data into the script gives me an error that there is a missing ')' in the 'Final' code.

• Re: Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Initial - Input table

Final - Final table

I missed following in previous post

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

• Re: Re: Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Thank you very much for clarification

It works. There is a minor mistake in it: it generates a YearMth field vor every entry although the entry may not even contain that YearMth.

Example: 201406 is being created for ID=2.

Therefore there are false values for all entries (see attached file).

How to fix that?

• Re: Database contains: FromDate, ToDate: Show correct values for month in between?

201406 in this pic is for ID=1. Last row 201405 is for ID=2.

• Re: Database contains: FromDate, ToDate: Show correct values for month in between?

The problem is that QV is making a cross table as soon as I add the ID to any sort of table (which gives false values).

• Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Can you post sample app with your issue.

• Re: Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Yes.

• Re: Re: Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Added ID to Final table. Check this app

• Re: Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Thank you a lot! Saved my day!

I am relatively new to QV, can you explain to me the Final table?

Why is it separated into two Load´s? Is it possible to put it into one table?

In the last line we use resident to load initial again...Why do we load initial and not final as there are the costs?

• Re: Re: Re: Database contains: FromDate, ToDate: Show correct values for month in between?

Outer Load in Final table is called Preceding Load. Preceding Load is used in this case, so we don't need to do calculate MthStrt and MthEnd multiple times in Cumulated_Costs calculation.

To create one table, use Join like this or create Precedent load on Inline Load

1. Using Join

Initial:

..

2,25.05.2014,28.05.2014,22 ];

Join(Initial)

YearMth,

....

Resident Initial While IterNo() <= \$(MonthDiff(FromDate,ToDate));

Or

2. Precedent Load on Inline table

Initial:

FromDate,

ToDate,

YearMth,

If(MthDiff = 1,

Costs,

If(FromDate >= MthStrt And FromDate <= MthEnd,

Costs * Interval(MthEnd-FromDate,'D'),

If(ToDate >= MthStrt And ToDate <= MthEnd, Costs * Interval(ToDate-MthStrt,'D'),

Costs * Interval(MthEnd-MthStrt,'D')))) As Cumulated_Costs,

ID;

FromDate,

ToDate,

(Costs/If(\$(MonthDiff(FromDate,ToDate)) = 1,

1,

Interval(ToDate-FromDate,'D'))) As Costs,

\$(MonthDiff(FromDate,ToDate)) As MthDiff,

ID

While IterNo() <= \$(MonthDiff(FromDate,ToDate));

Date#(FromDate,'DD.MM.YYYY') As FromDate,

Date#(ToDate,'DD.MM.YYYY') As ToDate,

Costs Inline [

ID,FromDate,ToDate,Costs

1,10.02.2014,30.06.2014,500

2,25.05.2014,28.05.2014,22 ];

Initial table is the source for Final table. Resident load is used to create Final table.