I had an expression that looked like this:
=Sum(Fields!MO1.Value, "DataSet_Eligibility") +
Sum(Fields!MO2.Value, "DataSet_Eligibility") +
Sum(Fields!MO3.Value, "DataSet_Eligibility") +
Sum(Fields!MO4.Value, "DataSet_Eligibility") +
Sum(Fields!MO5.Value, "DataSet_Eligibility") +
Sum(Fields!MO6.Value, "DataSet_Eligibility")
Which worked great.
Then I added parameters that let the user choose date ranges so it could be 3 months or could be 24 months (or more). My dynamic query creates the number of fields based on however many months (3 months = 3 fields, 24 months = 24 fields).
Is there a way to dynamically create the formula above based on the number of months?
I can calculate how many months there are, but I can't figure out how to create the formula that only sums the number of months/fields that I have.
Related
I am coding in Netezza and need to figure out how to use the Fiscal Period entered by the user at runtime to determine where to stop the TSL calculation.
Example: If we run today (June is Fiscal Period 9), and we enter FY 2020 and Period 7 (April) then the 'Amount' calculation would only include the sum of TSLVT + TSL01 + TSL02 + TSL03 + TSL04 + TSL05 + TSL06 + TSL07. Under this scenario, the calculation would need to explicitly leave out Periods 8 and 9 (exclude TSL08 and TSL09) so that we get the FY20 Amount through Period 7.
How to get current year months list from (Jan-19-Dec-19) and previous Year month list from (Jan-18-Dec-18) in similar format?
Expected Out:-
[Jan-18]
To
[Dec-18]
[Jan-19]
To
[Dec-19]
Can you help me on this.Please
If you just want to have the list of the 24 months you mentioned (current and last year) in the desired format, a solution would be:
SELECT FORMAT(DATEADD(month,number,CAST(YEAR(DATEADD(YEAR,-1,GETDATE())) AS varchar(4)) + '-01-01'),'MMM') + '-' +
SUBSTRING(CAST(CAST(YEAR(GETDATE()) AS VARCHAR(4)) + IIF(number<12,-1,0) AS VARCHAR(4)),3,2)
FROM master..spt_values
WHERE type = 'P'
AND number < 24
I have a year column that contains things like 2013, 2012, etc and a month column that displays 1-12. I need to use these columns to create a date that always pulls the last day of the month that the year and month represents as yyyymmdd. For example, if year = 2018 and Month = 3, I need the date to display as 20180331. The year and month fields are numeric. I am using SQL Server 2014 Management Studio Any ideas on how to do this?
You could shoe horn it this way:
select REPLACE(CAST(EOMONTH(DATEFROMPARTS(2018,2,1)) as varchar(20)), '-','')
I was able to find a formula that works, although the answer above is smaller in code and may be a more efficient way to accomplish the same thing, which I can test out. Here is the code that I was able to get to work for those interested.
CONVERT(VARCHAR(10),eomonth(CAST(CAST(FV_RPT_EMPLOYEECENSUS_ASOF.HRYEAR AS VARCHAR(4)) + RIGHT('0' + CAST(FV_RPT_EMPLOYEECENSUS_ASOF.HRPERIOD AS VARCHAR(2)), 2) + RIGHT('0' + CAST(01 AS VARCHAR(2)), 2) AS DATE)),112)
I have a cube with two dates - Date of Birth and Date of Purchase. I would like to create a calculated member which produces the age in years based on these two dates. I can already do this in the dataset once the query has been run however I would like to include all empty values as I have a layout based on showing all the blanks as well as the "used" values.
Can someone give me an example of how to calculate this in MDX as running with the two date sets currently causes memory issues.
A Basic Idea. This returns only one measure when two dates are given.
Please modify it as per your necessity.
With
Member NumOfDays As
{[Date].[Calendar Month Date].[Date].&[19850101]:[Date].[Calendar Month Date].[Date].&[19870927]}.Count
Member TotalYears As NumOfDays/365
Member Years As Int(TotalYears)
Member YearParts As TotalYears - Years
Member TotalMonths As (YearParts * 12)
Member Months As Int(TotalMonths)
Member MonthParts As TotalMonths - Months
Member Days As Int(MonthParts * 30)
Member Age As CSTR(Years) + " years, " + CSTR(Months) + " months and " + CSTR(Days) + " days"
Select
{NumOfDays, TotalYears, Years, YearParts, TotalMonths, Months, MonthParts, Days, Age}
On 0
From [Adventure Works]
The output should come as below.
I have 2 parameters in my report, Month and Year.
I also have a table which in one column has the count of rows for that particular month and another column which has the count of rows for that specific month and the next 5 months (Rolling 6 month).
Both of these columns have an expression which links to the parameters as shown below:
Month Expression:
6 Month Expression:
My parameters use specific values which i have put in (1 - 12) so when my rolling month adds 1 value each time as you can probably guess this wont work when I come near the latter months of the year as my expression does not roll over to next year and just stops at December.
Overview: Parameters are integer values 1 - 12 and I am not sure how to calculate a rolling 6 months which will carry over to the next year when selecting months late in the year. e.g. If I select November as my Month parameter, my rolling 6 months will only display the sum of rows for November and December, not going to the next year. I am assuming this is because my month values are integers and in my expression I add numbers to the integers for each month therefore my rolling 6 will be trying to add months 11, 12, 13, 14, 15, 16 when obviously months only go to 12.
This is not an answer to your question, but looking at this, you may be able to find a solution with SQL itself.
See the query below. Consider this as your report source sql.
DECLARE #A TABLE (ID INT IDENTITY(1,1),DT DATE)
INSERT INTO #A (DT)
VALUES
('2013-01-26'),('2013-02-23'),('2013-03-20'),
('2013-04-23'),('2013-05-23'),('2013-07-23'),
('2013-08-23'),('2013-08-29'),('2013-09-23'),
('2013-12-10'),('2014-03-01')
If you join the result with itself, some what like below, you will get the result in sql query itself, you need to only show it in report. See below.
SELECT DATEPART(YEAR,DT) [Y],DATEPART(MONTH,DT) [M],COUNT(*) [ROLLING 6]
FROM (
SELECT A.*
FROM #A A,#A B
WHERE ((DATEPART(YEAR,B.DT) * 12) + DATEPART(MONTH,B.DT)) BETWEEN
(DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) AND
((DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) + 6)) LU
GROUP BY DATEPART(YEAR,DT),DATEPART(MONTH,DT)
ORDER BY [Y],[M]