DAX: How to count based on dynamic time period? - time

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.

Related

How MAX(Calendar[date]) -as a filter in CALCULATE- evaluates to the current context date when calculating running total?

This Article by Alberto Ferrari shows how to compute the running total over a dimension:
Sales RT :=
VAR MaxDate = MAX ( 'Date'[Date] ) -- Saves the last visible date
RETURN
CALCULATE (
[Sales Amount], -- Computes sales amount
'Date'[Date] <= MaxDate, -- Where date is before the last visible date
ALL ( Date ) -- Removes any other filters from Date
)
In this video
he says: "Maxdate contains the last date that is visible in the context"
Where did the context come from when we are using ALL(Date) which cancels all filters and returns the entire Date table?
Shouldn't Max(Date) be always equal to the Maximum date of the entire Date table? not the one corresponding to the row being calculated.
What is meant by [visible context] hear? how is it defined? And/or how is it treated by CALCULATE when ALL is used?
If anyone please could explain to me -in depth- how the [MAX ( 'Date'[Date] )] evaluated to the date corresponding to the row being calculated and not to the Maximum date of the entire Date table
Thanks.

Using SUMMARIZE to Count Values for a Range of Dates

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

Power BI measure with filter affecting numerator and denominator differently

Building an accounting report, part of which is a ratio called "Receivables Turnover". It's calculated as "Sales" / "Trade Receivables". The calculation component of this is straight-forward (using CALCULATE), but I'm not able to wrap my head around how to filter this by date. Here's why:
The numerator - "Sales" - is filtered for the exact date i.e. "Get me the sum of sales in December 2018". However, the denominator of this calculation is filtered until the date i.e. "Get me the sum of trade receivables until December 2018".
So when I add a filter to the visual, it can be either filtered for the exact date OR filtered to be "on or before". How can I make it so that the numerator and denominator are impacted differently? Is this possible? If not, is there a way to workaround this?
I'm guessing each row of the receivable table contains a positive number for a debit or a negative number for a credit. In that case, the balance at a given point of time would be defined like below.
Receivable Balance =
CALCULATE (
SUM ( Receivable[Amount] ),
REMOVEFILTERS ( 'Calendar' ),
'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
)
For example, if you drill-down the calendar to December 2018, this measure will show the total of all records on or before the end of December 2018.
Then you can use this measure to define the turnover ratio.
Receivables Turnover = DIVIDE ( [Sales], [Receivable Balance] )

Issues on to calculate difference between previous quarter and current quarter

I am with the issue of difference between previous quarter, next quarter and current quarter. I have created date field and use the following calculation to resolve the issue but there is showing aggregated 4 quarters total instead of difference between last quarter and current quarter. The attached screenshot showing the grand total figure and quarter difference figure is same.
But the result is same. Only showing the total figure to sum all quarters. Please help me to find out the way to resolve the issue.
=IF (
FIRSTNONBLANK (
'10QuarterlyReportProgressPC'[Quarter], 1 ) = "Q1",
SUM ('10QuarterlyReportProgressPC'[Progress] ),
SUM( '10QuarterlyReportProgressPC'[Progress] )
- CALCULATE ( SUM ( '10QuarterlyReportProgressPC'[Progress]
),
PREVIOUSQUARTER ( '10QuarterlyReportProgressPC'[Start_QuarterYear] ) )
)

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

Resources