Using nested iif statements to subtract two columns of data - sql-server

Working in Visual Studio 2012 to create a SSRS Business Intelligence Services Report.
Trying to do something like below where based on the value of one column, subtract the value of another column.
=iif(Fields!ACADEMIC_YEAR.Value=Parameters!ACADEMIC_YEAR.Value, Fields!TotalProspects_Dom.Value-iif(Fields!ACADEMIC_YEAR.Value=Parameters!ACADEMIC_YEAR.Value-1,Fields!TotalProspects_Dom.Value, 0),0)
Basically my table looks like:
essentially the table for the report is two rows, the current academic year and the previous academic year and I have to compare the two. Each row is made of columns representing the data for domestic and international data for a larger group.

I think you just need to have separate IIFs to determine the year.
= SUM(IIF(Fields!ACADEMIC_YEAR.Value = Parameters!ACADEMIC_YEAR.Value, Fields!TotalProspects_Dom.Value, 0)
- SUM(IIF(Fields!ACADEMIC_YEAR.Value = Parameters!ACADEMIC_YEAR.Value - 1, Fields!TotalProspects_Dom.Value, 0)

Related

How to display data in area chart starting from the year chosen on a slicer and get all following years in Power BI

When choose a single Year on slicer I want area chart display all data from that chosen year and till the end (all years I have in my datasource).
But instead it just displays me the data for single year choosing on a slicer.
So I have this:
But I want it look like this: whatever Year I choose in slicer - chart will show all data starting from 2014 and goes till 2017.
I am simply following a PowerBI template example and it seems like it's possible to do that:
https://app.powerbi.com/view?r=eyJrIjoiMjc2NzExODItMjNhYy00ZWMxLWI2NGItYjFiNWMzYzUzMzhlIiwidCI6IjU3NGMzZTU2LTQ5MjQtNDAwNC1hZDFhLWQ4NDI3ZTdkYjI0MSIsImMiOjZ9
This is doable but it requires some tricks and extra measures.
TL;DR: The slicer you see is actually served as a value picker, not as a filter. An extra measure based on the value is created and used as visual level filter for the visual to do the trick.
If you want to follow along, you can download the .pbix file from this Microsoft edX course about Power BI.
First, create a new table based on the existing Date table, with only distinct years:
Year = DISTINCT('Date'[Year])
Then, create a slicer with the Year column from the newly created Year table (NOT the Date table).
A measure (used as flag) is created as follows:
Flag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) >= YearSelected, 1, 0)
So basically it gets the year selected from the year slicer and compare it with the year value in the date table to see if it's greater than or equal to it.
The chart is created with Year column from the Date table (NOT the Year table), and other needed measures. Flag is added to the Visual level filters and set to 1.
So the Flag value will change according to the value picked in the Year slicer, and served as the actual filter to the chart displayed.
Results:
EDIT: on more use cases
#Oleg Try to think of how you can apply the Flag concept further. For example, if you want another chart displaying data of the same year as the slicer, you can set up another flag called SameYearFlag and only change the part of value comparison to =. Add it to the chart Visual level filter and it'll show only data in the same year. Yes, by extension, that means you can have another flags like LastYearFlag, NextYearFlag, etc, as long as it makes sense to you. The use case is up to you.
LastYearFlag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(YearSelected - VALUES('Date'[Year]) = 1, 1, 0)
NextYearFlag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) - YearSelected = 1, 1, 0)
SameYearFlag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) = YearSelected, 1, 0)
Examples:
By having only one year slicer, I can have charts with data in the same year, last year, next year and all years following, by applying different flags to them.
As said, it's up to you to come up with more interesting use cases!
I would propose to consider the new numeric range slicer.
You can just set it as "Greater than or equal to". Users can select then the initial year in the range by entering the number or dragging the slicer.
You would need to enable this feature in Power Bi Desktop, Options under "Preview features".
Is well presented in the documentation https://powerbi.microsoft.com/en-us/documentation/powerbi-desktop-slicer-numeric-range/
This is how it could look like:

Sum of one field minus sum of another SSRS Expression

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.

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.

SSRS Math across matrix

I have created a matrix with the groupings below in SSRS 2008 R2 and SQL Server 2008 R2.
MembershipStatus has "New Enroll", "Term", "Active".
What I need to do is to add a row called "Other" to do the following calculation:
Last month new enroll + last month active – this month term – this month active
In the example below, it's
19,281 + 0 - 2,082 - 17,195 = 4
I've used temp tables and various combinations of APPLY to get the results I need in SQL, but due to the large number of parameter combinations, I really need to do it inside SSRS.
Should I just give up and code it in the CODE section of the report?
Any insight would be greatly appreciated.
Try using the ReportItems function. Each text box in SSRS will have unique name which can be found by selecting the respective text box and then checking the top most section in the properties window displayed on the right side.
Let's say your NEW ENROLL value for June is displayed in Textbox1, TERM FOR June in Textbox2, ACTIVE for June in Textbox3, NEW ENROLL FOR July in Textbox4, TERM for July in Textbox5, ACTIVE FOR July in Textbox6 and so on. Write the below expression for OTHER value for July as below:
=ReportItems!Textbox1.Value + ReportItems!Textbox3.Value - ReportItems!Textbox5.Value - ReportItems!Textbox6.Value
This formula will change based on the default naming by SSRS for the respective textboxes.
With the calculation provided, it only allows to do the calculation in the same month. The question is how to calculate numbers in the previous month against current month...
how could it be done?

Using Aggregate Function in SubTable in Visual Studio

I am trying to put together a Subtable on my report in Visual Studio 2008 with SSRS. The problem I am running into is using a function correctly and referencing a report item via my main report. On my report I have an expression that color codes, when a specific value is returned from the main query. In my Subtable, I want to count each instance of a specific value so for example:
Main Report
Supervisor Employee Ranking
Supervisor 1 Employee 1 Meets (Labeled Orange)
Supervisor 1 Employee 2 Outstanding (Labeled Yellow)
Supervisor 2 Employee 3 Meets
Supervisor 2 Employee 4 Meets
Subtable Outstanding Meets
Supervisor 1 1 1
Supervisor 2 0 2
I have tried using the following formulas
=count(Reportitems!Ranking.Value, "Meets", 0)
=count(Reportitems!Ranking.Value, "Orange", 0)
=count(Reportitems!Ranking.Value = "Meets")
=count(Reportitems!Ranking.Value = "Orange")
I have also changed =count to =countrows and =countdistinct
Can someone help? I am open to anything, I just want the report and Subtable to work correctly.
Once you've got a table grouped by Supervisor (and it looks like you do), you'll need to add a couple of columns with two conditional expressions:
Outstanding column:
=Sum(IIf(Fields!Ranking.Value = "Outstanding", 1, 0))
Meets column:
=Sum(IIf(Fields!Ranking.Value = "Meets", 1, 0))
These will count the occurrences of Outstanding and Meets for each Supervisor that is returned.
This assumes that Ranking is just column in your Dataset - you mention colour coding based on this, but we are only interested in the base value, not the colour.
Edit after comment
If Ranking is based off an expression, you can simply add this into the original expressions above, i.e. something like:
=Sum(IIf(<MyExpression> = "Outstanding", 1, 0))
It's a bit of a generic answer since I don't know what your specific example is, but you can certainly nest expressions in other expressions without issue.
For a more elegant solution, you should consider adding a Calculated Field to your dataset - this defines the expression once in the dataset, then you can use it like a normal field within the report itself. This way you would create a Calculated Field called Ranking in your dataset, then you can use expressions like the ones in the original answer referencing this new calculated Ranking field.

Resources