I have a dataset that should output the last segment of each quarter (2015 Q1, Q2, Q3, Q4). With the latest segment I mean the last value of the segment attribute from the first week in its respective quarter until the selected date as a parameter.
The query below is outputting
2015 Q1 - C
2015 Q2 - /
The problem is that from 2015 W1 - 2015 W14 the segment is always C. After W14 the segment becomes "/". It thus look to me that the query is not taking into account the YTD function.
SET [All calendars] AS
[Date invoice].[Bonus calendar - Quarter].[Bonus Quarter].ALLMEMBERS
SET [Stratum history] AS
Generate(
{[All calendars]} AS s
,s.Current
*HEAD(
NonEmpty
(
[Stratum history].[Stratum].[Stratum]
,[Stratum sales average]
)
,1
)
)
The where clause:
WHERE
//Only calculate for the current bonus year until the current week
StrToMember("[Date invoice].[Bonus year].&[" + CStr(StrToMember(#PAR_Date).Parent.MemberValue) + "]")
*Ytd(StrToMember(#PAR_Date))
*StrToMember(#POS_ID)
If I change the All calendars set to [Date invoice].[Bonus calendar - Quarte].[Bonus week of quarter].ALLMEMBERS then I get the full set
2015 W1 - C
...
2015 W14 - C
The query then stops at 2015 W14 as specified in my where clause! But I only want to show the quarter level on my rows.
Any advise?
How about you modify the NonEmpty clause of the second set a bit like below:
SET [Stratum history] AS
Generate(
{[All calendars]} AS s
,s.Current
*HEAD(
NonEmpty
(
[Stratum history].[Stratum].[Stratum].MEMBERS
,([Stratum sales average], s.Current)
)
,1
)
)
Related
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:
I am building some reports in Pyramid Analytics. I created a custom set and tried to write a MDX query which will retrieve the first day of the first month for the first three quarters of the previous year, selected in the slicer. i.e.
If I choose in my slicer 2017, I expect to see the following in the body of the report: Jan 1st 2016, April 1st 2016 and July 1st 2016. These will change according to the selection in the slicer.
I only got as far as the syntax below, which only returns first day of the first month of the first quarter of the previous year i.e. Jan 1st 2016
[Policy - Inception Date].[Calendar Hierarchy].[!#NewPar#!].PREVMEMBER.FIRSTCHILD.FIRSTCHILD.FIRSTCHILD
Note:Newpar = Parameter
Can you please help with the correct syntax?
Thanks.
Without knowing the structure of your Date dimension I'm having to use some guess work but I've made the following in Pyramid and it works fine against our cube:
Generate
(
Head //<<<this will get the first 3 quarters
(
Descendants
(
{[Date].[Date - Calendar Month].[!#aYear#!].PrevMember} //<<<this will get the previous year
,[Date].[Date - Calendar Month].[Calendar Quarter]
)
,3
)
,Head //<<<this will get the first day of each of the quarters found in the first argument of the generate function
(
Descendants
(
[Date].[Date - Calendar Month].CurrentMember
,[Date].[Date - Calendar Month].[Calendar Day]
)
,1
)
)
Here is the result with the year parameter in the top left:
Introduction
Currently I'm doing a maintenance job on a measure which annualizes another (YTD) measure. The granularity of measures is month. The annualization works fine on month level, but when we use parents in the date hierachy the calculation does not work.
It should work like this:
MeasureAnnualized = YTDMeasure * (CurrentMonth / 12)
CurrentMonth is implemented as
ClosingPeriod([Date].[DateHierarchy].[Month], [Date].[DateHierarchy]).MemberValue
On month level this works fine:
Month MeasureYTD MeasureAnnualized ClosingPeriod
july -50 -85,71 7
august -60 -90,00 8
september (null) (null) 9
But on a higher level in the DateHierarchy this does not work. For instance on Quarter level:
Quarter MeasureYTD MeasureAnnualized ClosingPeriod
3 -60 -80,00 (should be : -90,00) 9 (should be: 8)
As you can see, it picks the ClosingPeriod of quarter 3 (=9) whereas this should be the last "non-empty" ClosingPeriod (=8). The same applies to Year level:
Year MeasureYTD MeasureAnnualized ClosingPeriod
2016 -60 -60,00 (should be : -90,00) 12 (should be: 8)
The question therefore is: how can I get this ClosingPeriod to be the last Period in which the YTD measure is not (null)?
In the definition on msdn of ClosingPeriod it gives an equivalent nested version of the same logic:
Tail(Descendants(Member_Expression, Level_Expression), 1)
The below is similar to this with an added NonEmpty:
WITH
MEMBER Measures.[LastNonEmpty] AS
Tail
(
NonEmpty
(
(EXISTING
[Date].[Calendar].[Month])
,[Measures].[Internet Sales Amount]
)
,1
).Item(0).MemberValue
SELECT
Measures.[LastNonEmpty] ON 0
,[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM [Adventure Works];
It returns the following:
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]
)
We want to show current periods sales versus previous period sales.
To show the previous period we make use of a date dimenion table and the following calculation:
CALCULATE(SalesValueGross; DATEADD(Date[Date]; -1; YEAR))
Unfortunately somehow it shows minor (decimal) differences when comparing years.
The difference get's bigger when we slice to months.
Another issue we have is that this calculation does not seem to work when comparing (for example) week 1 - 2015 with week 1 - 2014.
Any help is greatly appreciated!
When I want to get prior calendar year sales, I use the a formula such as the following:
Cal Prior Yr Sales:=if(HASONEVALUE('Sale Date'[Calendar Year]),
Calculate([Total Sales],
PREVIOUSYEAR('Sale Date'[Date])),BLANK())
The HASONEVALUE just ensures that there is only one year selected so it will know the correct previous year to retrieve.
You can make a series of calculations that will let you use one calc that determines what level of the date hierarchy you are in (assuming you have the fields available in your date table). Here is something I've used in the past, with a fiscal calendar that was different from the normal calendar.
First, the base calculations:
Sales Same Week Prior Year:=
CALCULATE([Total Sales],Filter(All('Sale Date'),
'Sale Date'[Week Key] = max('Sale Date'[Same Week Last Year])))
Sales Same Month Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Month Seq] = max('Sale Date'[Month Seq])-12))
Sales Same Quarter Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Quarter Seq] = max('Sale Date'[Quarter Seq])-4))
Sales Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Fiscal Year] = max('Sale Date'[Fiscal Year])-1))
You can hide all of those calculations and then create one last calculation and leave it visible:
Sales Same Period Last Year:=
if(HASONEVALUE('Sale Date'[Week Key]), [Sales Same Week Prior Year],
if(HASONEVALUE('Sale Date'[Month Key]),[Sales Same Month Prior Year],
if(HASONEVALUE('Sale Date'[Quarter Key]),[Sales Same Quarter Prior Year],
if(HASONEVALUE('Sale Date'[Fiscal Year]), [Sales Prior Year], BLANK()))))
You may need to add a couple of calculated fields to your date table to make it work. I have fields for: [Same Week Last Year], [Month Seq], [Quarter Seq]. Same week last year is an integer field that is yyyyww. Month Seq and Quarter Seq are just autoincrementing integers in chronological order that do not repeat.
My formula for same week last year is
=if('Sale Date'[Week Nbr] = 53, (('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]-1),
(('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]))
I did the sequence numbers in my SQL view, which is the source for the date date. As an example, if my date table starts at 1/1/2010, the month seq for Jan 2010 is 1 and the month seq for Jan 2011 is 13. The quarter seq for Q1 2010 is 1 and the quarter seq for Q1 2012 is 9.
http://www.daxpatterns.com/time-patterns/ is a good read for this topic.