Running total on calculated column in SQL Server - sql

I am using SQL code to get the running total of a column which repeats itself, in my screen shot below, the column WeightedBilled is calculated and I use it on the report on group level as MAX(WeightedColumn).
Now I want to add a column that adds the values of WeightedBilled and pick up just one value of the said column.
When I use over partition by clause, it adds all repeating values.
My code is:
SUM( WeightedBilled) OVER (PARTITION BY(CheckProjID), rpt_tEmployeeName) AS EmpLevelWeightedBill`,
On the SSRS 2005 report side: the dark background is where I want the sum of the values.
Employee $2000 (this is what I need here)
Project 1 $500 (displayed as max(weightedBilled))
Project 2 $600 (displayed as max(weightedBilled))
Project 3 $700 (displayed as max(weightedBilled))
Project 4 $200 (displayed as max(weightedBilled))
[Report Layout]

You question is not that clear but assuming you want to add up the max values of a field and also assuming that you have a row group by employee called EmployeeRowGroup then the expression would look like this.
=SUM(MAX(Fields!WeightedBilled.Value, "EmployeeRowGroup"))
The row group is case sensitive and must be enclosed in double quotes.

Related

Conditional styles on a calculated value in Cognos

I have a report (Pivot table) for which I have a value everyday. My goal is to calculate the variance every day and highlight with conditional styles.
1 jan. 23
2 jan. 23
3 jan. 23
5
25
42
I have a query who calculates the variance between days in % (which I dont want to put in the report).
My conditional style rule is [Table].[Pourcentage variation D-1] between 0,05 and 10.
Since my request is not in displayed my report, I have this error :
RSV-VAL-0032 The following expression is not valid. If the item exists
in a query but is not referenced in the layout, add it to a property
list.
The problem is that I don't have the "Proprety list" in my pivot table like in can have in a Data table.
How can I highlight my variance based on a request that I dont display in my Pivot table ?
Thanks for helping
Add the calculated field to the pivot table (I know you do not want this in the layout, this is just to test if that removes the error). See if the conditional style works the way you expect.
If so, change the column for the percentage data item to property, set box type to none. This way it is still something that can be referred to, but is hidden from the final result

SSRS insert exact specific value in a cell of a Matrix using expression

I'm not sure if my question is really stupid, but I found nothing on the internet...
Is it possible to insert a specific value in a cell of a matrix?
for example I have a dataset like below:
Month Prod Amount
2 X 34$
11 Y 12$
7 Z 150$
and a matrix like:
-------| Month |
Prduct |SUM(Amount)|
So the row group are products and column group are the months of a specific year.
If I want to add an extra column, with a specific value chosen dynamically from the amount (for xample 150$) so to have
-------| Month |columnName
Prduct |SUM(Amount)| 150
is that possible? also if the value is repeated through the column (it would be useful if I wanted the new column to have this specific value added for each value)
thanks a lot!! :D
You can insert a value directly in your matrix but it will be repeated for each record.
The best way is to add a new column with conditional values is to do this in your dataset query. Probably with a CASE statement if you are using SQL.
EDIT: If you can't adjust the query for whatever reason, you can add the new column and use SWITCH function inside your textbox to achieve the same.

Variance column in QlikView

I need to create a "Variance" column in qlikview:
2013 2014 Variance
Measure 1 100 110 10%
Measure 2 105 100 -4.8%
...
Can this be done in Qlikview with just one "Calculated dimension" column that says something like:
[Value for Column2]/[Value for Column 1] - 1
So that it works for any new measures I add in the table and regardless of what the column 1 and column 2 are?
EDIT:
Sample Data:
Year Measure1 Measure2
2012 9750 197
2013 10000 200
2014 11000 210
2015 11500 215
I need the output to be structured as shown below with the Variance column as a calculation between 2 selected Year dimension values.
You can do this with the Column() function.
Column(2) / Column(1)
The number refers to the Expression column -- first Expression is #1, etc. Dimension columns are not counted.
An alternative that is insensitive to column postion, you can use column labels in the expression. Assume expressions with labels of "Sales" & "Margin". The variance expression can be written as:
[Margin] / [Sales]
I am 99% certain there is no built in function to do it.
I've played with a few options I thought might work, but only this is proving of any use.
I've defined the 2 calculated dimensions as variables Year1 and Year2 which I change through an input box. Then the simple calculated dimension =[$(Year2)]-[$(Year1)] gives me a new dimension of the variances.
This assumes the Measure is something that comes from the data, something like this, and now you just want to display it over varying years. I haven't considered what to do if the measures are all expressions.
Here is how to do it using variables and expressions. This will create 2 new columns that will look like a new dimension but will actually be defined by the use of an if() statement.
First step in the script we need to create a dimension that has the Measures in it. this should not associate to any of the column already in the data. (UDR stands for User Defined Report). the dual just allows us to define a sort order that is not alphabetical.
UDR:
load dual(UDR,Sort) as Rows inline [
UDR, Sort
Measure1, 1
Measure2, 2];
the result should be something like this.
Next step is creating the 2 variables for the years.
I do this in the script, but you could use any method of variable creation.
set vMaxYear="=max(Year)";
set vMinYear="=min(Year)";
Now we use the field Rows as the dimension.
And we need to create an expression for the max selected year. Notice that the first if() is testing which rows of the UDR the expression is on and then the definition of the expression for that line is provided. The second if() is testing the year dimension against the variable vMaxYear which will change as selections are made. Min year is the same just replace vMaxYear with vMinYear.
if(Rows='Measure1',sum(if(Year=vMaxYear,Measure1)),
if(Rows='Measure2',sum(if(Year=vMaxYear,Measure2))))
Lastly we use the column() function to calculate the variance in the third expression.
column(1)/column(2)
To make the expression labels dynamic I simply add =vMinYear into the label.
The result is this table, that will respond to my selections in the year list box.
2013 vs 2014
2014 vs 2015

Cognos - Conditionally hide a column, and summarise the remaining columns

I have a report with a static choice on the prompt page. The user can choose 'Full Detail', or 'Summarised'.
For a simplified example, say my report has these columns: Customer, Product, Date, Quantity, Value.
I would like to be able to show/hide the Date column based on the detail level choice, and have the Quantity and Value columns aggregate into a single Customer/Product line. I know how to show/hide the column (tying the choice variable to the column's Render Variable), but this does not do the aggregation, only makes the column invisible.
I have thought about doing a separate report page for Full Detail and Summary, but in my actual report I have a second choice box with which the user can choose a field to summarise by (e.g. Customer or Product), and the report will section-group by that field. At the moment I am doing that one per page (5 of them). Doing the detail choice the same way would mean I would need 10 pages. There is surely a better way.
Full detail:
Customer Product Date Qty Value
ABCD Things 22/10/2014 10 1.00
21/10/2014 40 4.00
23/10/2014 50 5.00
Summarised (How it looks at the moment, after hiding the Date column):
Customer Product Qty Value
ABCD Things 10 1.00
40 4.00
50 5.00
Summarised (How I would like it to look):
Customer Product Qty Value
ABCD Things 100 10.00
I am using Cognos Report Studio 10.1.1
You should not just hide column.
You should also set same value for this column in all rows
Instead of just [Date] in this column set
if (?HideDate? = 1) then ('') else ([Date])
or, if you prefer CASE
case ?HideDate? when 1 then '' else [Date] end
replace ?HideDate? = 1 with you own condition
Alexey's answer is great if you are using the standard Cognos 'auto-group and summarize' functionality.
If you have a custom aggregate that includes the [Date] column in its definition, you might squeeze a bit of performance gain out of modifying the aggregate function itself to disregard the [Date] column when a summarized total is desired.
If your aggregate function was:
total([Value] for [Customer],[Product],[Date])
..you might change this to a CASE statement like so:
CASE ?HideDate?
WHEN 1 then total([Value] for [Customer],[Product])
ELSE total([Value] for [Customer],[Product],[Date])
END
The data items after a 'for' clause usually end up in a GROUP BY clause in the resultant SQL. Limiting the items grouped, when possible, can help performance. In this case the performance improvement would likely be slight since there will only be one distinct value in Alexey's solution, but it's something to consider.

SSRS - Is there a way to have a table split by a page break to appear on the same page?

I am creating a report using MS Report Builder 3.0. For this report, I have a stored procedure built that filters down to the specific rows needed, and then I use a row group to group on a particular field (pass_no). The table that is displayed is 2 columns and 3 rows within the row group. The basic description of what I want to accomplish is instead of the rows running onto the next page, I want the rows to continue on the same page in a new set of 2 columns. Think of it like a newspaper where the text continues in a new column rather than running down onto the next page.
For the example I'm going to use here, there are 12 rows of data returned by the SP, and 8 unique values in the pass_no column which is what my row group is grouped on. So in the report I end up with 8 groups of 3 rows. I'm aiming to have the table display 6 pass_no values (so 6 groups of 3 rows) before, for lack of better terminology, starting a new table.
My first approach at this has been to create a column group and set the grouping expression to the following:
=Floor((RowNumber(Nothing) - 1) / 6)
While this works in creating a new set of 2 columns, the split for the new columns is based on the row number from the raw data returned by the SP rather than the number of rows sets created by the row group. So because there are 12 rows returned, and the 6th and 7th rows have the same pass_no value, the second set of columns duplicates that 1 set of data. Also, the top 6 rows of the second column set are blank with the second set of values appearing below the first set.
If I add an additional column group where it is also grouping by pass_no, then I don't get the duplicate values, but I do get a pair of columns for each pass_no as well (as would be expected). I've tried modifying the expression above a bit and changed Nothing to the row group name and have tried the table name, but neither of them have yielded the desired result.
I can't alter the SP to do the grouping there because there are other column values that are not identical and I pull that data into a cell value expression within the table using Join(LookupSet()).
I have also considered creating 2 tables and applying a filter to the table so the first table only displays the first 6 results and the second table displays the remaining results, but that also looks at the raw data rather than the groupings and TOP N can't be used on pass_no as it's a text value, not an integer. This would also cause problems if I need to go to 3 tables.
So long story short, is there a way to do a table break rather than a page break or to overflow columns onto the same page rather than onto a new page?
Here's the pertinent portions of the Dataset:
http://sqlfiddle.com/#!2/5082b/1
PASS_NO MASTERTRAN TRANS_NO DESCRIPTION IS_MOD
7913019000 4931019000 4931019000 General Admission Adult 0
7914019000 4932019000 4932019000 Sea Turtle Hosp Adult 0
7914019000 4932019000 4933019000 2:00 PM SEA TURTLE HOSP 1
7916019000 4934019000 4934019000 Sea Turtle Hosp Child 0
7916019000 4934019000 4935019000 2:00 PM SEA TURTLE HOSP 1
7917019000 4934019000 4934019000 Sea Turtle Hosp Child 0
7917019000 4934019000 4935019000 2:00 PM SEA TURTLE HOSP 1
7918019000 4934019000 4934019000 Sea Turtle Hosp Child 0
7918019000 4934019000 4935019000 2:00 PM SEA TURTLE HOSP 1
7922019000 4936019000 4936019000 General Admission Child 0
7923019000 4936019000 4936019000 General Admission Child 0
7924019000 4936019000 4936019000 General Admission Child 0
I think your data presents a bit of a problem.
As you've already figured out, typically for this sort of setup you'd set up a row group with an expression like:
=(RowNumber(Nothing) - 1) Mod 6
And a column group expression like:
=Ceiling(RowNumber(Nothing) / 6)
This would create a six row tablix that would grow horizontally as required.
See this SO question for a similar example.
However, you currently have the requirement of also grouping by another column - pass_no in your case. Normally you can approximate a group-level row number with an expression like:
=RunningValue(Fields!pass_no.Value, CountDistinct, "DataSet1")
Unfortunately, when you try to add this into one of the grouping expressions like:
=Ceiling(RunningValue(Fields!pass_no.Value, CountDistinct, "DataSet1") / 6)
You get the following error:
A group expression for the tablix 'Tablix1' includes the aggregate
function RunningValue. RunningValue cannot be used in group
expressions.
Based on all this, my recommendation is to try and get a Dataset that has one row per pass_no value and base the tablix on this, with the above row/column grouping expressions, i.e. no need to group on multiple pass_no rows. So in your example it would have eight rows. You could then have a separate Dataset with all the individual rows and use a lookupset function to concatenate the description, etc.
Your other option is to try and get everything on one Dataset only, including the aggregates as required. This might not be possible, but for description at least you can leverage any of the various techniques here to get a delimited list. Once you have this list you can replace the delimiter with vbCrLf to split it back over multiple rows.
All this is a very long-winded way of saying that I don't know if your requirement is possible with your data, but if you look at having at least one Dataset with one row per pass_no you should be able to make it work.