Forum Discussion
Row numbers with conditions
Hi everyone!
im trying to get the row number over partition with a measure with DAX, it need to be like that due the user needs to change the filter dinamicaly.
this si my current measure:
so we are looking for an historical information and the user wants to know th laste status by each partion of empno and id_date. The problem is that in this example the filter is set to 12/7/2023 10:30 AM and with an specific partion on of the records is after that datetime and its showing this:
| MASTID_EMPNO | ID_PRGDATE | datetime | RN |
| 428711 | 20231207 | 11/8/2023 13:31 | 2 |
| 428711 | 20231207 | 12/7/2023 11:30 | 1 |
as the datetime of the second row is after the filtered day, that one shoudlnt be include in the rownumber. the expected output should look something like this:
| MASTID_EMPNO | ID_PRGDATE | datetime | RN |
| 428711 | 20231207 | 11/8/2023 13:31 | 1 |
| 428711 | 20231207 | 12/7/2023 11:30 |
this is the sample data you can use and the expected output:
| MASTID_EMPNO | ID_PRGDATE | datetime | Expected RN |
| 414162 | 20231207 | 11/8/2023 19:45 | 1 |
| 414162 | 20231207 | 11/8/2023 9:41 | 2 |
| 423657 | 20231207 | 11/8/2023 19:44 | 1 |
| 423657 | 20231207 | 11/8/2023 9:41 | 2 |
| 426989 | 20231207 | 11/8/2023 13:31 | 2 |
| 426989 | 20231207 | 12/6/2023 11:34 | 1 |
| 427260 | 20231207 | 11/8/2023 13:31 | 1 |
| 427372 | 20231207 | 11/29/2023 8:42 | 1 |
| 427372 | 20231207 | 11/8/2023 13:31 | 2 |
| 427474 | 20231207 | 11/8/2023 13:31 | 1 |
| 427589 | 20231207 | 11/8/2023 13:31 | 1 |
| 428203 | 20231207 | 11/8/2023 13:31 | 1 |
| 428711 | 20231207 | 11/8/2023 13:31 | 1 |
| 428711 | 20231207 | 12/7/2023 11:30 | |
| 429494 | 20231207 | 11/8/2023 15:28 | 3 |
| 429494 | 20231207 | 11/10/2023 11:37 | 2 |
| 429494 | 20231207 | 12/6/2023 12:00 | 1 |
| 429798 | 20231207 | 11/8/2023 13:31 | 2 |
| 429798 | 20231207 | 12/6/2023 11:44 | 1 |
| 430220 | 20231207 | 11/16/2023 8:04 | 1 |
| 430220 | 20231207 | 11/8/2023 13:31 | 2 |
| 431150 | 20231207 | 11/8/2023 13:31 | 1 |
The final idea is with another measure count just the ones with a 1 in the RN to selected the latest version at the selected datetime filter. If you have a better solution is welcome too.
Hi, DylanVelo
You can try the following methods.
Measure = Var _N1=CALCULATE ( COUNT ( 'Table'[MASTID_EMPNO] ), FILTER ( ALL ( 'Table' ), [MASTID_EMPNO] = SELECTEDVALUE ( 'Table'[MASTID_EMPNO] ) && [datetime] >= SELECTEDVALUE ( 'Table'[datetime] ) && [datetime] <= SELECTEDVALUE ( Slicer[Slicer] ) ) ) + 0 Var _N2=CALCULATE ( COUNT ( 'Table'[MASTID_EMPNO] ), FILTER ( ALL ( 'Table' ), [MASTID_EMPNO] = SELECTEDVALUE ( 'Table'[MASTID_EMPNO] ) && [datetime] >= SELECTEDVALUE ( 'Table'[datetime] ) ) ) Return IF(SELECTEDVALUE(Slicer[Slicer])=BLANK(),_N2,_N1)Is this the result you expect? Please see the attached document.
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
2 Replies
- amitchandak
Super User
DylanVelo , seem like you need latest row, use this approch
Last Qty = Var _max = maxx(filter( ALLSELECTED(Data1), Data1[MASTID_EMPNO] = max(Data1[MASTID_EMPNO]) && Data1[ID_PRGDATE] = max(ID_PRGDATE) ),Data1[datetime])
return
CALCULATE(Count(Data1[datetime]), filter( (Data1), Data1[MASTID_EMPNO] = max(Data1[MASTID_EMPNO]) && Data1[ID_PRGDATE] = max(ID_PRGDATE) && Data1[datetime] =_max))https://amitchandak.medium.com/power-bi-get-the-last-latest-value-of-a-category-d0cf2fcf92d0
- v-zhangti
Community Support
Hi, DylanVelo
You can try the following methods.
Measure = Var _N1=CALCULATE ( COUNT ( 'Table'[MASTID_EMPNO] ), FILTER ( ALL ( 'Table' ), [MASTID_EMPNO] = SELECTEDVALUE ( 'Table'[MASTID_EMPNO] ) && [datetime] >= SELECTEDVALUE ( 'Table'[datetime] ) && [datetime] <= SELECTEDVALUE ( Slicer[Slicer] ) ) ) + 0 Var _N2=CALCULATE ( COUNT ( 'Table'[MASTID_EMPNO] ), FILTER ( ALL ( 'Table' ), [MASTID_EMPNO] = SELECTEDVALUE ( 'Table'[MASTID_EMPNO] ) && [datetime] >= SELECTEDVALUE ( 'Table'[datetime] ) ) ) Return IF(SELECTEDVALUE(Slicer[Slicer])=BLANK(),_N2,_N1)Is this the result you expect? Please see the attached document.
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.