Filter Table Using Value Selected In Excel Pivot Table Connected to SSAS Cube - ssas

I am trying to combine two tables in an SSAS cube and filter one of the tables depending on a value selected by the user in a pivot table from an Excel file connected to the cube. Is this possible and if so how do I do it?
To describe in more detail, I have two tables: history and forecast. For history there is only one set of records for each time period, but there are many forecasts for most periods. When a user selects (in the Excel pivot) a particular forecast version to view, I want to show them all of the forecast records available for that forecast and also history but only for periods that preceded the forecast.
So for example if a user today selects (in the Excel pivot) forecast 1, produced in 2021 Q3, the following is where the records come from:
2021 Q1 - history
2021 Q2 - history
2021 Q3 - forecast
2021 Q4 - forecast
2022 Q1 - forecast
2022 Q2 - forecast
but if a user today selects forecast 2, produced in 2022 Q1, the following is where the records come from:
2021 Q1 - history
2021 Q2 - history
2021 Q3 - history
2021 Q4 - history
2022 Q1 - forecast
2022 Q2 - forecast
Hopefully its clear what I'm trying to accomplish. If this approach won't work but there is another way to accomplish this, please let me know.
Thank you in advance for your help!

Create a new table that is unconnected in the model for the filter that lists your forecast periods. Mine here has a new forecast every quarter.
The start date is the column used to determine whether to use History or Forecast in these DAX measures:
Actual Value = IF(HASONEVALUE('Filter'[Period]),
CALCULATE( SUM(History[Value]), History[Date] < MAX('Filter'[Start date]) ),
"Select quarter")
Forecast Value = IF(HASONEVALUE('Filter'[Period]),
CALCULATE( SUM(Forecast[Forecast]), Forecast[Date] >= MAX('Filter'[Start date]) ),
"Select quarter")
Actual or Forecast Value = IF(HASONEVALUE('Filter'[Period]),
[Actual Value] + [Forecast Value],
"Select quarter")
And here's what it looks like in Excel:

Related

Teradata - How to Look up value based on today(date) from table C then filter Dates from Table A

I am a complete novice at this so please go easy on me, i have found some examples on here but have yet to be able to get them to work.
I have two tables:
Table C lists, Date, Financial Year - Financial Year is text and might look like "Year 21" if the date was 31 August 2022 and "Year 2022" if 1St September 2022 the exact date it changes is somewhat dynamic year on year but is stored in this table correctly.
Table A lists the main Data and includes columns like Store, item, Cost, Date - it does NOT include the financial year and i need it too.
I can join the Financial Year column by matching the dates from the two tables. Simple.
What I need to do is then show only the data in table A from Weeks 1 to the most recent week of the CURRENT Financial Year
Just now i have pulled the last full 6 weeks, but this wont be helpful in WK5, this would pull wk52, WK1 WK2, and so on.
Below pulls the correct data for the last 6 full weeks but i need it to pull everything in this Financial year which can be determined by looking up today against C.THE_DATE and returning the C.YEAR_NBR
I hope that makes sense? i'm probably over complicating all this, including the below so thoughts and feedback welcome!
SELECT
A.WEEK_NBR as "Week",
C.YEAR_NBR as "Year",
A.PERD_TO_DT as "Date",
A.LOC_NBR as "Store Number",
A.ITM_NBR as "SKU",
oreplace(A.ADJ_REAS,'Code 55','Audit Adjustment') as "Adj Reason",
SUM(A.ADJ_QTY) as "Adj Qty",
round(SUM(A.ADJ_CST), 2) as "Cost",
case when A.ADJ_REAS = 'Code 55' then round(SUM(A.ADJ_CST), 2) else 0 end as "Audit Cost",
case when A.ADJ_REAS = 'Code 55' then SUM(A.ADJ_QTY) else 0 end as "Audit Qty"
FROM MICROST.weekly_losses_item A
JOIN PRODUCT.CALENDAR as C on A.PERD_TO_DT = C.THE_DATE
WHERE A.PERD_TO_DT between NEXT_Day(CURRENT_DATE - 49,'Sunday') and NEXT_Day(CURRENT_DATE - 7,'Saturday')
AND A.ADJ_CST <>0
AND A.ADJ_QTY <>0
GROUP BY A.LOC_NBR, A.ITM_NBR, A.ADJ_REAS,A.WEEK_NBR, C.YEAR_NBR,A.PERD_TO_DT

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

Forcing rows to appear despite data missing

I have a large data set (called input below) which contains a variety of information such as sales dates, transaction dates, payments, sales.
The user can produce a report by year, quarter or month to show the amount of payment in a particular month/quarter/year of a certain sales year. So you could have a payment 5 years after the initial sale, i.e. a payment in 2016 relating to the sales year of 2011.
The user can decide whether they want to have these payment periods by month/quarter/year at the beginning of the code through the use of macro variables (i.e. %let ReportBasis = Year) and ReportBasis can be called on through the rest of the code without manual adjustments.
The report is produced using:
proc sql;
create table report as
select sales_year, &ReportBasis, Sum(Sales) as Sales
from input
group by sales_year, &ReportBasis;
quit;
Now the issue I am having is that if there is no payment in a particular period for all sales years in question, then there is no row for that period. This produces a layout problem.
I require a solution to this which can be dynamic due to the nature of the macro variable (changing from year to month to quarter).
So I have this (example):
2011 Month 11 100
2011 Month 12 250
2011 Month 13 85
2011 Month 15 90
2011 Month 16 300
But I require this:
2011 Month 11 100
2011 Month 12 250
2011 Month 13 85
2011 Month 14 0
2011 Month 15 90
2011 Month 16 300
where there is no actual payment in month 14 in all of my data (even other years 2012, 2013 etc.), so it doesn't show up in my first table, but the second table still cleverly knows to include it.
Thanks in advance.
Here is one solution that assumes that all years and reports are represented in input, although not all combinations:
create table report as
select sy.sales_year, r.rb, Sum(i.Sales) as Sales
from (select distinct sales_year from input) sy cross join
(select distinct &ReportBasis as rb from input) r left join
input i
on i.sales_year = sy.sales_year and i.&ReportBasis = r.&ReportBasis
group by sy.sales_year, r.rb;

DAX Dynamic Total with Power View

I am attempting to graph, via Power View, the sum of all account balances at various points in time, so that I can see how the total is changing.
I do not have transaction history (add / subtract), only 'Balance' at a specific point in time for each account.
A simplified example of my data is:
Account Date Balance
a111 01 January 2015 100
a111 01 February 2015 150
b222 01 March 2015 200
c333 01 March 2015 300
b222 01 May 2015 150
d444 01 June 2015 400
As far as I can tell, I need to create a measure to generate a dynamic rank per 'Account', ordered by latest date. I should then be able to create a second measure to SUM each account where the rank = 1.
There is a similar example of this issue discussed in the question PowerPivot DAX - Dynamic Ranking Per Group (Min Per Group), however I cannot seem to get this to display how I want on a line graph in Power View.
What I want to display on the line graph is (letting the graph sort out the missing dates):
Date Balance
01 Jan 2015 100 -- Account a111 only
01 Feb 2015 150 -- Account a111 only, but the new value
01 Mar 2015 650 -- Accounts a111 (latest), b222 and c333
01 May 2015 600 -- As above, but account b222 is updated
01 Jun 2015 1000 -- Latest values for all accounts
However, what I am currently seeing is:
Date Balance
01 Jan 2015 100 -- Sum of Balances matching this date
01 Feb 2015 150 -- As above
01 Mar 2015 500 -- As above
01 May 2015 150 -- As above
01 Jun 2015 400 -- As above
The problem, as far as I can tell, is that at each data point in the graph, the 'filter context' is reducing down to only the rows that match the date, whereas I need all rows on on before that date with a 'rank' of 1.
For reference, I am using Excel 2013 and the data is ultimately coming in via Power Query (pulling data from SharePoint), so I can work some magic there if necessary (i.e. generating a numeric 'group id' for use with DAX MAX() function, as that doesn't seem to like strings).
I hope this is not to confusing, and thanks in advance for any help.
The solution to this is to have a separate date dimension table so balance can be independently calculated on a per-month basis without respect to the fact table.
Let's assume you have a date table called Date.
So, first create the base measure
[Total Balance] := SUM( FactTable[Balance] )
Next we calculate the first month in the fact table to use as a floor for looking through our Date table values.
[First Ever Balance Date] := FIRSTDATE( ALL( FactTable[Date] ) )
And now we determine the last date with a balance for the date range between that first month and the current month in context:
[Last Balance Date] :=
LASTNONBLANK (
DATESBETWEEN ( Date[Date], [First Ever Balance Date], MAX ( Date[Month] ) ),
[Total Balance]
)
So take account a111 in the month of 5/1/2015. This measure will look in all the dates between 1/1/2015 (our [First Ever Balance Date] ) and 5/1/2015 ( the MAX Date[Month] ) , find the last row with a balance and return the corresponding Date[Date] ( 2/1/2015 ).
This measure will return BLANK for any month before an account's initial balance. Using BLANKS in date filtering doesn't work, so we should replace these BLANK values with the [First Ever Balance Date] to ensure the running balance for that account will still return 0.
[Last Balance Date Remove Blanks] :=
IF (
ISBLANK ( [Last Balance Date] ),
[First Ever Balance Date],
[Last Balance Date]
)
Now we can calculate the current balance of any account for any month by looking up the balance as of the Last Balance Date Remove Blanks
[Last Balance] :=
CALCULATE (
[Total Balance],
DATESBETWEEN ( Date[Date], [Last Balance Date Remove Blanks],
[Last Balance Date Remove Blanks] )
)
And finally we use a SUMX across the accounts to add up their individual Last Balance amounts.
[Account Total Balance] := SUMX( ALL( FactTable[Account] ), [Last Balance] )
Now in your pivot table you just put Date[Month] on the rows and [Account Total Balance] as the measure. Note this will include months with no fact table rows like 4/1/2015 in your sample set. If you wish to exclude these, add another intermediate measure to count your fact table rows:
[Fact Table Row Count] := COUNTROWS ( FactTable )
And then create a measure to return BLANK() if [Fact Table Row Count] is BLANK(), and otherwise [Account Total Balance]
[Account Balance Hide Rows] :=
IF (
ISBLANK ( [Fact Table Row Count] ),
BLANK (),
[Account Balance]
)

MDX Last Child with Filter

I am using the following calculation in my cube to get balances for P&L and Balance Sheet accounts. this is working fine but the problem is that this code doesn't work if I have filters on the date dimension.
For example, let's say I am looking at amounts for the 4 quarters for 2014 - the sub-total in this case for balance sheet accounts would be the Amount in 2014 Q4(and the calculation works correctly). However, if when am looking at say two quarters Q1 and Q2 for 2014, the calculation still shows the sub-total as the Amount in 2014 Q4 whereas the correct value should be Amount in 2014 Q2. I have pasted a screen shot of Excel pivot table also.
CASE [Accounts].[Account Type].CurrentMember.MemberValue
WHEN 'Balance Sheet'
THEN ([Dates].[Hierarchy].currentMember.lastChild, [Measures].[Measures].[Amount])
ELSE Amount
END
Any idea how to achieve this?