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)
)
Follow up questions:
In combination with this measure, in a matrix with 1 year and the 12 months, I'm showing the active, openings and closings (which I get from other tables)
I then need the active and total to carry over the result of adding/removing closings, so it looks something 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 |
I've managed to get that calculation working almost perfectly, with the following:
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)]
)
)
But now I have an issue with the year break,
for example if I have the total as 197 on year-end 2024, when I select 2025 the active for january 2025 it hasn't retained 197 and is instead something else.
Any suggestions on how to keep that?
The running total is somehow correct, but not the running actual