Data warehouse, SSAS - ssas

I have been given population data like this;
Year Region Population
----------------------------------
2012 District1 1000
2012 District2 1500
2012 District3 2000
Now I have to make a cube where a user can filter population in Month, Quarter and Year level. So I decided to enter data into a fact table with each and every month of the given year that means 12 records for each District with the same given number. So if a user asks for any month he will get the same count. But now the problem is if user does not filter it by Month, Quarter and Year I get the Sum of all the data that means District1 will display 12 times 1000 = 12000. How can I get 1000 for district1 at any given time? If data is in multiple years then also it should not sum them up. Is my approach wrong? Hope I am clear enough to explain the problem.

Your fact is a semi additive measure, more info at http://msdn.microsoft.com/en-us/library/ms175356.aspx

Related

Finding the difference of revenue on average between year 2013 and 2014, and group by-ed by product type

I have a table that shows sales of 4 different types of items across different years ( 2012 ~ 2016)
I want to know the increase/decrease of revenue in average between year 2013 and 2014 by each type of product_group.
I tried LAG OVER command but, It only show a result that group by-ed by just one column either year or product_gruop. I want a value that group by-ed by the two columns.
enter image description here

Add results to a row regarding the last 12 months rows- SQL Server

At my last meeting someone asked me if it was possible to hide people who where ill since a year from a dashboard. So I'm searching for the best way to actually KNOW who has been ill for 12 months.
I am working with a table with the number of days you've been absent for every kind of absence you could have, the number of days you should have been working that month, with a row per person, department and profession each month.
So it looks something like this :
PersonID
YEAR
MONTH
DEPARTMENT
PROFESSION
Absence1
Absence2
Absence3
WORKING DAYS OF THE MONTH...
11111
2021
07
HR
ASSISTANT
0
2
0
22
11111
2021
08
HR
ASSISTANT
0
0
0
22
==> So if I'm on a row of July 2021 I need to check the lines from June 2020 to June 2021.
My guess is that I need to add a column to this table who will say (with some kind of loop maybe) "if for the last 12 months (rows) the total number of days of absence equal the number of working days of the last 12 months then "ILL FOR A YEAR OR MORE" for each person (knowing that a person can work in more than one department or more than one profession so she'll have more than one row per month).
But I really have no idea how to actually write it in a script as I usually do very basic things. I'm using SQL SERVER and have 429 207 rows in the table. I'm thinking about doing it in the whole table and not only treating this month's rows because in the dashboard we show an historic.
Your table is heavily denormalized. If you want to represent all this information in the database, I would have expected the following tables, instead of just one:
Person
Department
Illness (list of illnesses)
IllnessAbsence (join table between Person and Illness)
Either way, you can get the information you need with something like this:
I've assumed you want the whole table, so you need a window function
We need to flip the logic on its head: exclude all rows which have no non-absence in the last 12 months
SELECT
PersonID,
YEAR,
MONTH,
DEPARTMENT,
PROFESSION,
ILLNESS1,
ILLNESS2,
ILLNESS3,
[WORKING DAYS OF THE MONTH]
FROM (
SELECT *,
NotIllLast12Months = COUNT(CASE WHEN DATEFROMPARTS(YEAR + 1, MONTH, 1) >= GETDATE()
AND ILLNESS1 + ILLNESS2 + ILLNESS3 = 0 THEN 1 END)
OVER (PARTITION BY ID)
FROM HETP_ABS
) abs
WHERE NotIllLast12Months > 0;

Same period last year. Leap year

I have 3 columns in my SSAS tabular model: Date, Amount, Amount n-1
The problem is that I would like to have for example a row with:
Date Amount Amount n-1
29thFeb 0 2000$
is there any way to deal with leap years situations? As 29th Feb 2017 does not exist, this row does not show up in my table.
My datasource is just a simple SQL Server Sales Table with 2 columns, Date and Amount
Example of what I would like to achieve when using Date, Amount and Amount(n-1) in a PivotTable, using my SSAS tabular model as datasource:
Date Amount Amount n-1
1stFeb 1500$ 1700$
2ndFeb 1300$ 1000$
...
28thFeb 700$ 800$
29thFeb 0 2000$
TOTAL 3500$ 5500$

Ms ACCESS: calculating past annual averages over varying date ranges

In a form on Ms ACCESS, a user can select a commodity (such as copper, nickel, etc.) from a list and a commodity price date from a list. A trailing 12 month average commodity price should then be calculated.
For example: the user selects Copper as commodity and February 1st 2010, 02/01/2010. I then want the average price to be calculated over the time period: [02/01/2009 - 02/01/2010].
I'm not sure how to write this in query form. This is the current incomplete code;
SELECT Avg(CommPrices.Price) AS Expr1,
FROM CommPrices
WHERE (((CommPrices.Commodity)=[Forms]![Tool Should Cost]![List243]))
AND CommPrices.DateComm = [Forms]![Tool Should Cost]![List55];
List243 is the list of commodities the user can select from, list55 is the list of dates the user can select. All data is obtained from the table CommPrices.
Note: the earliest dates in the column DateComm is 01/01/2008. So if the user selects a date for example 02/01/2008, then calculating the average over the past 12 months before 02/01/2008 won't be possible. I do want the code to still calculate the average using the dates available. (in the example it would just be the average over the past month)
Second Note: the column DateComm only has monthly dates for the first day of every month (e.g 01/01/2008, 02/01/2008, 03/01/2008). The dates listed in list55 can refer to different days in the month (e.g 03/16/2009), in that case I want the code to still calculate the past 12 month average using the closest commodity dates possible. So if the user selects date 03/16/2009, I want the code to calculate the 12 month average for 03/01/2008 - 03/01/2009.
For "integer" months it would be:
SELECT
Avg(CommPrices.Price) AS AveragePrice,
FROM
CommPrices
WHERE
CommPrices.Commodity=[Forms]![Tool Should Cost]![List243]
AND
CommPrices.DateComm = BETWEEN
DateSerial(Year([Forms]![Tool Should Cost]![List55]) - 1, Month([Forms]![Tool Should Cost]![List55]), 1)
AND
DateSerial(Year([Forms]![Tool Should Cost]![List55]), Month([Forms]![Tool Should Cost]![List55]), 1)

extracting common data of current months and last two monnth sql

I have a table with more than 20000 rows, In one of column i have month from jan 2014 to Dec 2014, and in another column i have a loan number. Most of the loan Numbers are reapeting every months,now i need to get only the loan Number which are apperead in all three monthy consecutively. For eg if i am getting data for current months i also wanted get data which are common in two months before the current months. The database that i m using is Access DB. Any adivice will be more than a help, Thanks in Advance.
SELECT Loans.LoanID, Sum(IIf([period]=[month],1,0)) AS CM, Sum(IIf([period]=[month]-1,1,0)) AS [m-1], Sum(IIf([period]=[month]-2,1,0)) AS [m-2]
FROM Loans
GROUP BY Loans.LoanID
HAVING (((Sum(IIf([period]=[month],1,0)))>1) AND ((Sum(IIf([period]=[month]-1,1,0)))>1) AND ((Sum(IIf([period]=[month]-2,1,0)))>1));
I used month as an integer, and didn't make any adjustment for months 1 and 2 to loop back and look at prior year - you should be able to modify this based on the actual format you are using for the month.