SSRS 14 - Matrix Column - Get Value Where Month = MAX(Month) - sql-server

I've a Matrix to show the values over the months. But the last column I want to show the Varianve between the current month and the previous month. I've this dataset (Months):
Servername Month Year Reference Value Previous_Value
SV1 8 2017 80 11 Null
SV1 9 2017 80 13 11
SV1 10 2017 80 18 13
SV1 11 2017 80 21 18
SV1 12 2017 80 12 21
SV1 1 2018 80 18 12
Basically, I want to build a expression that allows me to get the value from MAX(Month) and MAX(Year). I try this:
=IIF(Fields!Month.Value = max(Fields!Month.Value, "Months") and Fields!Year.Value = max(Fields!Year.Value, "Months"),Fields!Previous_Value.Value,0)
But when I run the report I'm getting 0 to all of my machines... And my final matrix are:
**Servername 8 9 10 11 12 1 Previous_Value**
SV1 11 13 18 21 12 18 12
How can I do this?
Thanks!

Your expression is saying if Month = MAX(Fields!Month.Value) which is 12 and Year = Max(Fields!Year.Value) which is 2018 then show Previous_Value.
As non of your rows match this then it won't work.
I've not tested this but try aggregating the month and year and comparing to that. This is easier to do in SQL by adding a new column
SELECT *, ([Year] *100) + [Month] as YearMonthKey FROM myTable
Now your expression can just check YearMonthKey against MAX(Fields!YearMonthKey.Value)

Related

How to identify gaps in reported year in an unbalanced panel with repeated observations for firm-year?

I have a dataset at the firm-product-year level. I want to identify which firms having gaps in reporting years between 1994-2004. Consider an example below:
clear
input id year sales product
14 1994 28.9 2
14 1994 67.9 3
14 1994 12.5 9
14 1994 451.8 34
14 1994 27.5 44
14 1994 647.6 45
14 1995 9.7 2
14 1995 33.5 3
14 1995 112.4 9
14 1995 712.2 15
14 1995 902.3 41
14 1995 67.3 45
14 1995 15.1 50
14 1996 6.5 2
14 1996 24.6 3
14 1996 1009.4 5
14 1996 77.1 9
14 1996 76.9 17
14 1996 12.4 45
14 1996 946.3 88
14 1996 15.4 92
14 1997 .7 2
14 1997 63.2 2
14 1997 91.7 3
14 1997 860.8 9
14 1997 12.4 21
14 1997 800.8 32
14 1997 33.7 45
14 1997 41 95
15 1999 .1 44
15 2000 .1 58
15 2001 .4 27
15 2001 .1 95
15 2002 .5 5
15 2002 .1 58
15 2003 .1 17
15 2004 3.5 28
15 2004 .1 39
16 2000 .8 2
16 2001 .6 2
16 2003 .2 2
16 2004 .1 2
16 2004 .1 8
16 2004 2.5 8
end
Firm 14 produced 6 products in 1994. It produced every year consecutively until 1997. Because there are no missing years in between, I keep this firm. But firm 16 reports in 2000, 2001 and then in 2003. I assume that the firm still operated in 2002 but doesn't report in the data. How to create a dummy variable for this firm?
tsfill doesn't help because I have repeated values within id-year.
In the first step, you delete the companies that do not produce any products in a year by creating a dummy variable "firm_any_production" that indicates whether a company has produced at least one product in a given year. Then the maximum of this dummy variable is calculated for each firm and the firms for which the maximum is 0 are deleted.
gen firm_any_production = sum(sales) > 0
bysort id (year): egen firm_missing_year = max(firm_any_production)
drop if firm_missing_year == 0
In step 2 you calculate whether the newly added products of a company have higher sales than the core product. This is calculated by creating a dummy variable "is_new_product", which indicates whether a product is a new product. Then the sales of these new products are calculated and compared to the sales of the core product. If the sum of the turnover of the new products is greater than the turnover of the core product, another dummy variable "greater_than_core" is created and set to 1.
bysort id year: egen core_product_sales = max(sales)
gen is_new_product = sales != core_product_sales
gen new_product_sales = sales * is_new_product
gen greater_than_core = sum(new_product_sales) > core_product_sales
Translated with www.DeepL.com/Translator (free version)
Added:
The code is creating a firm_missing_year variable that takes the value of 1 if a firm doesn't report any product in the current year. The is_core_product variable indicates which product has the highest sales in a given year for each firm. The is_new_product variable takes the value of 1 if the product wasn't produced in the previous year. Finally, the higher_new_sales variable takes the value of 1 if the sum of sales of new products is greater than the sales of the core product.
use "your_data_file.dta", clear
gen firm_missing_year = 0
bysort id (year): egen last_year = max(year), unique(id)
replace firm_missing_year = 1 if year > last_year[1]
gen is_core_product = 0
bysort id year: egen max_sales = max(sales), unique(id year)
replace is_core_product = 1 if sales == max_sales
gen is_new_product = 0
bysort id year: gen lagged_product = product[_n-1]
replace is_new_product = 1 if product != lagged_product & sales != max_sales
bysort id year: egen sum_new_sales = sum(sales * is_new_product), unique(id year)
gen higher_new_sales = 0
replace higher_new_sales = 1 if sum_new_sales > max_sales

SQL Server 2016 - Transpose row to columns

I'm trying to figure out if it's possible to transform table rows to columns where the number of rows included changes at the time of the query. Here's a sample of what I'm trying to do:
Characteristics Table
strategy
year
month
aaa
aa
a
InvestmentA
2020
12
5
4
10
InvestmentB
2020
12
8
15
25
Investment(n)
2020
12
x
x
x
Output
year
month
Credit Type
InvestmentA
InvestmentA
Investment(n)
2020
12
aaa
5
8
x
2020
12
aa
4
15
x
2020
12
a
10
25
x

JFreeChart: How to define a DataSet that groups by year, month and day?

I have a SQL query already that gets the data I need but I'm struggling to figure out how to get that into a chart. This is sample data as result of my query:
year month day mode amount duration
2013 2 22 0 1 36001
2013 7 7 1 1 55062
2015 12 23 1 6 13
2015 12 23 4 4 11
2015 12 23 7 31 104
2015 12 23 8 2 4
2015 12 23 12 11 21
2015 12 23 13 3 8
2016 3 24 1 207 519
If I wanted to graph lets say amount grouped per year, month and day how would that be done in JFreeChart?

Required help in building a crosstab query - PostgreSQL

I have two tables.
Table1:
Label Date CT
A 2014-01-01 19
A 2014-02-01 10
A 2014-03-01 19
A 2014-04-01 18
B 2014-01-01 20
B 2014-02-01 16
B 2014-03-01 14
B 2014-04-01 16
C 2014-01-01 13
C 2014-02-01 12
C 2014-03-01 19
C 2014-04-01 14
Table2 :
Label Date CT
D 2014-01-01 19
D 2014-02-01 10
D 2014-03-01 19
D 2014-04-01 18
E 2014-01-01 20
E 2014-02-01 16
E 2014-03-01 14
E 2014-04-01 16
F 2014-01-01 13
F 2014-02-01 12
F 2014-03-01 19
F 2014-04-01 14
Desired Output :
Label Jan'14 Feb'14 Mar'14 Apr'14 Total
A 19 10 19 18 66
B 20 16 14 16 66
C 13 12 19 14 58
D 19 10 19 18 66
E 20 16 14 16 66
F 13 12 19 14 58
I'm new to PostgreSQL.
I wanted to take the unique values of Label column from both the table.
And produce the sum total of count to their respective label.
I can combine both the tables in a straight forward method using UNION ALL.
But that'll not give me the desired output or the view like a pivot.
I did google on this but nothing could help me out.
Came across this in SO. And I'm still trying on with it.
But I actually don't have a clue whether it can be done or not.
Can someone help me in getting the desired output.
Thanks in advance!!
Try Like This
select *,("Jan ''14" + "Feb ''14" + "Mar ''14" +"Apr ''14") as total
from crosstab($$
select id,to_char(da,'Mon ''yy') as tt,no from t2
union all
select id,to_char(da,'Mon ''yy') as tt,no from "T1"
$$,$$values ('Jan ''14'), ('Feb ''14'),('Mar ''14'),('Apr ''14') $$) as at
(id text, "Jan ''14" integer,"Feb ''14" integer,"Mar ''14" integer,
"Apr ''14" integer) order by id

need hint with a custom Linux/UNIX command line utlity "cal" in C

Ok I need to make this program to display "cal" 3 month(one month before and one month after) side by side, rather than just one single month it displays in any Linux/UNIX. I got it working to display 3 calendar by using "system(customCommand)" three times; but then it's not side by side.
I got some hint to use the following system calls:
close(..) pipe(..) dup2(..) read(..) and write(..)
my question is what should I start with? Do I need to create child process and than catch it in pipe(..)?
How can I display three calendar side by side.
ex.
February 2009 March 2009 April 2009
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4
8 9 10 11 12 13 14 8 9 10 11 12 13 14 5 6 7 8 9 10 11
15 16 17 18 19 20 21 15 16 17 18 19 20 21 12 13 14 15 16 17 18
22 23 24 25 26 27 28 22 23 24 25 26 27 28 19 20 21 22 23 24 25
29 30 31 26 27 28 29 30
Assuming you want to write it yourself instead of using "cal -3", what I'd do (in psuedo code):
popen three calls to "cal" with the appropriate args
while (at least one of the three pipes hasn't hit EOF yet)
{
read a line from the first if it isn't at EOF
pad the results out to a width W, print it
read a line from the second if it isn't at EOF
pad the results out to a width W, print it
read a line from the third if it isn't at EOF
print it
print "\n"
}
pclose all three.
if "cal -3" doesn't work, just use paste :)
$ TERM=linux setterm -regtabs 24
$ paste <(cal 2 2009) <(cal 3 2009) <(cal 4 2009)
febbraio 2009 marzo 2009 aprile 2009
do lu ma me gi ve sa do lu ma me gi ve sa do lu ma me gi ve sa
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4
8 9 10 11 12 13 14 8 9 10 11 12 13 14 5 6 7 8 9 10 11
15 16 17 18 19 20 21 15 16 17 18 19 20 21 12 13 14 15 16 17 18
22 23 24 25 26 27 28 22 23 24 25 26 27 28 19 20 21 22 23 24 25
29 30 31 26 27 28 29 30
$
(setterm ignores -regtabs unless TERM=linux or TERM=con.)
just do
cal -3
Does this not work?
cal -3
Ok, how about cal -3?
cal -3 12 2120 to make it a special month and year, with one before and one after.
The approach I would use for this would be to capture the output, split it into lines, and printf the lines out next to each other. I'd probably do it in Perl, though, rather than C.
Or just use cal -3, if your cal has it.

Resources