Using SUMMARIZE to Count Values for a Range of Dates - dax

I am trying to use SUMMARIZE to generate a table containing a list of people (represented by Phishing[Zid]) and the number of times where the person had a "Phishing Outcome" of "Fail" for a 12 month period. The 12 month period for each person will start on the date represented by Calendar[Month Index] and then I want to count the failures between that month index and month index - 11.
For example, assume the current row in the Phishing table has a Zid of wzqc51 and Calendar[Month Index] = 27. In the CALCULATE function I want to count the number of fails for month index between 16 and 27. It's the piece in angle brackets below that I cannot get right.
Also, there is a 1:M between Calendar and Phishing and the Calendar table is not a traditional daily calendar. It is a monthly calendar where month index of 1 is the first year and month of data and month index of say 27 is the 27th year and month of data we have.
SUMMARIZE(
Phishing,
Phishing[Zid],
Calendar[Month Index],
"Count", CALCULATE(
COUNT(Phishing[Zid]),
< I want to calculate a count between the current month index and month index - 11 >
Phishing2[Phishing Outcome]= "Fail"
)
)
The output should look like this:
Zid Month Index Count
QZ4TIN 27 4
This would mean that the user with a Zid of QZ4TIN failed four times between month index 16 and 27.

It's a bit tricky to write DAX without having data to test against, but I suspect you want something like
FILTER (
ALL ( Calendar ),
Calendar[Month Index] <= EARLIER ( Calendar[Month Index] ) &&
Calendar[Month Index] >= EARLIER ( Calendar[Month Index] ) - 11
)
Where EARLIER refers to the earlier row context (from the SUMMARIZE rather than the FILTER), not anything to do with dates or time.
You may wish to create a variable before the CALCULATE rather than using EARLIER
SUMMARIZE (
Phishing,
Phishing[Zid],
Calendar[Month Index],
"Count",
VAR CurrMonth = Calendar[Month Index]
RETURN
CALCULATE (
COUNT ( Phishing[Zid] ),
FILTER (
ALL ( Calendar ),
Calendar[Month Index] <= CurrMonth &&
Calendar[Month Index] >= CurrMonth - 11
),
Phishing2[Phishing Outcome] = "Fail"
)
)

Related

DAX Measure to Calculate Total Yearly Forecast by Snapshot Period

I want to recreate a sumifs dax measure (not calculated column) to Sum Yearly Totals for each snapshot period (less than or equal to selected slicer Month End) by (forecast)month and Productcategory
In Excel this would be accomplished with the following sumifs function =SUMIFS($D$2:$D$97,$A$2:$A$97,B2,$C$2:$C$97,C2)
That is sum amount if reporting period equals value of month in current table row and category equals value of category in current table row
Table fields:
Snapshot Date (range from 31JAN2020 to 31DEC2020 by month end)
Forecast for Month (range from 31JAN2020 to 31DEC2020 by month end)
Sub Category (bikes, coats)
Amount (number actual sales mtd or projected count of sales)
Type (Actual or projection) actual sales amount to date, or projected for each month to end of year
Snapshot Date,Forecast for Month,Sub Category,Amount,Type,Expected Result fo 31MAR2020
1/31/2020,1/31/2020,Bikes,3,Actual,133
1/31/2020,2/29/2020,Bikes,7,Projection,138
1/31/2020,3/31/2020,Bikes,11,Projection,0
1/31/2020,4/30/2020,Bikes,20,Projection,0
1/31/2020,5/31/2020,Bikes,10,Projection,0
1/31/2020,6/30/2020,Bikes,11,Projection,0
1/31/2020,7/31/2020,Bikes,20,Projection,0
1/31/2020,8/31/2020,Bikes,20,Projection,0
1/31/2020,9/30/2020,Bikes,2,Projection,0
1/31/2020,10/31/2020,Bikes,9,Projection,0
1/31/2020,11/30/2020,Bikes,18,Projection,0
1/31/2020,12/31/2020,Bikes,2,Projection,0
1/31/2020,1/31/2020,Coats,1,Actual,111
1/31/2020,2/29/2020,Coats,8,Projection,136
1/31/2020,3/31/2020,Coats,10,Projection,0
1/31/2020,4/30/2020,Coats,17,Projection,0
1/31/2020,5/31/2020,Coats,12,Projection,0
1/31/2020,6/30/2020,Coats,1,Projection,0
1/31/2020,7/31/2020,Coats,11,Projection,0
1/31/2020,8/31/2020,Coats,4,Projection,0
1/31/2020,9/30/2020,Coats,16,Projection,0
1/31/2020,10/31/2020,Coats,10,Projection,0
1/31/2020,11/30/2020,Coats,10,Projection,0
1/31/2020,12/31/2020,Coats,11,Projection,0
2/29/2020,1/31/2020,Bikes,3,Actual,133
2/29/2020,2/29/2020,Bikes,17,Actual,138
2/29/2020,3/31/2020,Bikes,7,Projection,0
2/29/2020,4/30/2020,Bikes,17,Projection,0
2/29/2020,5/31/2020,Bikes,11,Projection,0
2/29/2020,6/30/2020,Bikes,11,Projection,0
2/29/2020,7/31/2020,Bikes,18,Projection,0
2/29/2020,8/31/2020,Bikes,17,Projection,0
2/29/2020,9/30/2020,Bikes,3,Projection,0
2/29/2020,10/31/2020,Bikes,13,Projection,0
2/29/2020,11/30/2020,Bikes,9,Projection,0
2/29/2020,12/31/2020,Bikes,12,Projection,0
2/29/2020,1/31/2020,Coats,15,Actual,111
2/29/2020,2/29/2020,Coats,7,Actual,136
2/29/2020,3/31/2020,Coats,15,Projection,0
2/29/2020,4/30/2020,Coats,11,Projection,0
2/29/2020,5/31/2020,Coats,1,Projection,0
2/29/2020,6/30/2020,Coats,12,Projection,0
2/29/2020,7/31/2020,Coats,9,Projection,0
2/29/2020,8/31/2020,Coats,13,Projection,0
2/29/2020,9/30/2020,Coats,2,Projection,0
2/29/2020,10/31/2020,Coats,16,Projection,0
2/29/2020,11/30/2020,Coats,19,Projection,0
2/29/2020,12/31/2020,Coats,16,Projection,0
Any help would be appreciated
This is a possible measure that emulates the sumifs expression.
It work at the level of the single row and returns BLANK() if more than one row is in the current filter context
Result =
VAR SelectedMonth =
SELECTEDVALUE ( Snapshot[Forecast for Month] )
VAR SelectedSubCategory =
SELECTEDVALUE ( Snapshot[Sub Category] )
RETURN
IF (
NOT ISBLANK ( SelectedMonth )
&& NOT ISBLANK ( SelectedSubCategory ),
CALCULATE (
SUM ( Snapshot[Amount] ),
REMOVEFILTERS ( Snapshot ),
Snapshot[Snapshot Date] = SelectedMonth,
Snapshot[Sub Category] = SelectedSubCategory
) + 0
)

Dax calculated column for holding max value grouped by date

I have a table that holds ten days of values broken out by hour:
date hour sales
11/20/2019 1 10
11/20/2019 2 20
11/20/2019 3 30
...
11/20/2019 23 230
this pattern is repeated for each date until the current date where a row is inserted on the hour with latest sales
I would like to take the last sales amount for each date and divide the prior rows by that value using DAX. I am first trying to create a calculated column to just hold the max sales value, but for some reason I am getting the max value for the table instead of the group:
Estimated[SalesPct] =
var maxSales = MAXX('Estimated'[Exp_Sales])
return
CALCULATE(divide('Estimated'[Exp_Sales], maxSales)
For calculated column following DAX will do:
Estimated[SalesPct] =
VAR maxSales =
CALCULATE (
MAX ( 'Estimated'[Exp_Sales] ),
ALLEXCEPT ( 'Estimated', 'Estimated'[DateColumn] ) //change to your date column
)
RETURN
DIVIDE ( 'Estimated'[Exp_Sales], maxSales )
Thank

DAX - Calculate average between last day of past month and last day of current month

I'm working on Excel and Power Pivot and I'm trying to get the average sales between the last day of the past month and the last day of the current month, but I'm unable to get the right expression for it.
I have the "Credits" table and the "Calendar" table, both linked by the "Date" field.
So, this is what I have:
=CALCULATE ( AVERAGE(Credits[Sales] );
FILTER ( Calendar ;
Calendar[DateNum] >= VALUE(FORMAT(STARTOFMONTH(Calendar[Date])-1;"YYYYMMDD"))
&&
Calendar[DateNum] <= VALUE(FORMAT(ENDOFMONTH(Calendar[Date]);"YYYYMMDD"))))
I use that measure in a dynamic table along with the "Month" dimension, but it only shows the average for the full month, not taking into account the filters I'm trying to apply so that it also takes the last day from the previous month.
I think what's happening is that the month is still in your filter context.
Try FILTER( ALL(Calendar) ; instead.
I think you could probably also simplify your measure a bit. Maybe try something along these lines:
CALCULATE(
AVERAGE( Credits[Sales] );
DATESBETWEEN(
Calendar[Date];
STARTOFMONTH( Calendar[Date] ) - 1;
ENDOFMONTH( Calendar[Date] )
)
)

DAX measure with TOTALMTD running slow

I have two measures in my tabular cube.
The first one called
'Number of Days' := CALCULATE(COUNTROWS(SUMMARIZE('A'[Date])))
The second one will includes the first one as its expression
'Number of Days (MTD)' := CALCULATE(TOTALMTD([Number of Days],'A'[Date]))
The second measure when I browse the cube and pull out the measure.
It runs incredibly slow.
Any idea how I can optimize these measurements and make it run faster?
Sample Data
Volume:= SUMX(A, DIVIDE([Volume],2))
Volume (MTD):= TOTALMTD([Volume],'A'[Date])
Updated extra measurements
The best practice should be creating a Calendar/Date table and use TOTALMTD Time Intelligence function. However this approach can be used if your model doesn't include a Date table.
First measure, number of days:
Num of Days := DISTINCTCOUNT(A[Date])
Cumulative measure:
Num of days (MTD) :=
CALCULATE (
[Num of Days],
FILTER (
ALL ( A ),
[Date] <= MAX ( A[Date] )
&& MONTH ( [Date] ) = MONTH ( MAX ( [Date] ) )
&& YEAR ( [Date] ) = YEAR ( MAX ( [Date] ) )
)
)
UPDATE: Added screenshot.
UPDATE 2: It seems you need to calculate a cumulative total, in that case just use the below expression for the second measure:
Num of days (MTD) :=
CALCULATE ( [Num of Days], FILTER ( ALL ( A ), [Date] <= MAX ( A[Date] ) ) )
UPDATE 3: Usuing SUMX and DISTINCT to count distinct dates.
Replace the first measure by the following:
Num of Days = SUMX(DISTINCT(A[Date]), 1)
This solution could be more performant than use COUNTROWS + SUMMARIZE,
however it could be very slow depending on the number of rows and the
machine where it is running.
Let me know if this helps.

DAX: How to count based on dynamic time period?

I have a data set as presented in the Sample 1 image below, and I need to generate a ratio of events per Serial Number (summary table as shown in the image).
The curve ball I got into here is the fact that the count of events ad sub events is monthly and the count of serial numbers is every 3 months, therefore the SN count needs to move the time period dynamically. In other words in March I need to divide the events from March by the count of SN's from January to March, while on April i need to divide the events from April by the count of SN's from February to April.
Any idea on how to do this?
Thanks in advance for all the help
You will have to create two calculated columns in your table to get the month number and the month from the distinct count should be calculated.
MonthNumber =
SWITCH(
[Event Month],"Jan",1,"Feb",2,"Mar",3,"Apr",4,"May",5,"Jun",6,
"Jul",7,"Ago",8,"Sep",9,"Oct",10,"Nov",11,"Dec",12)
MonthFrom =
SWITCH([Event Month],"Jan",1,"Feb",1,"Mar",1,"Apr",2,"May",3,"Jun",4,"Jul",5,"Ago",6,
"Sep",7,"Oct",8,"Nov",9,"Dec",10)
Then you can create this measure that will give the SN distinct count of the last three months:
SN Count =
CALCULATE (
DISTINCTCOUNT ( Event[SN] ),
FILTER (
ALL ( Event ),
Event[MonthNumber] >= MAX ( [MonthFrom] )
&& [MonthNumber] <= MAX ( [MonthNumber] )
)
)
Let me know if this helps.

Resources