Spotfire Custom Expression summarizing over dates and unique identifiers - expression

I am trying to make a bar chart in Spotfire where the y-axis is displayed via a custom expression with the x-axis being a year.
I am using a multiplier for each year and then summarizing. Basically, I have a large dataset and I am using a published model that says each year, only a certain percentage of these data are actually important. So I am trying to make a bar chart with just that percentage of "important" data. When my data contains only unique records, this expression works:
Sum(Case
when (Year([Date*])=2011) and ([Source]="A") then 0.43
when (Year([Date*])=2012) and ([Source]="A") then 0.44
when (Year([Date*])=2013) and ([Source]="A") then 0.45
when (Year([Date*])=2014) and ([Source]="A") then 0.47
when (Year([Date*])=2015) and ([Source]="A") then 0.48
else 0
end)
However, my data does contain some duplicate records. For instance, my data looks something like this:
ID Source Date* TA
1 A 1/2/2013 C
1 A 1/2/2013 D
2 A 3/5/2015 E
3 A 11/15/2012 B
3 A 11/15/2012 C
4 B 2/15/2014 B
If I used the above working code, I would end up double counting for record IDs 1 and 3. Note I also have data with a Source <> A, but I do not want to include those in the chart.
I have tried something like this, but it is just completely wrong:
Sum(Case
when (Year([Date*])=2011) and ([Source]="A") then UniqueCount([ID]) * 0.43
when (Year([Date*])=2012) and ([Source]="A") then UniqueCount([ID]) * 0.44
when (Year([Date*])=2013) and ([Source]="A") then UniqueCount([ID]) * 0.45
when (Year([Date*])=2014) and ([Source]="A") then UniqueCount([ID]) * 0.47
when (Year([Date*])=2015) and ([Source]="A") then UniqueCount([ID]) * 0.48
else 0
end)
How do I sum over different percentages by year and unique identifier? Do I need to consider some kind of over statement after my then? I'm stuck and would appreciate any guidance.

I believe that this should work:
UniqueCount([ID]) *
Avg(Case
when (Year([Date*])=2011) and ([Source]="A") then 0.43
when (Year([Date*])=2012) and ([Source]="A") then 0.44
when (Year([Date*])=2013) and ([Source]="A") then 0.45
when (Year([Date*])=2014) and ([Source]="A") then 0.47
when (Year([Date*])=2015) and ([Source]="A") then 0.48
else 0
end)
Essentially you are using the unique count to sum the unique IDs which are split into years based on your x-axis. Then you are multiplying this count by the number you specified in your case statement. The Avg is needed as the value needs to be aggregated for the visualization to work properly.

Try the following:
(1) Add a calculated column (Data Cleaning Value) that assigns the values from your case statement:
Case
when (Year([Date*])=2011) and ([Source]="A") then 0.43
when (Year([Date*])=2012) and ([Source]="A") then 0.44
when (Year([Date*])=2013) and ([Source]="A") then 0.45
when (Year([Date*])=2014) and ([Source]="A") then 0.47
when (Year([Date*])=2015) and ([Source]="A") then 0.48
else 0
(2) Add a calculated column (RowID) for RowID:
RowID()
(3) Add a calculated column (Cleaned Data) which flags the first row of each duplicate value record set and returns the value from the case statement for that record:
If([RowId]=Min([RowId]) over ([ID]),[Data Cleaning Value],null)
(4) Create your bar chart with date on the x-axis and sum([Cleaned Data]) on the y-axis.
When I did this with the data you provided the resultant plot was the same as that driven by the custom expression in my other answer. Without additional data though I wasn't able to test if it would on a larger data set.

Related

SSRS Report Builder throws #Error when handling NULL in numeric column

I'm trying to calculate an unweighted average given a data for averages and counts, and I can't figure out why my Total calculation is throwing #Error.
Preliminary aggregation for this report (aggregation by color) is done within the dataset's query, but drilldown functionality has been requested for this report, and as far as I know that requires totaling to be done on the Report Builder side. That requires calculating the total aggregate from a list of subtotal aggregates.
=Sum(Fields!weightedAveragePercent.Value * Fields!weight.Value) / Sum(Fields!weight.Value) gets me what I need for the total row, which is the aggregate weighted average percent for the category. In light of that, I'd expect that =Sum(Fields!averagePercent.Value * Fields!count.Value) / Sum(Fields!count.Value) would behave similarly. However, when the averagePercent field is blank (representing a NULL value passed over from the SQL query underlying the report) the calculation for the total averagePercent returns #Error.
Color
count
weight
weightedAveragePercent
averagePercent
Red
14
$10.10
3.702%
2.500%
Blue
25
$5.38
5.016%
7.181%
Brown
0
$0.00
Total
39
$15.48
4.544%
#Error
Things I've checked:
=Sum(Fields!averagePercent.Value) and =Sum(Fields!count.Value) both return the expected values. =Sum(Fields!averagePercent.Value * Fields!count.Value) returns #Error.
=Sum(Iif(IsNumeric(Fields!averagePercent.Value), 1, 0)) returns the expected value (2, in this case) but =Sum(Iif(IsNumeric(Fields!averagePercent.Value), Fields!averagePercent.Value, 0)) returns #Error. This is not resolved by changing 0 to 0.000, or by wrapping IsNumeric() in Not().
Pushing =Fields!averagePercent.Value * Fields!count.Value into a calculated column still throws #Error.
Data Types:
count: int
weight: float
weightedAveragePercent: float
averagePercent: float

Creating a column with % change from previous year in Tableau

I am attempting to make a calculated filed that shows the percent change in "Renewal at Par" over the past year. I have calendar year (cy) as a column in my dataset.
"At Par" is a calculated field where ra means renewal available.
(sum([renewal_at_par])/ sum([ra]))
Currently my formula is
ZN([AT PAR])-LOOKUP(ZN([AT PAR]),-1)
However, this generates value in the YoY% change column that are the percent difference in the previous row. This is not the intended output I want. I would like to output the percent change for each geo from the previous year (21 vs. 20).
Geo
Renewal at Par
YoY % Change
APAC
92.89%
LATAM
90.27%
-2.6%
EMEA
89.53%
0.7%
NA
89.86%
0.3%
I have also tried the below but it produces an output of 0 for all Geos
if [opportunity_close_date_fiscal_year_number] = 2020 then syb else 0
end
and then did the same for 2021 and then performed a yoy calculation but was unsuccessful.
ex. YOY = ((calculated field 1 – calculated field 2) / calculated field 2) * 100

DAX - Calculate grand total

I have a data file as below
TOTALUNITS TOTALRECEIPTS
55529 12806357
45472 6813097
19605 4217647
19202 2105760
17114 2568849
16053 1577361
1506 657607
I need to write a measure to calculate the average of TOTALUNITS. The measure should give me a result as (TOTALUNITS/SUM(TOTALUNITS))*100 as below
TOTALUNITS TOTALRECEIPTS %TOTALUNITS
55529 12806357 31.8%
45472 6813097 26.1%
19605 4217647 11.2%
19202 2105760 11.0%
17114 2568849 9.8%
16053 1577361 9.2%
1506 657607 0.9%
Someone help me with this.
If you want this as a measure that will sum to 100% of what you have filtered, then try this:
%TotalUnits = DIVIDE(
SUM(Receipts[TotalUnits]),
CALCULATE(
SUM(Receipts[TotalUnits]),
ALLSELECTED(Receipts)))
If you try this as a calculated column though, you'll get 100% every time. If you want to do this as a calculated column, then you'll need to remove the SUM from the first argument of DIVIDE:
%TotalUnits = DIVIDE(
Receipts[TotalUnits],
CALCULATE(
SUM(Receipts[TotalUnits]),
ALL(Receipts)))
Calculated columns are not responsive to slicers or filtering on the report page. They are computed once when the data is loaded or refreshed, but not dynamically interactive.
Please try this, and format as Percentage:
%TotalUnits =
DIVIDE(
SUM(Receipts[TOTALUNITS]),
CALCULATE(
SUM(Receipts[TOTALUNITS]),
ALL(Receipts)
))
Edit: Receipt is the table name

SSRS Report expression setup

I have a dataset with 4 fields (Date,SeccurityName, FiledName, Value). In my dataset query I filter the data to bring only records with a specific value in the filed name(Last Price). Following is a sample of my dataset
Date SecurityName FiledName Value
5/5/2016 A LastPrice 20.01
5/6/2016 A LastPrice 19.8
5/7/2016 A LastPrice 19.9
5/5/2016 B LastPrice 43.1
5/6/2016 B LastPrice 43.5
5/7/2016 B LastPrice 43.7
In this dataset I have data for each security for each business day for the last 5 years.
In my report I need to show in a table The security name , the last value, the value from a month ago, the value from a year ago and the value from three years ago
Security name LastPrice 1M 1Year 3Years
A 20.1 18.8 19.01 16.05
I would appreciate if someone can give me the best way to build this format.
I would group your table on the Security Name. This would aggregate all records with the same security name on the same line.
Then for each date/value column, create an IIF statement to filter for the date that you want:
=MAX(IIF(Fields!Date.Value = Parameters!LastDate.Value, Fields!Value.Value, NOTHING))
The Last Month (and other dates) would be similar:
=MAX(IIF(Fields!Date.Value = DATEADD("M", -1, Parameters!LastDate.Value), Fields!Value.Value, NOTHING))
The MAX is used to aggregate the NOTHINGS with the Values.
I think it would be best to have the Last Date as a parameter (with a default of yesterday?) to make it easier to create the expressions and you would also have the ability to look at past dates if you ever have the need.

How to reference a specific Group Total of one dataset in the expression in different dataset

I have a report with multiple datasets. In one of them I need to reference the group total from another dataset. It looks like this:
Tablix1:
Region1 Total Age1 Age2
a 7 5 2
b 12 6 6
c 20 12 8
Total 39 23 16
Tablix2:
Region2 Value %
a 4 57.14%
b 6 50.00%
c 5 25.00%
The values in the "%" column of Tablix2 come from formula: %a = Tablix2 Value a / Tablix1 Total a.
My current expression in % column of Tablix2 looks like:
=CountDistinct(Fields!ID.Value, "Region2")/CountDistinct(Fields!CONSTITUENT_ID.Value, "Tablix1")
but what I get is the percentage calculated of the Total row of Tablix1 and not each Region of Tablix1.
The Lookup function would work for this. It's similar to a vlookup in Excel. It would look something like this:
=Lookup(Fields!Region1.Value, Fields!Region2.Value, Fields!ID.Value, "Region2")
This would pull the corresponding value from Region 2 into Tablix 1. You can just switch it around if you want it in the other table.

Resources