SQL Dsum based on criteria - ms-access

I'm trying to sum all amounts where column 'weight'=2 and do so according to months and format it in dollar currency. Here is what I have so far:
SELECT
Format([Final Action Date],"yyyy-mm") AS [Month],
DSum("[Amount]","C02: Underwriting Audit Case Detail Report Record Selection"," [Weight] = '2'") AS FA_Critical,
Sum(IIf([Weight]="2",1,0)) AS Critical_Count
FROM [C02: Underwriting Audit Case Detail Report Record Selection]
WHERE ((([C02: Underwriting Audit Case Detail Report Record Selection].[Case Type]) Not In ("**Target IUP")))
GROUP BY Format([Final Action Date],"yyyy-mm");
Here is the result that I have so far:
Month FA_Critical Critical_Count
2015-01 2035480 2
2015-02 2035480 2
2015-03 2035480 0
2015-04 2035480 1
Here is what I want:
Month FA_Critical Critical_Count
2015-01 $1,350,000 2
2015-02 $510,480 2
2015-03 $0 0
2015-04 $175,000 1
Please help.
Thanks.

Your problem is in the criteria for your DSum, it's looking at [Weight]='2' across the whole recordset, which is why you are getting the same value for all 4 records. The DSUM is not partitioned by the GROUP BY the same way that the SUM is.
Use a SUM instead of the DSUM, and instead of using 1,0 for true and false, use the value of your amount column:
Sum(IIf([Weight]="2",[Amount],0)) AS FA_Critical

Related

Aggregate values and display in chart

I am struggling with this problem and I can't figure out how to solve it.
This is my dataset detail data:
Order Item StartDay EndDay EndYear StartEndDiff
1 1 01.01.2018 02.01.2018 2018 1
1 2 03.01.2018 05.01.2018 2018 2
2 1 05.01.2019 06.01.2019 2019 1
2 2 06.01.2019 06.01.2019 2019 0
2 3 08.01.2019 10.01.2019 2019 1
3 1 07.01.2019 08.01.2019 2019 1
I now group them by Order (this works fine in a tablix). Group name is OrderGroup:
Order MinStartDay MaxEndDay EndYear MinStartMaxEndDiff
1 01.01.2018 05.01.2018 2018 4
2 05.01.2019 10.01.2019 2019 5
3 07.01.2019 08.01.2019 2019 1
For MinStartMaxEndDiff I got the following expression:
=DateDiff("d", Min(Fields!StartDay.Value, "OrderGroup"), Max(Fields!EndDay.Value, "OrderGroup"))
Now I added another group in the tablix and displayed the Avg(MinStartMaxEndDiff) per EndYear. This still works fine in the tablix with all the groupings. The result:
Avg(MinStartMaxEnd) Year
4 2018
3 2019
But I can't figure out how to display only the data from the result, based on the calculations, in a chart. I normally do these calculations on the SQL side, but this time I can't do them SQL side.
The problem is when I try to add the OrderGroup in the chart it always shows me the details. But I need this group to get the right values. Hide or set the category label to Nothing didn't work. Also setting the DataElementOutputto NoOutput didn't help (real look):
I would just need on the x-axis the Years and as values the Avg(MinStartMaxEnd). This would be my desired output based on the sample data from above:
EDIT AFTER A SOLUTION WAS FOUND:
What can I do if a row data looks like this:
Order Item StartDay EndDay EndYear StartEndDiff
1 1 29.12.2018 02.01.2019 2019 4
1 2 28.12.2018 30.12.2018 2018 2
This one will get divided into two separate datarows in the tablix, because of the EndYear grouping. The expected result should be look like this:
Order MinStartDay MaxEndDay EndYear MinStartMaxEndDiff
1 28.12.2018 02.01.2019 2019 5
But a grouping of the year with the expression
=Max(CDate(Fields!EndYear.Value), "OrderGroup")
is not possible.
You can use custom code inside your tablix to calculate the average per year, and call the calculated values in the chart.
Add the following custom code to your report
Public Dim YearAvg As New System.Collections.Generic.Dictionary(Of String , Decimal )
Public Function SetYearAverage( ByVal s As String, ByVal d AS Decimal) As Decimal
YearAvg.Add(s,d)
Return d
End Function
In the textbox you calculate the year average change the expression to the following (make the appropriate changes to field names)
= Code.SetYearAverage(Cstr(Fields!EndYear.Value), Avg ( DateDiff("d", Min(Fields!StartDay.Value,"OrderId"), Max(Fields!EndDay.Value,"OrderId"))))
Then set your chart expression to
= Code.YearAvg( Cstr( Fields!EndYear.Value))
This is not an answer as I didn't get the results you were expecting, work got in the way! However, it's fairly close so I thought I would post it in case it was any help.
A pity doing this on the server was not an option as it would be really easy.
Anyway, I replicated your dataset (with a couple of field name changes to avoid keyword conflicts) and set the chart up as follows..
Category groups - None
Series Groups 2. Parent group is on 'EndYear' called 'ChartYearGroup', child group is on OrderID called 'ChartOrderGroup'
Values - Here I set the Category Field to [EndYear] and the expression was
=AVG(DateDiff(
"d"
, Min(Fields!StartDay.Value, "ChartOrderGroup")
, Max(Fields!EndDay.Value, "ChartOrderGroup")
)
, "ChartYearGroup"
)
This does give the correct numbers but unfortunately shows per order rather than per year.
I'm not sure if this will be possible to resolve. Somehow I guess you'll have to hide the order group or combine the two groups into one, I just ran out of time to test.
Here's the end result.

SSRS Grouping merge details

Here is my table
EmployeeNr IDNumber Period MonthlyIncome IncentiveAmount
0000002 8912135105086 2017-04-30 3975.00 1000.00
0000002 8912135105086 2017-05-31 4658.38 670.31
0000002 8912135105086 2017-06-30 5292.19 353.90
0000003 4912015215108 2017-05-31 4274.38 862.81
0000003 4912015215108 2017-06-30 5503.75 248.13
Here is how i am able to display it with grouping:
So my question is can i display it with grouping so that the different values for each employee is only in one row?
I know how to get to the result with creating a table on my query select with all the months and looping trough to ad the values but I would like to avoid that if possible
EDIT
Here is how i want it to display:
The test come from
=IIf(Month(Fields!Period.Value) = ((Month(First(Fields!StartDate.Value,
"PayrollTaxYear")) + 0 - 1) Mod 12) + 1, Fields!MonthlyIncome.Value, "test")
I use this statement to display the data under the month it is in according to the payroll year start and end, That is also why it start on this example from march.
Edit 2
First One
Second One
You just need to use column grouping in your report, which has the same effect of using a pivot:
The expression for the month name headings is just =MonthName(Month(Fields!Period.Value))
Which outputs:

SSRS 2012 MATRIX TOTAL DYNAMIC COLUMNS BY COLUMN HEADING

Simple Q:
In SSRS 2012: require a matrix with column totals derived from the dynamic column headings;
e.g.
Period 1 2 3 Total Total
Type Act Bud Act Bud Act Bud Act Bud
Total 10 9 10 9 10 9 30 27
is this possible in SSRS?
Can easily get the total of the sum of columns -(57) but not split by type.
Thank You.
data is grouped ;
Business Unit - Row Group
Account Type - Row Group
Month Period - Column Group
Amount_Type (Act, Bud) - Column Group
The Aggregate is Amount
Tks
Without knowing the structure of your dataset I think you can add two columns outside the columns groups at the right side and use these expression to calculate the respective total:
For Actual total:
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0))
For Budget total:
=SUM(IIF(Fields!Type.Value = "Bud",Fields!Amount.Value,0))
UPDATE: Try setting the scope of the SUM function.
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0),"DataSetName")
Replace DataSetName by the actual name of your dataset.
Let me know if this helps.

Reporting Services How to get the Count() value from IIF

I have this table with a column named Open_Time which contain a datetime value. I would like to have another column named Total Ticket In June with the total count of ticket in June, so I've inserted the expression like below:
=Count((IIF(DatePart("m",Fields!Open_Time.Value,0,0) = "6",1,0)))
but there seems to be an error. To make myself clear, the table should look like this:
Assigned Name Ticket ID Open_Time Total Ticket in June
Ivan 001 3/28/2014 2
002 6/24/2014
003 6/11/2014
I would like to get value "2", which is the total number of ticket in June. Any idea? :)
You need to switch to a Sum aggregate instead of Count (based on your IIF, Count will count every row instead of giving you a total number of occurences):
=Sum(IIF(DatePart("m",Fields!Open_Time.Value,0,0) = 6, 1, 0))
From your table it's hard to tell your row groupings, but keep in mind that you'll only get the sum you expect if that expression is on a totals row (i.e. if you use that expression on the detail row it will simply list 1 or 0 for each date).
Try this:
=Count((IIF(DatePart(dateinterval.Month,Fields!Open_Time.Value,0,0) = 6,1,0)))

Reporting services put data from two DataSet rows in one tablix cell

I need to create report with table which looks like this
Country 1 Country 2 Country 3 Total
Category 1 1(2) 2(1) 5(6) 8(9)
Category 2 2(3) 2(1) 4(0) 8(4)
Category 3 3(2) 2(1) 3(1) 8(4)
Total 6(7) 6(3) 12(7) 24(17)
Report contains data about TFS WI's and has information about current week's WI count and last weeks WI count (in brackets)
Data set on which this report is based on MDX query against TFS warehousw cube and has such structure:
Category Country Week Count
1 1 this 1
1 2 this 2
1 3 this 5
1 1 last 2
1 2 last 1
1 3 last 6
Trouble is, I cann't find a way how to concatenate data about current and last weeks incident count in one cell. I have toyed around with idea to do it in MDX, but with my limited MDX skills I can't see how it could be done.
Rowgroup on Category.
Columngroup on Country.
Inside the cell you should be able to have 2 placeholders with the second one in brackets. The first expression should be:
=Sum(iif(Fields!Week.Value = "this", Fields!Count.Value, 0))
The second one should be:
=Sum(iif(Fields!Week.Value = "last", Fields!Count.Value, 0))
In MDX you could create two calcs - this and last week (of course you'll need to change this to work with your cube):
WITH
MEMBER [Measures].[thisWeekCount] AS
([Date].[Week].[this], [Measures].[Count])
MEMBER [Measures].[lastWeekCount] AS
([Date].[Week].[last], [Measures].[Count])
SELECT
{
[Measures].[thisWeekCount],
[Measures].[lastWeekCount]
} ON 0,
{
...
} ON 1
FROM [Your Cube]
Then, you can use them within placeholders as jimconstable explained, but without the iif functions.
Thank you all for your answers!
I found out that main problem (reporting services allows only one measure on columns) can be solved by using Analysis server OLE DB provider. There are some drawbacks, like that parameters are not supported, but I can live with this.