Find min value of member, MDX - max

Just started to explore MDX. Can anyone help me to get below result.
Looking to build a MDX query which gives same out as below SQL query
select max(date),min(date) from Fiscal_calendar
Where :
1. Fiscal_Calendar is dimension in cube
2. date is the attribute

Min Date will be the First Child of the [All] member while Max Date will be the Last Child.
SELECT {Fiscal_Calendar.Date.[All].FirstChild, Fiscal_Calendar.Date.[All].LastChild} ON 1,
{} ON 0
FROM [YourCube]
Second last child:
Fiscal_Calendar.Date.[All].LastChild.Lag(1)

SELECT {
[Dimension - Fiscal Calendar].[Fiscal Quarter].members.item([Dimension - Fiscal Calendar].[Fiscal Quarter].members.count-2)
,[Dimension - Fiscal Calendar].[Fiscal Quarter].[All].firstChild
} ON 1,{} ON 0 FROM [MyCube]
Thanks #SourabA. Above code worked out to me. Do let me know if any better approach

Related

How to calculate total time in hour using DAX?

I have to table, start and end with time as the data, I want to calculate the total hour from start to end.
I tried this but it does not return hour, it returns date and the date also same in the column:
Column = SUM('Data'[Finish Date])-SUM('Data'[Start Date])*24-12
Anyone can give me idea please. Thank you so much
Read the docs: https://learn.microsoft.com/en-us/dax/datediff-function-dax
Column = DATEDIFF('Data'[Start Date], 'Data'[Finish Date], HOUR)

DAX / Power Pivot - Count occurrence substring within string of another table without relation

Dears, i'm stuck in a difficult yet simple task.
I need to count the number of occurence of speficic substrings within a string in a column that as no relation.
Let me give you an example with Emojis :
Table 1:
People
Message
John
You got it 😊
Paul
So Beautifull 😍😍😍
John
😊
Paul
Good luck 😚
Table 2 :
Subgroup
Emoticones
face-smiling
😁
face-smiling
😂
face-smiling
😊
face-smiling
😇
face-affection
😍
face-affection
😚
In a pivot, i would like to be able to do this kind of thing with Puivot :
Subgroup
Total emoji
face-smiling
2
face-affection
4
Person
face-smiling
face-affection
John
2
0
Paul
0
4
I would create a calculated table using GENERATE() to iterate over each Emoticone
Then use ADDCOLUMNS() to iterate over each Messages, and add the count of the current Emoticone in the current Message.
To count the number of occurrences of an icon in a Message, compute the difference of lengths between Message and Message with removed icons (an icon counts for 2 characters)
And finally FILTER() out pairs without match:
EmotiCounts =
VAR cj = GENERATE(Emoticones,
VAR icon = Emoticones[Emoticones]
RETURN
ADDCOLUMNS(
Messages,
"Counts",
(
LEN(Messages[Message])-
LEN(
SUBSTITUTE(
Messages[Message],
icon,
""))
)/2
)
)
RETURN
FILTER( cj, [Counts] > 0 )
That table contains both People and Subgroup needed for your visuals.

Power Query (M language) 50 day moving Average

I have a list of products and would like to get a 50 day simple moving average of its volume using Power Query (M).
The table is sorted by product name and date. I add a custom column and applied the code below.
if [date] >= #date(2018,1,29)
then List.Average(List.Range(Source[Volume],[Volume]-1,-50))
else ""
Since it is already sorted by date and name, an if statement was applied with a date as criteria/filter. However, an error occurs that says
'Volume' column not found in the table.
I expect to have an added column in the power query with volume 50 day moving average per product. the calculation to be done if date is greater than or equal Jan 29, 2018.
We don't know what your columns are, but assuming you have [product], [date] and [volume] in Source, this would average the last 50 days of [volume] for the identical [product] based on each [date], and place in a new column
AvgAmountAdded = Table.AddColumn(Source, "AverageAmount", (i) => List.Average(Table.SelectRows(Source, each ([product] = i[product] and [date]<=i[date] and [date]>=Date.AddDays(i[date],-50)))[volume]), type number)
Finally! found a solution.
First, apply Index by product see this post for further details
Then index again without criteria (index all rows)
Then, apply below code
= Table.AddColumn(#"Previous Step", "Volume SMA(50)", each if [Index_byProduct] >= 50 then List.Average(List.Range(#"Previous Step"[Volume], ([Index_All]-50),50)) else 0),
For large dataset, Table.Buffer function is recommended after index-expand step to improve PQ calculation speed

How to find the maximum value from a column satisfying two or more IF conditions in DAX

I am a newbie to Power BI and DAX.
I have a dataset as attached. I need to find the maximum value for each person for each week. I have written the formula in Excel.
=MAX(IF(A$2:A$32=A2,IF(D$2:D$32=D2,IF(B$2:B$32=1,C$2:C$32))))
How can I convert it to DAX or write the same formula in Power BI? I tried the DAX Code as below, But it did not work(ALLEXCEPT Function expects table).
Weekly Maximum =
CALCULATE ( MAX ( PT[Value] ), ALLEXCEPT ( PT, PT[person], PT[Week],
PT[category] ==1 ) )
Once I calculate this, then I need to calculate the Expected value for each week, that has the maximum value of the previous week * 2.85, as shown in the screenshot. How can I put the previous week's maximum value for this week?
Any corrections/solutions, please?
TIA
The Max Value for Category 1 can be written like this:
= CALCULATE(MAX(PT[Value]),
ALLEXCEPT(PT, PT[Person], PT[Week]),
PT[Category] = 1)
(The Category filter doesn't go inside ALLEXCEPT().)
For your Expected Value column, you can do something similar:
= CALCULATE(2.85 * MAX(PT[Value]),
ALLEXCEPT(PT, PT[Person]),
PT[Category] = 1,
PT[Week] = EARLIER(PT[Week]) - 1)
(The EARLIER function gives you the value for the row you are in. The name refers to the earlier row context.)

MDX: Calculating MONTH COVER (of Stock) in a performant way

this is my dataset:
I want to calculate the "Cover Month". Therefore I have to look for Stock(in this example in january 2016 = 5,000), then have a look for each future month if current stock(january 2016) is bigger than "cum. Sales" of following month. If yes, then remember value = 1. This should be done for each future month. After this step all remembered values should be added, so result is 4 (Cover Month). Stock will be enough for 4 following months.
Next step system should do this for next month - dynamically for each month...
How can I do this in a performant way?
Is this the right way:
Filter([TIME].[Year to Month].currentmember : NULL,
[Measures].[cum Sales] < [Measures].[Stock])
?
Maybe anybody can give me a hint? Or maybe I need another alternative formula to get a subtotal and then do another calculation?
Thanks in advance, Andy
If you just require a 1 or 0 then can things not be simplified:
IIF(
SUM(
{[TIME].[Year to Month].currentmember : NULL},
[Measures].[cum Sales]
)
< ([Measures].[Stock],[TIME].[Year to Month].&[Jan-2016]) //<<amend to the date format used in your cube
,1
,NULL
)

Resources