Max value of a SMA (Simple Mobile Average) measure in selected range - powerbi

I have a measure, an SMA (Simple Mobile Average), and I need to calculate the MAX of it.
It should be simple, but I can't find what am I doing wrong.
In my case I have a table (allDataCSV). Each row represents an event, and it has a DATE. I can calculate the num of events that occur in when filtering the table (just count rows):
Count = COUNTROWS()
And my SMA is calculated this way:
CountSMA5Days = AVERAGEX(
DATESINPERIOD(allDataCSV[Day],LASTDATE(allDataCSV[Day]),-5,DAY),
[Count])
I want to calulate the MAX value of [CountSMA5Days]. It is a measure, not a field, so I cant use MAX. I have tried MAXX, with no luck:
MaxSMA = MAXX(allDataCSV,[CountSMA5Days])
It returns me '1', and I suppose it is because it evals row by row, and, in ths case, [CountSMA5Days] returns an average of 5 'ones'
Could you help me, please?
Thanks!
EDIT:
Thans to #Mik, who guided me to solution. The right meassure is:
MaxSMA = MAXX(
all(allDataCSV[Day])
,[CountSMA5Days]
)
That's it!!!

The problem with your measure can come from CALCULATE() that you get by enclosed measure.
I mean your measure MaxSMA = MAXX(allDataCSV,[CountSMA5Days]) DAX converts to MaxSMA = MAXX(allDataCSV,CALCULATE([CountSMA5Days])). So, all data in rows filters your measure. Try the same trick as you did with AVERAGEX()
MAXX(
DATESINPERIOD(allDataCSV[Day],LASTDATE(allDataCSV[Day]),-5,DAY)
,[CountSMA5Days]
)
I believe that DAX will convert it to
MAXX(
DATESINPERIOD(allDataCSV[Day],LASTDATE(allDataCSV[Day]),-5,DAY)
,CALCULATE(
AVERAGEX( -- your measure
DATESINPERIOD(
allDataCSV[Day]
,LASTDATE(allDataCSV[Day]) --= date from current iteration of MAXX
,-5
,DAY
)
,CALCULATE([Count])
)
,allDataCSV[Day]= date from current iteration
)
)
I didn't check the measure. Hope you'll manage to fix the issue.

Related

How to perform a MIN value Grouped By Date Measure in PowerBI?

I have a measure called PERC that basically spits out a percentage. I am now creating a measure that I can place in the "Y-Axis Start" function for a trend line. I need this new measure to calculate the MIN of PERC grouped by [MonthYear] (a column in my data). How do I perform this? I have tried using the GROUPBY function but keep getting errors no matter how much I play with MIN, MINA and MINX...
My brain wants to do this:
CALCULATE(
MIN([PERC], "*Grouped By*"('Data'[MonthYear])
)
Thank you in advance. I'll be happy to answer any follow up questions for more context.
Try
MinPERCbyMonthYear :=
VAR MyTable =
SUMMARIZE ( Table1, Table1[MonthYear], "PERCbyMonthYear", [PERC] )
RETURN
MINX ( MyTable, [PERCbyMonthYear] )
replacing Table1 as required.

How to calculate average percentage in PowerBI?

Hi everyone,
I'm still new to PowerBI, right now I have a set of data in PowerBI as shown in the screenshot above. I have a Measure to calculate the % of OK:
total_student = COUNT(StudentAns[Name])
ok_% =
VAR OK_COUNT = COUNTROWS(
FILTER(
StudentAns,
StudentAns[Answer] = "OK"
)
)
RETURN (OK_COUNT/StudentAns[total_student])
I created a Matrix to show the % of OK for each month as shown in the screenshot below:
What I want to find is the average percentage for all the months. So the final output answer should be 89.05%, which is the average of 85.95%, 91.4%, 89.27% and 89.58%.
The reason I want to get the average percentage of OK across all the months is because I want to use the output as a Target Goals for KPI visualization.
Any help or advise will be greatly appreciated!
You can add one more measure to the matrix as follows:
ok_2 % =
IF(
HASONEVALUE( 'StudentAns'[Month] ),
[ok_%],
AVERAGEX(
VALUES( StudentAns[Month] ),
[ok_%]
)
)
It calculates your original measure for each month, but for the Totals it returns the average of results of your measure.
HASONEVALUE returns True if there is only one distinct value in the filtered context; VALUES - creates a list of unique values; AVERAGEX - calculates the average of a set of expressions evaluated in each row.

DAX PowerBI: Calculating sum of column based on other column

I am trying to calculate the TotalFTE, but I can't figure it out. I am fairly new to DAX and trying to make the best of it with some studycases.
The table contains a lot of columns, but these are the columns that are needed for the measure.
The measure should sum the highest FTE in a year per ID and ignore the blanks in ID.
I've build a measure that gives an error when I try to visulise the result in a card. My measure: TotalFTE = CALCULATE(MAX(Table[FTE]), FILTER(Table, DISTINCT(Table[ID])))
Any feedback?
Moving forward, please post data as tables in stack and refrain from provide the data as image.
Measure =
SUMX (
GROUPBY (
'Fact',
'Fact'[year],
'Fact'[ID],
"#Max", MAXX ( CURRENTGROUP (), 'Fact'[FTE] )
),
[#Max]
)

DAX Cumulative Window Function

I have the simple dataset as below:
I need to permit summing the DistanceTraveled column in a "Measure" given date filters selected, and date order, to allow a cumulative total. The data model is dead simple as it only have one date dimension:
My DAX for the measure is:
Measure = CALCULATE(SUM(ActivityReport[DistanceTraveled]), FILTER(Timestamp,Timestamp[Timestamp] <= MAX(Timestamp[Timestamp])))
I know I must be missing something simple, how can I create a cumulative total given the filtered and increasing Timestamps for column DistanceTraveled?
I think you forgot to include all dates, Try this..
Measure = CALCULATE(
SUM(ActivityReport[DistanceTraveled]),
FILTER(ALL('Timestamp'[Timestamp]),
Timestamp[Timestamp] <= MAX(Timestamp[Timestamp])
)
)
What I ended up doing:
Measure = CALCULATE(
SUM(ActivityReport[DistanceTraveled]),
FILTER(ALLSELECTED(ActivityReport),
ActivityReport[Timestamp] <= MAX(ActivityReport[Timestamp])
)
)

DAX SUM previous period integer value

This issue seemed to be easy at the first glance but I have been trying to solve it for a while.
enter image description here
I would like to dynamically sum the previous period sales in a power pivot measure. The thing is that my period column is a integer value not a date.
I manage to calculate the previous period but I did not manage to set it up as a filter value:
Max(Table1[Period])-1 --> this gives me the previous value of the period field
However when I want to add this as a filter of a calculated measure it doesn't work: --> Calculate(
Sum(table1[Sales]), Filter(table1,Max(table1[Period])=Max(table1[Period])
)
I tried simply this one as well: Calculate(Sum(table1[Sales]), table1[Period] = table1[Period] -1 )
but neither of them are working. I though that I do it with calculated column however I would rather do it with measure.
Can you please help me?
Expected result:
Create measure:
Previous Sales:=
CALCULATE( SUM(Table1[Sales]),
FILTER( ALL(Table1), Table1[Period] = MAX(Table1[Period]) - 1))
It will give you dynamic previous sales. Please note: it relies on the fact that periods increment by 1.
If you need to summarize Previous Sales, create a second measure:
Total Previous Sales:=
SUMX( VALUES(Table1[Period]), [Previous Sales])