SSRS group by ranges - tsql

I have a field in a dataset that contains a formula (using IFF and datediff) that determines how long a record was open (there is a CreatedDate and a ClosedDate). I am then grouping by that field.
However, I want to be able to group that data into ranges. So right now, the header is Days Open and the three rows say 7, 5, and 1 (days). But the rows will grow as more data comes in, as Days Open could be any number between 1 and 50. That means I could end up with 50 rows.
I would like there to be a set number of rows, each within a range. So one row will have "1-5 days open" and then count how many rows are >=5, another row with "6-10 days open", and count those. Etc.
How can I group a calculated field by different ranges? The only way I can think of is to write a whole lot of SQL.

So if I've understood correctly, you have added a field to a Dataset that uses an expression to calculate the "Days Open" value based on two other fields (CreatedDate and ClosedDate).
You already know that the value of Days Open can be 1 - 50 and you know what ranges you want, so you could add another calculated field to the Dataset that is based on the same calculation as Days Open, e.g.
Assuming your existing Days Open calculation is
=DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value)
then you could add
=Switch(
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 0 And
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=5,
"1-5 Days",
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 5 And
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=10,
"6-10 Days",
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 11 And
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=15,
"11-15 Days",
...
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 45 And
DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=50,
"45-50 Days"
)
Although this could be vastly simplified if you can modify the actual SQL query for the Dataset to perform the Datediff function, e.g. in T-SQL for SQL Server you could add
SELECT
...
DATEDIFF(dd, CreatedDate, ClosedDate) AS DaysOpen
...
FROM
...
which would give you a single Dataset Query field that you could use in the above expression e.g.
=Switch(
Fields!DaysOpen.Value) > 0 And
Fields!DaysOpen.Value) <=5,
"1-5 Days",
Whichever way you calculate it, you can then use this Dataset field containing the range values in a simple Grouping expression in your tablix.

Related

Formula to extract unique records in the last 2 weeks

I have the following coding below, however I am wanting the report to sum the number of unique absences for the previous 2 weeks. Is there something I can add in the SUM formula that says extract the information for the last 2 weeks, as currently I'm having to do this on a daily basis.
if (Not IsNull(DistinctCount ({COVID_19_Absence_Report.Person Number}, {COVID_19_Absence_Report.Job Group}))
and DistinctCount ({COVID_19_Absence_Report.Person Number}, {COVID_19_Absence_Report.Job Group}) > 0)
then
100 * Sum ({COVID_19_Absence_Report.COVID-19 Assessment Done}, {COVID_19_Absence_Report.Job Group}) /
DistinctCount ({COVID_19_Absence_Report.Person Number}, {COVID_19_Absence_Report.Job Group})
else
0
Thanks in advance for who ever looks into this for me.

Unable to display view using two Parameters in Tableau

I have a "Select Year" Parameter for Year.
If I select from "Select Year", then I want to display results for Selected Year and Previous Year (both) in a CrossTab View on the same Sheet.
Please refer to attached to know more about my query.
*Other way (unwanted): *
I tried to create two Calculated Fields (Dimensions Filters for selecting the Year), then created two different sheets - "Selected Period Value" and "previous Period Value", but I want just one sheet to display it together.
//Selected Year
IF YEAR([Year]) = INT([Year Parameter]) THEN
INT([Year Parameter])
END
//Previous Year
IF YEAR([Year]) = INT([Year Parameter])-1 THEN
INT([Year Parameter])-1
END
Please assist.
Just assuming 46.34% and 46.85% as Profit measure.
Create two calculated fields, one for the year selected from parameter and the other for prior year.
Profit_Current calculated field:
IF YEAR([Year]) = INT([Year Parameter]) THEN Profit
ELSE 0 END
Profit_Previous calculated field:
IF YEAR([Year]) = INT([Year Parameter])-1 THEN Profit
ELSE 0 END
Use these two calculated fields in cross tab
Let us know if this works!

SSRS Count Records expression with date less than group by date

I want to count records that are not marked as completed with a Received date less than the Received date of the row (group by Received date "Details") This will be the Start of Day column showing how many records are in queue.
I have a tablix in VS 2017 SSDT.
Tablix is grouped by Received Date
COLUMNS
Received Date (group by Details) another column same field (Textbox5)
Start of Day
New Tasks
Completed
I'm having an issue with the code logic for Start of Day column field.
I want to count records that are not marked as completed with a Received date less than the Received date of the row (group by Received date "Details")
This code works for New Tasks column.
=COUNT(IIF(Fields!Received.Value < Fields!Received.Value AND
Fields!Completed.Value = "NO", 1,0),"Details")
When I attempt the Start of Day expression I get errors. Textbox5 is the same dataset field used in group by field (Received). I added it to test different approach.
=COUNT(IIF(Fields!Received.Value < ReportItems!Textbox5.Value AND
Fields!Completed.Value = "NO", 1,0),"Details")
Error: rsAggregateReportItemlnBody aggregate functions can be used
only on report items contained in page headers and footers.
Sample data and expected output for Start of Day Column: it should count records in the group by row if they were in queue prior to start of day (yesterday).
You can use running value to calculate the total. To get the prior date total you subtract the group total value
Start Of Day
= RunningValue( 1, SUM, "Tablix1")- SUM( 1)
New Task
= SUM(1) or COUNT(1)
Complete
= RunningValue( Iif(Fields!CompYN.Value = "YES",1,0), SUM, "Tablix1")- SUM( Iif(Fields!CompYN.Value = "YES",1,0))
New Tasks column: =COUNT(IIF(Fields!Received.Value = Fields!Received.Value AND Fields!Completed.Value = "NO", 1,0),"Details")
Start of day column: =COUNT(IIF(Fields!Received.Value < Fields!Received.Value AND Fields!Completed.Value = "NO", 1,0),"Details")

How to Create Calculated field for last 7 days sales

I am trying to create one calculated field for last 7 days and want to display same info for like total order value,Total orders, Total Ordering retailers
My Formula for Calculated Field
IF ATTR([CreatedDate])>=TOTAL(MAX([CreatedDate]))-6 THEN
SUM([OrderAmount])
END
Create a new calculated field called [Day Index] that indexes your date field by day:
DATEDIFF('day', [Date], today())
Then a new field per measure to get the value for the last 7 days:
IF [Day Index] <= 6 THEN [Total Orders] END
The 6 assures you data source includes the current day also, if it doesn't then you may want to adjust this to 7.
create calculation :
[order Date] >= (TODAY()-7)
drag this calculation into filter and select TRUE

Qlikview Automate past 12 months selection

I have a combo chart which shows the average days it took for a person to pay their bill.
The Dimension of the Chart is = [Pay Month Year last 12 months]
There are no Dimension limits
There is 1 expression which is called Average and its definition is:
avg({< InvoicefromSqlType = {'Invoices'},[Is Invoice Paid] = {'Y'},[Is Positive Amount] = {'Y'},[Is Paid last 12 months] = {'Y'},DueGroups=,[Pay Month Year last 12 months]=>}[Days to Pay])`
Its is sorted by expression, which is [Pay Month Year last 12 months]
Now the above fields are built up like this:
[Pay Month Year last 12 months]
If([Pay Date] >= '$(vPeriodS12)',[Pay Month year]) as [Pay Month Year last 12 months],
PayLoadOrder:
Load * Inline [Pay Month Year last 12 months
May-2014
Jun-2014
Jul-2014
Aug-2014
Sep-2014
Oct-2014
Nov-2014
Dec-2014
Jan-2015
Feb-2015
Mar-2015
Apr-2015
May-2015
];
Now what is happening is every month when it reaches the end, the next month is needing to be manually added and the first month removed (e.g. in the above I would delete the line May-2014 and at the end add the line Jun-2015)
Also if there are months defined which there is no data for yet i.e. you have Jun-2015 hard coded and the current month this May-2015 then Jun-2015 will show data from 2014 and the order of the months will get mixed up.
What I want to do is completely remove the need to hard code the months above and have this done its self.
If there are any more information you need let me know
Instead of using a "sort order" table that you may have to manually update, it may be worth doing the following:
Create a new field derived from [Pay Date] that returns a month and year that you can sort. For example:
dual(date(makedate(year([Pay Date]),num(month([Pay Date]))),'MMM-yyyy'),
year([Pay Date]) * 100 + num(month([Pay Date]))) as PayMonthYear
Here, the dual function allows you to associate a different representation of a field value to its underlying value. For example, here we set the underlying data to be the year of [Pay Date] added to the month, but state that it should be displayed as MMM-yyyy. For example, internally, QV still sees the value 201502, but displays it as Feb-2015. This means you can sort it correctly based on its underlying value.
Using dual is a big topic, please consult the built-in help for QV for more information.
Change your chart dimension from [Pay Month Year last 12 months] to use PayMonthYear and set the sort to ascending. This would then mean that your months would be sorted correctly, even if a new month was added.
Remove table PayLoadOrder from your script.
Alternative Method
An alternative would be to use a calendar table which joins on your Pay Date field. This would achieve the same thing, however, you could also integrate your "year to date" indicator into the calendar as well and remove it from your main table. An example I quickly threw together is shown below:
MinMax:
LOAD
Max([Pay Date]) AS MaxDate,
Min([Pay Date]) AS MinDate
RESIDENT MyData;
LET varMinDate = Num(Peek('MinDate',0,'MinMax')); // 0 is first record
LET varMaxDate = Num(Peek('MaxDate',-1,'MinMax')); // -1 is last record
LET varToday = Num(Today());
MasterCalendar:
LOAD
monthstart([Pay Date]) >= monthstart(AddMonths(Today(),-12)) as PaidInLast12MonthsFlag,
dual(date(makedate(year([Pay Date]),num(month([Pay Date]))),'MMM-yyyy'),year([Pay Date]) * 100 + num(month([Pay Date]))) as PayMonthYear
[Pay Date];
LOAD
date($(varMinDate) + RecNo() - 1,'DD/MM/YYYY') as [Pay Date]
AUTOGENERATE num($(varMaxDate)) - num($(varMinDate)) + 1;
DROP TABLE MinMax;
So, in the above, the field PaidInLast12MonthsFlag is equal to -1 if the value of the [Pay Date] field occurs in the last 12 months, 0 otherwise. You could use this in your set analysis expression as a filter. Furthermore, you can use PayMonthYear as your chart dimension.