Sum of one field minus sum of another SSRS Expression - sql-server

I have a SSRS report I'm working on. What I would like to do is get the value of one field from its own dataset and subtract the value of another field from a different dataset. I can do this; however, the values are grouped so rather than giving me an individual value it gives me: (sum of all completed) - (sum of all completed the previous year).
Here is my expression I am using for the column "Compared to last year"
=SUM(Fields!Completed.Value, "MTDSales") - SUM(Fields!Completed.Value, "MTDminus1")
"MTDSales" and "MTDMinus1" are 2 seperate datasets. MTDSales Dataset is the current months sales outcomes grouped by company MTDMinus1 dataset is last years figure for this current month as i am comparing the 2 months separately.

I had to do this in a report where I was pulling current data from one database and older data from a data warehouse and combining. You will need to do a few things:
1. Establish a match field
This can be as simple as a single column. If you need to match on multiple fields you will need to add a calculated field to each dataset that you can match on. Assuming you need to match on company and financial year and each dataset returns one year of data, this might look something like match_id (assuming numeric values - otherwise you might need to use | or something as a separator):
`="A" & Fields!fin_year.Value & "B" & Fields!cust_id.Value`
2. Retrieve the data to the source field.
In your tablix add a column as you have to hold the looked up value:
=Lookup(Fields!matchId.Value, Fields!matchId.Value, Fields!Completed.Value, "MTDminus1")
3. Use the data
Now you can aggregate the data or do whatever further calculations you wish as if the field was part of your original dataset.

Related

Google Data Studio convert metric to dimension not working

I have imported my GA4 data into Google Data Studio and am trying to see how many giftcards have been sold by their value.
The item revenue metric in GA4 is equal to the giftcard value (i.e. revenue = $200 therefore $200 giftcard was sold).
I want to breakdown sales by giftcard value like so:
Giftcard (revenue)
Count
$200
4
$250
3
$300
6
To do this, I need to set a copy of item revenue as a dimension rather than a metric.
In Google Data Studio, I can create a calculated field with the following formula that should convert the item revenue into text:
CAST(Item Revenue AS TEXT)
The problem I'm having is that while the formula sets the field type as text, it is still regarded by GDS as a metric and can't be used as a dimension.
Even when I try to add text, GDS still recognises the field as a number:
CONCAT(CAST(Item Revenue AS TEXT), " giftcard")
To use a metric as a dimension you can make a combination of data. When defining the graphic element (table, for example) and the respective data source, just create a data combination, but do not combine the data with any other source and just define the combination with the initial data itself. So you will have the same data structure only through a combined structure.
When making a combination of data, data studio recognizes all calculated fields (metrics) as dimensions. Thus, it is possible to make the conversion.

Google Data Studio date aggregation - average number of daily users over time

This should be simple so I think I am missing it. I have a simple line chart that shows Users per day over 28 days (X axis is date, Y axis is number of users). I am using hard-coded 28 days here just to get it to work.
I want to add a scorecard for average daily users over the 28 day time frame. I tried to use a calculated field AVG(Users) but this shows an error for re-aggregating an aggregated value. Then I tried Users/28, but the result oddly is the value of Users for today. The division seems to be completely ignored.
What is the best way to show average number of daily users over a time frame? Average daily users over 10 days, 20 day, etc.
Try to create a new metric that counts the dates eg
Count of Date = COUNT(Date) or
Count of Date = COUNT_DISTINCT(Date) in case you have duplicated dates
Then create another metric for average users
Users AVG = (Users / Count of Date)
The average depends on the timeframe you have selected. If you are selecting the last 28 days the average is for those 28 days (dates), if you filter 20 days the average is for those 20 days etc.
Hope that helps.
I have been able to do this in an extremely crude and ugly manner using Google Sheets as a means to do the calculation and serve as a data source for Data studio.
This may be useful for other people trying to do the same thing. This assumes you know how to work with GA data in Sheets and are starting with a Report Configuration. There must be a better way.
Example for Average Number of Daily Users over the last 7 days:
Edit the Report Configuration fields:
Report Name: create one report per day, in this case 7 reports. Name them (for example) Users-1 through Users-7. These are your Row 2 values. You'll have 7 columns, with the first report name in column B.
Start Date and End Date: use TODAY()-X where X is the number of days previous to define the start and end dates for each report. Each report will contain the user count for one day. Report Users-1 will use TODAY()-1 for start and end, etc.
Metrics: enter the metrics e.g. ga:users and ga:new users
Create the reports
Use 'Run reports' to have the result sheets created and populated.
Create a sheet for an interim data set you will use as the basis for the average calculation. The first column is date, the remaining columns are for the metrics, in this case Users and New Users.
Populate the interim data set with the dates and values. You will reference the Report Configuration to get the dates, and you will pull the metrics from each of the individual reports. At this stage you have a sheet with date in first columns and values in subsequent columns with a row for each day's values. Be sure to use a header.
Finally, create a sheet that averages the values in the interim data set. This sheet will have a column for each metric, with one value per column. The one value is calculated from the series in the interim data set, for example =AVG(interim_sheet_reference:range) or any other calculation you'd like to do.
At last, you can use Data Studio to connect to this data source and use the values. For counts of users such as this example, you would use Sum as the aggregation field type when you are creating the data source.
It's super ugly but it works.

How can I split data from a second dataset in SSRS

I have two tables in SSRS. One holds the amount of insurance claims in a given month and one holds the amount of insurance complaints in a given month
Each table is calculated by either =COUNT(Fields!Claims.Value) for claims and =COUNT(Fields!Complaints.Value) that is simple enough and is split over the current 10 months of a year
Where it gets tricky though is that the claims table has an additional line where it calculates complaints as a ratio of claims. My current expression reads as follows:
=COUNT(Fields!Complaints.Value, "Complaints"/=COUNT(Fields!Claims.Value)
but the problem I have is that it's taking the full YTD value of the complaints and dividing by the monthly amount of claims.
ASK:
How can I get a calculation similar to above but only dividing complaints by month and claims by month - but keeping in mind that the complaints data comes from a different table
You can use LookupSet function to get the claims in the same month.
Create a tablix and use the Complaints dataset in the DataSetName property.
Add Month as Row Group.
For complaints column use:
=Count(Fields!Complaints.Value)
For claims column use:
=LookupSet(Fields!Month.Value,Fields!Month.Value,Fields!Claims.Value,"Claims").Length
For Ratio column use:
=IIF(
ReportItems!Textbox69.Value=0,0,
Count(Fields!Complaints.Value)/ReportItems!Textbox69.Value
)
Replace Textbox69 by the name of the textbox where Claims (LookupSet) expression is placed.
Note the validation for zero denominator in case there is no claims in a given month.
It should produce:
Let me know if this helps.

SSRS report with stored procedure Dataset and aggregation by month - how to handle "empty" months?

I have a SSRS report which calls a stored procedure to return a two columns dataset - one column is a Date and the other is a calculated column I created to show the month in the format MMM-YYYY.
Using this dataset I have created an aggregated bar chart report which shows the number of records per month for each month value which has at least one record in the dataset, however I would like to be able to include on the chart any months within the range of the dataset even if they do not contain any records.
So for example, where at the moment I see the following data points on my chart:
Month Count
----- -----
Oct-2015 2
Nov-2015 5
Jan-2016 3
Feb-2016 6
Dec-2015 is missing because my Dataset didn't return any records for it, so I would like to be able to plot a zero Count value on the chart for this month.
Now I know how I would normally do this in SQL, with some kind of calendar table LEFT JOINed in so that I can return the months with zero counts, but can I achieve the same in SSRS with a stored procedure as the target of the main dataset?
If your grouping is on a date/time (or a number), you can make the axis type for it Scalar.
Create your chart, with Count as the value and Month as the Category Group
Right-click the grouped axis (horizontal in my example below) and go to the Axis Properties
Change the Axis type to Scalar, and change the Interval type to Months
Still in the axis properties, change the Number Category to Date, and the Type to Jan 2000 (or use a custom format)
My first instinct was to aggregate by an actual date, converted to the first of the month, instead of your MMM-yyyy string. However, it appears that SSRS (2008 R2 in my case) recognises text in the format MMM-yyyy as a valid date, so your existing procedure shouldn't need changing.

How do I calculate the percentage of a count function?

I am trying to take the percentage of a count function so to create a MS BIDS report resembling this excel file:
Excel Close Rate Summary
The unique identifier for the opportunities is the field "opportunityid", so I am using COUNT(Fields!opportunityid.Value) to determine the number of cases in each stage. I want to write an expression that will return the percentage of cases in each stage per creation month. Which can be seen in the above excel screenshot.
This is my current MS BIDS report when i preview it
To be more specific, I want to have the percentage of "Active" and "New" opportunities in January to represent 67% and 33% respectively. 67% comes from 4/6. The 4 comes from the active opportunities out of the 6 opportunities created in January. Likewise, the 33% comes from the 2 new opportunities out of the 6 that were created in January.
There are more stage names than Active and New. Other options include New, Warm, Hot, Implementation, Active, Hibernate or Canceled. This is relevant to mention because I have tried to create an expression that counts based on the number of opportunities with a specific stage name, but have been unsuccessful.
Currently the expression I am using to calculate the percentage is:
=COUNT(Fields!new_rptstage.Value)/SUM(COUNT(Fields!opportunityid.Value),"GroupbyStageName")
Based on this expression, I am only able to get 1/1 or 100% for each of the stage names. I have tried a bunch of variations of the above expression by changing the scope, but have been unsuccessful in getting the desired results. Can someone explain how to correct this?
SAMPLE DATA:
In the sample data, I want the expression to be in the percentage column. The percentage should be the # of cases in a particular stage for the total cases that month. So looking at the above picture:
Active February 54 54/168 [have 54/168 display as a percentage]
Warm February 8 8/168
etc.
EDIT:
These are the expressions that may help show the underlying data in the chart.
The creation month expression is
=Fields!MonthCreated.Value & " " & year(Fields!createdon.Value)
The percent expression is listed above.
You don't want to use the COUNT() function. COUNT(*) returns a count of the number of rows that have a value. It doesn't return the actual value.
Since you've only showed a screen shot of your report, I don't know how your underlying data columns relate to it, but what you want to do for your Percent column expression is this:
This is psuedo code because I don't know your dataset field names:
CaseCount.Value / SUM(CaseCount.Value)
EDIT: Now that I better understand how your data relates to your report, I think the only change you need to make to your existing formula is casting it to a decimal type. It's probably rounding all fractions up to 1.
Try this for the expression in your percentage column:
=CDbl(COUNT(Fields!new_rptstage.Value))/CDbl(SUM(COUNT(Fields!opportunityid.Value),"GroupbyStageName"))

Resources