Forum Discussion
Count same values in most recent date
Hi everyone,
I want to create a formula which count how many times a specific value shows up in the most recent date.
I made a sample set (see below). I want to know how many times '-1' shows up in the latest month (March). So the answer should be 3.
| Date | Aspect | Value |
| January 2020 | Responsibility | 1 |
| January 2020 | Validity | 0 |
| January 2020 | Satefy | -1 |
| January 2020 | Guarantee | -1 |
| February 2020 | Responsibility | 0 |
| February 2020 | Validity | -1 |
| February 2020 | Satefy | 0 |
| February 2020 | Guarantee | 0 |
| March 2020 | Responsibility | -1 |
| March 2020 | Validity | -1 |
| March 2020 | Satefy | -1 |
| March 2020 | Guarantee | 1 |
- Anonymous6 years ago
It depends on how you want to visualize it, if you just want to hard coded like this, or maybe you want a matrix, so you will have dimTable to show all the Value, then you get the CurValue=SELECTEDVALUE(dimTable[Value]), or you have a Date Table, then you need to take care the relationship as well.
CountValue=
VAR MaxMonth = MONTH(MAX(yourTable[Date]))
VAR CurValue = -1
RETURN
COUNTROWS(FILTER(yourTable,MONTH(yourTable[Date])=MaxMonth && yourTable[Value] = CurValue)) - Anonymous6 years ago
If you just want 9, do this, but you need to take care of your date format
CountValue1=
VAR MaxYear = YEAR(MAX(yourTable[Date]))
VAR MaxMonth = MONTH(MAX(yourTable[Date]))
VAR CurValue = -1
RETURN
SUMX(
GROUPBY(yourTable,[Location]),
COUNTROWS(FILTER(yourTable,Year(yourTable[Date])=MaxYear && MONTH(yourTable[Date])=MaxMonth && yourTable[Value] = CurValue)))
11 Replies
- AnonymousNot applicable
It depends on how you want to visualize it, if you just want to hard coded like this, or maybe you want a matrix, so you will have dimTable to show all the Value, then you get the CurValue=SELECTEDVALUE(dimTable[Value]), or you have a Date Table, then you need to take care the relationship as well.
CountValue=
VAR MaxMonth = MONTH(MAX(yourTable[Date]))
VAR CurValue = -1
RETURN
COUNTROWS(FILTER(yourTable,MONTH(yourTable[Date])=MaxMonth && yourTable[Value] = CurValue))- AnonymousNot applicable
Hi Anonymous and amitchandak ,
Thank you both for your help.
The formula of you did work, Vera. My orginal dataset contains data over several years. So I discovered that your formula counts values of e.g. December 2017 when the latest date actually December 2019 was. To work around that, the formula will be as follows:
CountValue=
VAR MaxYear = Year(MAX(yourTable[Date]))
VAR MaxMonth = MONTH(MAX(yourTable[Date]))
VAR CurValue = -1
RETURN
COUNTROWS(FILTER(yourTable,Year(yourTable[Date])=MaxYear && MONTH(yourTable[Date])=MaxMonth && yourTable[Value] = CurValue)) - AnonymousNot applicable
What if you want to make a distinction on 'Location', so that the formula has to return all values with '-1' in the latest month and year for each individual location. So, the latest month and year for The Netherlands is March 2020, for Germany March 2019 and for Spain Oktober 2019. I copied/paste the values, so formula should return 9.
Date Aspect Value Location 1 januari 2020 Responsibility 1 The Netherlands 1 januari 2020 Validity 0 The Netherlands 1 januari 2020 Satefy -1 The Netherlands 1 januari 2020 Guarantee -1 The Netherlands 1 februari 2020 Responsibility 0 The Netherlands 1 februari 2020 Validity -1 The Netherlands 1 februari 2020 Satefy 0 The Netherlands 1 februari 2020 Guarantee 0 The Netherlands 1 maart 2020 Responsibility -1 The Netherlands 1 maart 2020 Validity -1 The Netherlands 1 maart 2020 Satefy -1 The Netherlands 1 maart 2020 Guarantee 1 The Netherlands 1 januari 2019 Responsibility 1 Germany 1 januari 2019 Validity 0 Germany 1 januari 2019 Satefy -1 Germany 1 januari 2019 Guarantee -1 Germany 1 februari 2019 Responsibility 0 Germany 1 februari 2019 Validity -1 Germany 1 februari 2019 Satefy 0 Germany 1 februari 2019 Guarantee 0 Germany 1 maart 2019 Responsibility -1 Germany 1 maart 2019 Validity -1 Germany 1 maart 2019 Satefy -1 Germany 1 maart 2019 Guarantee 1 Germany 1 mei 2019 Responsibility 1 Spain 1 mei 2019 Validity 0 Spain 1 mei 2019 Satefy -1 Spain 1 mei 2019 Guarantee -1 Spain 1 juni 2019 Responsibility 0 Spain 1 juni 2019 Validity -1 Spain 1 juni 2019 Satefy 0 Spain 1 juni 2019 Guarantee 0 Spain 1 oktober 2019 Responsibility -1 Spain 1 oktober 2019 Validity -1 Spain 1 oktober 2019 Satefy -1 Spain 1 oktober 2019 Guarantee 1 Spain - AnonymousNot applicable
If you just want 9, do this, but you need to take care of your date format
CountValue1=
VAR MaxYear = YEAR(MAX(yourTable[Date]))
VAR MaxMonth = MONTH(MAX(yourTable[Date]))
VAR CurValue = -1
RETURN
SUMX(
GROUPBY(yourTable,[Location]),
COUNTROWS(FILTER(yourTable,Year(yourTable[Date])=MaxYear && MONTH(yourTable[Date])=MaxMonth && yourTable[Value] = CurValue)))
- amitchandakSuper User
measure =
var _sel = -1
return
calculate(count(table[Value]),Table[date] = format(today(),"MMMM YYYY"),table[value] =_sel)