MDX - Format of Month member in dynamic string - mdx

I have a dynamic string to calculate the current month from my time dimension using MDX. The string looks like this:
StrToMember("[Time Sales Stage].[Time Sales Stage YQMD].[Year].&["
+ format(now(),"yyyy") + "].&[Q"
+ format(datepart("q", now())) + "].&["
+ format(now(), "MMM" + "]"))
The "MMM" part in the end, should return "May" but it now actually returns "Mai" which is my local language name for the month. When this value is returned, my query fails due to that the member name being asked for, is not a valid member.
Is there any way to format the "MMM" part to return the english name of the month?

Related

How to display a month in VBA given conditions

Currently I am trying to display a month given certain conditions in VBA. I have two columns named Quarter and Exact Month in Quarter. Based upon these two conditions, I would like to display a month.
For example, For Quarter 1 Month 1, I would like the new column to display January.
Thank you for your help.
You can just offset each quarter by a value of 3. For instance, Q2 will correspond with 3. When you add your quarter serial #, it will be converted into a calendar serial # (1-12). After that, you just need to set the desired format.
This will just be an excel solution, and if this needs to be in VBA, you can apply similar logic there.
You can do this a few ways. The below image just serves as an example. How to apply this will depend on your data structure. You may need to implement a VLOOKUP as such.
G2 = TEXT(Date(2018, E2 + VLOOKUP(D2,$A$2:$B:$5,2,0),"MMM")
[1] Only one Excel Formula in Column C - doesn't need any helper columns:
This example e.g. for cell C2 as shown in your post converts quarters * 3 months (1 quarter = 3 months) minus 1 plus month in quarter to DATE (argument year can be any year >= 1900) and displays the month's Long form via TEXT argument MMMM; quarter values are extracted from the last string position in column A ( here A2) via RIGHT:
=TEXT(DATE(1900,3*(RIGHT($A2,1)-1)+$B2,1),"MMMM")
[2] Approach via VBA Example Function
Uses the same logic as above:
Sub Test()
MsgBox getMonthInQuarter("Quarter 4", 3)
End Sub
Function getMonthInQuarter(ByVal quarter As Variant, ByVal month As Integer) As String
Const MonthsInQuarter As Integer = 3
Dim q As Integer: q = Val(Right(quarter, 1))
' Return function value
getMonthInQuarter = Format(DateSerial(1900, MonthsInQuarter * (q - 1) + month, 1), "MMMM")
End Function

Setting Access dates to various weekdays of the month

I have done a lot of research over the internet for something to help me with what I am trying to do, but haven't been able to come up with anything that helps my situation.
I'm new to using MS Access (using 2013 version) and I am working with a database through my SharePoint 2013 site. What I have is a SharePoint site that keeps track of lots of events through multiple lists. Essentially what I am trying to do is create a list template in SharePoint that I can just upload to the site instead of re-creating the list from scratch every month. With this list I have 1800 events per month and am trying to set the "Date" fields to the first, second, third, etc. Monday of the month, I am trying to do this for Tuesday through Friday also, so that way I don't have to manually set 1800 dates.
I figured this would be possible through MS Access rather than through SharePoint itself. At first I thought I would be able to set the default value to what I need each date field to be, but from my understanding this won't work, so I'm trying to figure out if there is a way to accomplish this in SQL. Now keep in mind that I have never used SQL before so I really don't know what I'm doing with it.
So in case that I didn't explain what I'm trying to do well, I'm trying to set date fields in Access to the 3rd Wednesday of the month, the second Thursday of the month, etc.
Any help is appreciated!
Thanks!
It will be some work to translate to pure SQL as the count of a weekday in a month will be either 4 or 5, but here is a generic function using VBA that does exactly this:
' Calculates the date of the occurrence of Weekday in the month of DateInMonth.
'
' If Occurrence is 0 or negative, the first occurrence of Weekday in the month is assumed.
' If Occurrence is 5 or larger, the last occurrence of Weekday in the month is assumed.
'
' If Weekday is invalid or not specified, the weekday of DateInMonth is used.
'
' 2016-06-09. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateWeekdayInMonth( _
ByVal DateInMonth As Date, _
Optional ByVal Occurrence As Integer, _
Optional ByVal Weekday As VbDayOfWeek = -1) _
As Date
Const DaysInWeek As Integer = 7
Dim Offset As Integer
Dim Month As Integer
Dim Year As Integer
Dim ResultDate As Date
' Validate Weekday.
Select Case Weekday
Case _
vbMonday, _
vbTuesday, _
vbWednesday, _
vbThursday, _
vbFriday, _
vbSaturday, _
vbSunday
Case Else
' Zero, none or invalid value for VbDayOfWeek.
Weekday = VBA.Weekday(DateInMonth)
End Select
' Validate Occurence.
If Occurrence <= 0 Then
Occurrence = 1
ElseIf Occurrence > 5 Then
Occurrence = 5
End If
' Start date.
Month = VBA.Month(DateInMonth)
Year = VBA.Year(DateInMonth)
ResultDate = DateSerial(Year, Month, 1)
' Find offset of Weekday from first day of month.
Offset = DaysInWeek * (Occurrence - 1) + (Weekday - VBA.Weekday(ResultDate) + DaysInWeek) Mod DaysInWeek
' Calculate result date.
ResultDate = DateAdd("d", Offset, ResultDate)
If Occurrence = 5 Then
' The latest occurrency of Weekday is requested.
' Check if there really is a fifth occurrence of Weekday in this month.
If VBA.Month(ResultDate) <> Month Then
' There are only four occurrencies of Weekday in this month.
' Return the fourth as the latest.
ResultDate = DateAdd("d", -DaysInWeek, ResultDate)
End If
End If
DateWeekdayInMonth = ResultDate
End Function
Take a look at the WEEKDAY function.
For example you can get the next Sunday for any given date with the following:
=[date] - WEEKDAY([date]) + 7

Creating a calculated member containing the number of years between two dates

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.

Print date in Month name day, year format

I'm looking to print the current date in Month name day, year format (example: October 3, 2014). I understand that I can get the current month in integer format with Month(Date) or Month(Now), but I want to know how I would output the current month in text format.
While waiting for nobody to answer this question I figured out that you can use the MonthName( number, [ abbreviate ] ) function to print the month name. As you can see from my example, the parameters of this function are number, which is a value from 1 to 12 representing the month, and abbreviate, which is an optional boolean. If this parameter is set to TRUE, it means that the month name will be abbreviated. If this parameter is set to FALSE or left out, the month name won't be abbreviated; now lets get back to answering the question.
Since Month(Date) returns the number of the month, you can set it as the first parameter of MonthName(); soMonthName(Month(Date))will return the name of the current month. You can then print the current date in the Month name day, year format with this code:MonthName(Month(Date)) & " " & Day(Date) & ", " & Year(Date)`.

Is it possible to create a dynamically calculated field?

How can I create a dynamically calculated field to determine the age in days based on a date/time stamp?
For example say I have a field that has a date time stamp. What I want to be able to see when browsing the cube is a column showing the number of days since the time stamp. So if the date time stamp is '8/21/13 12:00PM' and I browse the cube on '8/22/13 12:00PM', the calculated column "Number of Days Since" would show 1.
Is this even possible with SSAS?
Yes, you can:
with member Measures.[date string] as
Left(Right([Date].[Date].CurrentMember.UniqueName, 9), 8)
member Measures.[date formatted] as
Left(Measures.[date string], 4) + "-" +
Mid(Measures.[date string], 5, 2) + "-" +
Right(Measures.[date string], 2)
member Measures.[date date] as
CDate( Measures.[date formatted])
member Measures.[to today] as
now() - Measures.[date date]
, format_string = 'yy" years, "m" months, "d" days"'
select {
Measures.[date string],
Measures.[date formatted],
Measures.[date date],
Measures.[to today]
}
on columns,
[Date].[Date].[Date].Members
on rows
from [Adventure Works]
gives what you want. You do of course not need all the intermediate measures, they just show step by step what is calculated.
One remark, however: I formatted the to today measure with a date format using yy. A four digit year would show as "1912 years" etc, as SSAS is internally using the date coding like Excel of number of days since Jan, 1, 1900. I mis-used the date formatting here to format a duration, which is a number and not a real date. This formatting approach is only giving correct results for positive durations less than 100 years. But this is only a formatting issue and the main duration calculation is correct nevertheless. It just shows the duration in days, and the time within the day as fractions.