Same period last year. Leap year - powerpivot

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$

Related

How to calculate sum of amount for current month including the amount of previous months in Power Bi?

I would like to create DAX expression. This is the condition and I have my month and year table in separate column.
If we choose Month as a March then it should show the sum of amount of Jan, Feb and as well as March. Similarly, If we choose Month as Dec then it should have the sum from Jan to Dec.
How can we write DAX expression for this condition in Power Bi?
You probably need a measure as below. You need to have a separate Date/Calendar table for that which is connected to your Data table using Date column.
total_amount_ytd =
CALCULATE(
SUM(your_table_name[your_amount_column_name]),
DATESYTD(your_date_table_name[your_date_column_name])
)
With this above Measure, if you add Year/Month in a table with the new Measure, you will find the value per year will increase from January to December for a single year and starts re calculating from January again in the next year.

SQL - SUM two columns with a date range on one of the columns

First off its been a long week and I'm having a moment e.g. I just can't figure this out but I know its straight forward.
I have a CTE with a query for a GL Detail and one for Budget which are bought together to display all the details needed for a report.
For example the GL detail bring back the actual amount from a date range such as 2019-01-01 and 2019-04-01
and the budget part brings back the budget for each month so the date range is 2019-01-01 and 2019-12-01 as I need to sum up the total budget in a report.
Using an iif expression in SSRS the report shows the actuals up to the month a user select (there is on a to_date parameter)such as April then the report shows actual from Jan and then budget from May to Dec. I need to show the total of budget for the 12 month which is straight forward as I have bought each month amount back.
My question is I need to sum the GL actual amount for the months selected in by the user Jan to April and the remaining budget amounts May to Dec. For some reason I just can't work it out in my head.
Edit - I'm using
postgresql
SSRS - Microsoft SQL Server Reporting Services
columns are in GL is gldetail.amount and for Budget budget.amount_budget - I have a column in both gldetail and budget queries which is called post_date.
Worked it out -
,case
when src.post_date <= src._to_date then src.amount
when src.post_date > src._to_date then src.amount_budget
end as actual_budget_total
Then in the report sum the actual_budget_total

MDX - Average over the whole time period, even when no data exists

I have a fact table with 1 row for each day. Example:
ID Date Value
1 20190101 10
1 20190102 15
2 20190101 31
If I take a simple Value average in SSAS cube I get:
ID Average <Formula>
1 12.5 (10+15)/2
2 15.5 31/2
As I understand, 15.5 is there because in total there are 2 days in the scope as only two days exist in the fact data when I select the whole month.
However, I need to calculate a monthly average instead. It should check that there are 31 days in that month (based on Date dimension) and get this result:
ID Average <Formula>
1 0.8 (10+15)/31
2 1 31/31
So far I've tried to create some "fake rows" if my data, for example I've tried to create rows with Value = 0 for dates 20190103-20190131 for ID=1.
This works, it forces the calculation for ID=1 to always take all days in the period, but it messes up my other calculations in the cube.
Any other ways to force average calculation in SSAS multidimensional cube to always calculate for the entire month?
If you want to do the calculation in the Cube, you can use the Descendants function on your Date dimension
For eg., the following gives the number of days in a month using the AdventureWorks sample
WITH MEMBER Measures.DayCount AS
Descendants
(
[Date].[Calendar].CurrentMember,
[Date].[Calendar].[Date],
LEAVES
).Count
SELECT [Measures].[DayCount] ON 0,
[Date].[Calendar].[Month].ALLMEMBERS ON 1
FROM [Adventure Works]
I would recommend:
select id, eomonth(date) as eom,
sum(value) * 1.0 / day(eomonth(date)) as average
from t
group by id, eomonth(date);
EOMONTH() returns the last day of the month. You can extract the day to get the number of days in the month.
The * 1.0is because SQL Server does integer division. Your numbers look like integers, but if you are getting 15.5, then you actually have numerics or something other than an integer.

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)

Data warehouse, 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