A QV best practice would be to create a month column,
month(CS_DATE) as Month
and create the cross table on in the QV interface which is really simple.
Otherwise, you could try in the script to make a join between the detailed table with an aggregated table,
MONTH(CF_DATE) AS Month,
sum(if(Month = 1,COST_PER_DAY,0)) as January,
sum(if(Month = 2,COST_PER_DAY,0)) as February,
Group by Month;
Thanks for that Karl, however I don't think this is performing the calculation as needed. What i need to do is multiply the cost per day by the amount of days in the month where the cost will be incurred.
For example on "serial No" A-317 above the value in the "January" column will be Cost per day * (last day in january - cost start date)
so effectively it will be $7,891.35*(31-10) = $165,718.35
I have it working in Excell but i am limited there and would like to pull this into Qlikview to simplify the process and expand on my data.
That is the beginnings of it Karl... you are a saint.
I don't have experience with loops but i think this would be a perfect place to put a loop. What i need to do is create a column for each YYYY_MM in my record set. so rather than having to type the if statement fore each YYYY_MM can't i do a loop that would go through all the records and populate that fore me?
so really i would have multiple January columns. I can restrict the script to only look 18 months out to keep it from having to work too hard. IF i restrict it to 18 months the most i would have is two of any month column.
Here's an idea using a loop. You'll have to create an empty table that we will concatenate to in the loop. Then in the loop we're going to concatenate each of the 18 past months to the empty table, and each month will create a new column that corresponds to the month and year of the CS_DATE. This solution does not consider having information older than 18 months in the table.
Note: This isn't tested so try it out and if you have any more questions, please ask.
Also, I don't know how you are going to present this in the interface. Having variable column names doesn't give you the easist way to build and maintain the QV interface, so it's worth noting that what you are calculating can also be done with a sum(if(...)) in the interface without creating these additional columns.
Load null() as SERIAL_NO,
null() as CS_DATE,
null() as AVG_TAT,
null() as CF_DATE,
null() as COST_PER_DAY
for vCounter = 0 to 17
set vMonthName = monthname(today(),-vCounter);
set vMonthYear_ColumnName = text(monthname(today(),-vCounter));
COST_PER_DAY * (day(monthend(CS_DATE)) - day(CS_DATE)) as [$(MonthYear_ColumnName)]
Where monthname(CS_DATE) = $(vMonthName);
Thanks for all the help so far. I have been tinkering with the script and have come up with the following which is working to calculate the current month cost by multiplying the cost per day by the amount of days the work is to be forecasted for the current month with multiple IF statements. What I am trying to do is to forecast 18 months into the future. This is contrary to the loop examply you sent which is pulling historical data. I am wondering if there is a better method of doing this than the script i have below. Also what i would like to do is turn the IF statements below into a loop which would perform the same calculation for the next 17 months and name the column for that month. Any help is appreciated.
these values are then used to calculate the total $ in cost that will occur for each month on each record in the load. That value is placed in a new colum named for the month as outlined above (YYYY_MM). As Carl stated above this has limited how i can use the data in reporting and i would rather have all $ values in a single column labled M onthly Cost and another column labeld Month that would indicate which month these dollars will occur in. I am certain this can be simplified and done in a loop however i am not familiar with loops and I have surely overcomplicated this.
what i currently have is with the attached script in its simplist form is:
Part Number Serial Number Cost Start Date Cost Finish Date cost per day total cost 2012_02 2012_03 2012_04 2012_05 X Y 2/2/2012 4/4/2012 $ 1.00 $ 88.00 $ 27.00 $ 31.00 $ 30.00 $ -
what i would like to have for the same data would be :
Part Number Serial Number Cost Start Date Cost Finish Date cost per day total cost Month Cost X Y 2/2/2012 4/4/2012 $ 1.00 $ 88.00 2012_02 $ 27.00 X Y 2/2/2012 4/4/2012 $ 1.00 $ 88.00 2012_03 $ 31.00 X Y 2/2/2012 4/4/2012 $ 1.00 $ 88.00 2012_04 $ 30.00
QV SCRIPT.docx 41.1 K