Forum Discussion
Count with filtering different date columns before and after current year
- 1 year ago
Anonymous , Try using
DAX
ActiveStoresAtStartOfYear =
VAR SelectedYear = SELECTEDVALUE(DateTable[Year])
RETURN
CALCULATE(
COUNTROWS(
FILTER(
'StoreTable',
'StoreTable'[OpeningDate] < DATE(SelectedYear, 1, 1) &&
(
ISBLANK('StoreTable'[ClosingDate]) ||
'StoreTable'[ClosingDate] >= DATE(SelectedYear, 1, 1)
)
)
),
REMOVEFILTERS(DateTable)
)
bhanu_gautam Thank you so much! Worked great
Follow-up question,
In conjunction with this measure, I then have two other tables for openings and closings which are more updated than the one used above.
Which I then use to visualise active + opening - closings,
which I then want to carry over onto the next month, for both the total and the active.
Like this:
| January | February | March | April | May | June | |
| Active | 199 | 198 | 200 | 200 | 199 | 199 |
| Opened | 2 | 1 | 1 | |||
| Closed | -1 | -2 | ||||
| Total | 198 | 200 | 200 | 199 | 199 | 200 |
And the measure I have worked great for this, until I changed it just now.
Now the active doesn't seem to properly get the previous end of year count
For example, let's say the table above is 2024 and december insteaf of june, then if I filter 2025 I want january to be 200
I've used following measures for active (based on the one you just helped me with, and another one for total.
Running Active =
VAR _SelectedYear = SELECTEDVALUE('DateTable'[Year])
VAR _CurrentMonth = MAX('DateTable'[Month])
VAR _PreviousMonth = IF(_CurrentMonth = 1, 12, _CurrentMonth - 1)
VAR _PreviousYear = IF(_CurrentMonth = 1, _SelectedYear - 1, _SelectedYear)
VAR PreviousActive =
CALCULATE(
[Active],
FILTER(
ALL('DateTable'),
'DateTable'[Month] = _PreviousMonth &&
'DateTable'[Year] = _PreviousYear
)
)
RETURN
PreviousActive +
CALCULATE(
[Openings] + [Closings (negative)],
FILTER(
ALL('DateTable'),
'DateTable'[Month] < _CurrentMonth &&
'DateTable'[Year] = _SelectedYear
)
)
Running Total =
VAR _SelectedYear = SELECTEDVALUE('DateTable'[Year])
VAR _CurrentMonth = MAX('DateTable'[Month])
VAR _PreviousMonth = IF(_CurrentMonth = 1, 12, _CurrentMonth - 1)
VAR _PreviousYear = IF(_CurrentMonth = 1, _SelectedYear - 1, _SelectedYear)
RETURN
CALCULATE(
[Active] +
//[Running Active] +
SUMX(
FILTER(
ALL('DateTable'),
'DateTable'[Month] <= _CurrentMonth &&
'DateTable'[Year] = _SelectedYear
),
[Openings] + [Closings (negative)]
)
)