DAX Cumulative Window Function - powerbi

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])
)
)

Related

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

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.

Power BI Calculate distinct impacting when I use filter

I made a measure that is as follows:
wo = CALCULATE(
DISTINCTCOUNT('Table1'[won]),
ALLEXCEPT(Table1, 'Table1'[flag]),
ALLEXCEPT(Calendar,Calendar[End of Week]),
FILTER(Table1,[flag]="Y")
)
I want the total amount of items in 'won' column with the flag = 'Y'. But when I use one date range filter [End of Week] I had problem because my total amount keeps changing and shouldn't change, I want the total of my entire table regardless of date.
This measure works without 'Y' flag, but it's not what I need:
wo= CALCULATE(
DISTINCTCOUNT('Table1'[wo]),
ALL(Calendar)
)
Could you please help me how to adjust this measure?
Solved using this DAX CODE:
wo= CALCULATE(
DISTINCTCOUNT('Table1'[won]),
FILTER(ALL(Table1),[flag]="Y")
)

DAX efficient measure to get max date for max date and blanks for other dates

I would like to get the latest date of FactTable column in both ways (1) repeated all across table and (2) just in the max date.
The first:
DateMax Repeated Measure = CALCULATE(MAX(FactTable[Date]), ALLSELECTED('FactTable'))
The second:
DateMax Only Measure =
VAR GetMaxDate = CALCULATE( MAX(FactTable[Date]), ALLSELECTED('FactTable'))
return
CALCULATE( MAX(FactTable[Date]), FILTER('Calendar', 'Calendar'[Date]=GetMaxDate))
The second measure (as desired) returns all blanks except the single date where date is max. Is there no efficient way then I did it?
You shouldn't need to use CALCULATE a second time. You should just be able to do something like
IF(MAX('Calendar'[Date]) = GetMaxDate, GetMaxDate, BLANK())

Ranking a Measure Value (as opposed to a column value) in DAX?

Short version of question: how can I create a rank based on a calculated measure?
Question background:
I have a table/query that yields many rows for each employee, with each row having a time taken value.
I'd like to calculate the average time taken for each employee, and then present a table with a row for each employee, showing their name, average time taken, and rank (based on time taken).
To do the first part, I created a measure called AverageTimeLength and set it equal to:
AverageTimeLength = Average(Table_name[Column_name])
Then I coded the following:
AverageTimeLength_employee = CALCULATE([AverageTimeLength], GROUPBY(Table_name, Table_name[EmployeeName]))
These two are measures; I was able to insert the second into the new table chart I'm creating, but unfortunately, I can't use RANKX() on it because the measure values don't come from a column. If I try to create a column derived from the measure (i.e., column_name = [AverageTimeLength_employee]) I just get an error accusing column_name of circular reasoning.
What I want to do seems like it should be simple; does anyone know how I can create a simple rank parameter, to rank the measure values?
You can create the Average measure and use it in the Rank measure as follows:
Average = AVERAGE([Time taken])
Rank = IF (
HASONEVALUE ( Table_Name[Name] ),
RANKX ( ALL ( Table_Name[Name] ), [Average])
)
Hope it helps.

Define the granularity for "day" when calculating NumOfDays?

When calculating the average sales per day, I have the following measure for NumOfDays:
NumOfDays = IF (
[Sales Amount] > 0;
COUNTROWS ( Date )
)
What this is doing is removing the number of days for those dates when there are no sales.
Thus, I have the following visual:
As you can see, the total is wrong.
This is due to the fact that the database has more years than those shown in the matrix.
How can I define the granularity for "day" when calculating NumOfDays?
That is, how can I count the rows for those days with sales only?
Thanks!
I recommend two things:
Use CALCULATE() instead of IF, which is the proper DAX way of defining measures with filters
Use DISTINCTCOUNT() instead of COUNTROWS(), to ensure you never run into double counting (in case your data ever becomes more granular)
This calculation may work - but you may need to specify the actual date column inside your Date table.
NumOfDays = CALCULATE(DISTINCTCOUNT(Date[Date]),FILTER(SalesTable,[Sales Amount] > 0))
If there is more to the problem, let me know what exactly you are expecting to see in your matrix.