DAX ALL being overridden by cross highlight - dax

I have a Dates table, which includes a Season column. Seasons run from August 1 to July 31. As part of a more complex measure, I need to know the last selected date in the date slicer. The measure is run per season, so to get this date I am using
MaxSelectedDate =
CALCULATE ( MAX ( Dates[Date] ), ALL( Dates[Season] ) )
This works fine, if the slicer date range is set to end at 31 May 22, the measure returns this date for all seasons. However, the visual featuring the measure can be cross-highlighted by another season-based visual. If you choose say, the 2018/19 season, the measure calcs as 31 July 19, the max date for the chosen season. It seems to ignore the ALL( Dates[Season] ) filter removal. How can I stop it doing this?

Related

How do control the start date in DATESYTD and DATESQTD (for example: whether it is calendar year, ISO year and fiscal year)?

Here are my DAX formula's for YTD and QTD calculations.
Sales YTD := CALCULATE( [Total Sales], DATESYTD( 'Date'[Date] ) )
Sales QTD := CALCULATE( [Total Sales], DATESQTD( 'Date'[Date] ) )
The date table has dates from 01-01-2020 to 31-12-2040
What is the logic used by the DATESYTD and DATESQTD to derive the 1st date of the year (DATESYTD) and first date of the quarter (DATESQTD)? For example - How do I make it use the start date based on either of the following: calendar year, ISO year and fiscal year ?
For the DATESYTD, you can control the start of the year, by setting the year end date. The default with out this option will be the normal calendar year.
DATESYTD('Date'[Date]', "03-31")
This will start the YTD calculation from the "01-04" (Normal UK tax year)
For variations between Calendar, ISO and Fiscal years, you'll have to create individual measures, a switch based on a selection, or use a calculation group to drive your YTD measures.
DATESQTD doesn't have the same option. You'll need a well-formed calendar table for the other 'To Date' options.
Update: for week related start/end year, best to check out the dax patterns approach

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.

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

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: 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