Forum Discussion
Filter on a Measure Applied to another Measure
I am trying to gather data for the last production work day. To do this I have a field in the date table called WorkDay. I am have a measure (ReportingDate) that calculates the max date in the date table where workday is true and it's less than today, my formula is below. For reference 'Date' is the date table and the field [Date] is the short format date
The problem is in how you are specifying the filters. By using FILTER over the entire date table you are calculating [Reporting Date] for every row of the date table. Due to context transition, the row context generated by the iteration is translated into a filter context which then affects the calculation of [Reporting Date].
You need to calculate [Reporting Date] only once, in the current filter context, and then apply that as a filter.
MinMonthDate = VAR ReportingDate = [ReportingDate] VAR Result = CALCULATE ( MIN ( 'Date'[Date] ), 'Date'[Year] = YEAR ( ReportingDate ), 'Date'[MonthNumber] = MONTH ( ReportingDate ) ) RETURN Result
4 Replies
- johnt75Super User
I find it useful to have a start of month column in the date table. You could create a calculated column in DAX like
Start of month = EOMONTH ( 'Date'[Date], -1 ) + 1You could then write the MinMonthDate measure like
MinMonthDate = CALCULATE ( MAX ( 'Date'[Start of month] ), 'Date'[Date] < TODAY (), 'Date'[Workday] = TRUE ) - schae235New Member
johnt75 thank you for your reply and I believe that could be a solution for the first of the month. I was hoping for a reason why the measure is showing the behavior it is. I have other measures that calcualte off that one other than just he first of the month. For example I also have the first of the week, etc. I guess a work around would be to add all the different values to the table but I just see that is ineffective. It would be nice to have it calculated.
- johnt75Super User
The problem is in how you are specifying the filters. By using FILTER over the entire date table you are calculating [Reporting Date] for every row of the date table. Due to context transition, the row context generated by the iteration is translated into a filter context which then affects the calculation of [Reporting Date].
You need to calculate [Reporting Date] only once, in the current filter context, and then apply that as a filter.
MinMonthDate = VAR ReportingDate = [ReportingDate] VAR Result = CALCULATE ( MIN ( 'Date'[Date] ), 'Date'[Year] = YEAR ( ReportingDate ), 'Date'[MonthNumber] = MONTH ( ReportingDate ) ) RETURN Result- schae235New Member
Thank you. I wish that didn't mean that I need to redo all the measures that are dependent on the reporting date but at least I have a path forward.