QlikView Conditional SUM current value - conditional-statements

I have a pivot table with multiple rows dimensions (District, Region, Shop) and months in columns. I need to calculate sales Growth ((sales this month - sales previous month)/sales previous month). As You can see from formula, i need a value from previous month (previous column). I have a formula for testing:
Sum(Total {<District={'District1'}, [Month]={'2015 09'}>} Quantity)
With this formula i am able to get the value from previous column (lets say i am calculating growth in month 2015 10 and District1). The problem is, that i get a wrong value, when this formula is used in a row with different District.
Is there any way to get district value of current row and use it in a formula? I tried multiple variations like:
Sum(Total {<District={$(District)}, [Month]={'2015 09'}>} Quantity)
Sum(Total {<District={$<=District>}, [Month]={'2015 09'}>} Quantity)
but none of them work

There is no need to add District in the calculation. QV will aggregate to it automatically since District is dimension already.
If you want to aggregate all rows under each District to show the total quantity for the whole District then you can use the following expression:
sum( total <District> Quantity )
The picture below demonstrate the result:
Also you can check the QV help (c:\Program Files\QlikView\English.chm) for the Aggr() function as well. With Aggr() function the same result can be achieved with:
aggr( NoDistinct sum( Quantity ), District )

Related

Trying to create a well count to compare to BOE using the on production date and comparing it to Capital spends and total BOE

I have data that includes the below columns:
Date
Total Capital
Total BOED
On Production Date
UWI
I'm trying to create a well count based on the unique UWI for each On Production Date and graph it against the Total BOED/Total Capital with Date as the x-axis.
I've tried unique count by UWI but it then populates ALL rows of that UWI with the same well count total, so when it is summed the numbers are multiplied by the row count.
Plot Xaxis as Date and Y with Total BOED and Well Count.
Add a calculated column to create a row id using the rowid() function. Then, in the calculation you already have, the one that populates all rows of the UWI with the same well count, add the following logic...
if([rowid] = min([rowid]) over [UWI], uniquecount([UWI]) over [Production Date], null)
This will make it so that the count only populates once.

Total Average, ignoring GROUP BY

I created a query in Microsoft Access such as the one below:
SELECT LoanType
,Avg(Loan Amount)
,Avg(Loan Rate)
FROM Table1
GROUP BY LoanType
The output is as you would expect, the average loan amount and the average loan rate for each loan type.
However, I'd like for my Access Report to calculate the average of all the loans, regardless of loan type, and place this row at the very bottom. Using the Report view in Access, you can add a "Totals" row where you can write a formula such as COUNT(), SUM(), AVG(). But as you know, calculating an average of an average goes against basic math.
I'm assuming I have to create this "Totals" row at the SQL/Query level. But I can't seem to figure it out. Any input would be greatly appreciated!
You can use UNION ALL to add a row with a similar query, just without the GROUP BY and a NULL for LoanType (or any other value you like as long as it's implicitly castable to the data type of LoanType).
SELECT LoanType,
Avg(Loan Amount)
Avg(Loan Rate)
FROM Table1
GROUP BY LoanType
UNION ALL
SELECT NULL,
Avg(Loan Amount)
Avg(Loan Rate)
FROM Table1;
Of yourse you can do exactly what you described: Build a query that calculates the averages by LoanType, build a report on this query and calculate an average in the report footer. Unfortunately, this "total average" will be an average of averages, but I guess you want an average over all records. To achieve this:
Base your report on Table1.
Create a group to group by LoanType.
Calculate the averages by LoanType in the group footer.
If you don't want to see the details, set the details section to be invisible.
Calculate the "total averages" in the report footer.

Crystal Report formula to reset a count?

I want to get the total qty of items sold by year by each part number. So far I have the following formula:
if {INVOICE_DATE} >= DateTime(2012, 01, 01) and
{INVOICE_DATE} <= DateTime(2012, 12, 31)
THEN Sum ({INVOICE_QTY)
Now, that formula just sums all the parts sold in that year. What I need is for it to reset the sum as it count through each invoice and by each part. How can I do this?
Your formula is not doing what you think it is. For each row in your report (I'll assume that this is by an Invoice_ID or something along those lines) this will simply display the grand total sum of {INVOICE.QTY} for all invoices in the report and will not change from row to row.
To get something that is like your image, you need to create a Cross-Tab object and configure it such that its rows are based on the Product_ID, the columns are based on Invoice_Date and set to be printed "for each year", and set the "Summarized Fields" to be the sum of {INVOICE.QTY}.
I guess you are not grouoing the report hence you are getting the grand total using that formula. What you can do is:
First create a group by Year and then by Part.
In details place your fields that need to be summed and supress the detail part.
Take sum in group footer 2 (For parts summary) and Group footer 1 (For year summary).

Conditional Sum Based on Count

I have reviewed the various 'Conditional Sum' questions in the forum and none quite match what I'm trying to do:
In the database, there is Date, Store#, Item#, and %Total Sales. In some cases, the same item# for the same date may be given more than one value for %Total Sales. (For some reason this is a valid business scenario that happens rarely, but it happens.)
In that situation only, the requirement is to sum the two values together into one line. So if Item# 123 has a line with a value of .05%, and another line with a value of .08%, I need to sum those two values into one line for Item #123 that has a %Total of .13%. ONLY when an item has more than one percentage assigned, those percentages should be summed. Otherwise, if an item# has only one percentage value assigned, we just want to see that value.
I cannot figure out how to do this. Basically, I would like to implement logic that would work like this:
SELECT Date, Store#, Item#,
CASE WHEN Count(%Total Sales) >1 THEN Sum(%Total Sales)
ELSE %Total Sales
END
FROM (some tables and joins)
GROUP BY Date, Store#, Item#
However, I'm not sure how to craft it so that I don't get a syntax error (this query produces errors).
Any help would be appreciated.
Thank you!
Provided you group by Date, Store#, Item# the only values that return multiple lines are those with multiple values for Total Sales of the same item on the same date.
Therefore grouping the items should be sufficient.
SELECT Date, Store#, Item#, Sum(%Total Sales)
FROM (some tables and joins)
GROUP BY Date, Store#, Item#
Why don't you make an update instead of entering a new value in the table.
Rather than manipulating of the data after insertions.
I believe this is a better idea that you can check for the existing date and update the percentage at that moment itself.
Is it not possible?

MDX- Divide Each row by a value based on parent

I am in a situation where I need to calculate Percentage for every fiscal year depending on distinct count of the rows.
I have achieved the distinct count (fairly simple task) for each year city-wise and reached till these 2 listings in cube.
The first listing is state wide distinct count for given year.
Second listing is city wise distinct count for given year with percentage based on state-wide count for that year for that city.
My problem is that I need to prepare a calculated member for the percentage column for each given year.
For eg, In year 2009, City 1 has distinct count of 2697 and percentage raise of 32.94%. (Formula used= 2697/8187 ).
I tried with ([Measures].[Distinct Count])/(SUM(ROOT(),[Measures].[Distinct Count])) but no luck.
Any help is highly appreciated.
Thanks in advance.
PS: City wide sum of year 2009 can never be equal to statewide distinct count of that year. This is because we are calculating the distinct count for city and state both.
You need to create a Region Hierarchy for this, like State -> City. The create a calculation like below. Then in the browser put your Hierarchy on the left and the sales and calculated percentage in values.
([Dim].[Region].CurrentMember, [Measures].[Salesamt]) /
iif(
([Dim].[Region].CurrentMember.Parent, [Measures].[Salesamt]) = 0,
([Dim].[Region].CurrentMember, [Measures].[Salesamt]),
([Dim].[Region].CurrentMember.Parent, [Measures].[Salesamt])
)