cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Opilio2
Frequent Visitor

New column with value of next row grouped/filtered by multiple columns

Hi everyone, 

since I'm no DAX pro, I urgently need your help with the following issue. 

Background: I'm analyzing the use of electronic devices. To be specific, I want to know how often users start different sequential programs in the daily use. That is, if a certain program on device A on day x is started, what is the next program?

Data basically looks like this: 

Device IDTimestampProgram ID
A2022-01-01 14:38   ID1
B2022-01-01 09:12  ID 2
A2022-01-03 19:45   ID 3
C2022-01-04 19:45   ID 2
B2022-01-01 11:03   ID 2
B2022-01-01 22:54   ID 1
A2022-01-03 23:09   ID 1

I need a new column "NextProgramSameDay"  which, for every distinct device and every single day, contains the program ID of the directly following program on the same day:

Device IDTimestampProgramNextProgramSameDayComment (for clarification, not to be included in the dataframe)
A2022-01-01 14:38ID 1 no next program because no additional program was started on the same day in device A
B2022-01-01 09:12ID 2ID 2Next program on device B on same day is program 2 at 11:03
A2022-01-03 19:45ID 3ID 1Next program on device A on  same day is program 1 at 23:09
C2022-01-04 19:45ID 2 no next program because no additional program was started on the same day in device C
B2022-01-01 11:03ID 2ID 1Next program on same day in device B is program 1 at 22:54
B2022-01-01 22:54ID 1 no next program because no additional program was started on the same day in device B
A2022-01-03 23:09ID 1 no next program because no additional program was started on the same day in device A

 

Can anyone tell me how to accomplish this? 

 

Thanks a lot in advance!!!

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Opilio2 
Yes you are right. I missed that we are dealing with time rather than date. Please use

NextProgramSameDay column = 
VAR CurrentTimeStamp = 'Table'[Timestamp]
VAR CurrentDate = DATE ( YEAR ( CurrentTimeStamp ), MONTH ( CurrentTimeStamp ), DAY ( CurrentTimeStamp ) )
VAR FilteredDeviceTable =
    FILTER (
        CALCULATETABLE (
            'Table',
            ALLEXCEPT ( 'Table', 'Table'[Device ID] )
        ),
        DATE ( YEAR ( 'Table'[Timestamp] ), MONTH ( 'Table'[Timestamp] ), DAY ( 'Table'[Timestamp] ) ) = CurrentDate
            && 'Table'[Timestamp] > CurrentTimeStamp
    )
VAR NextTimeStamp =
    MINX (
        FilteredDeviceTable,
        'Table'[Timestamp]
    )
VAR NextProgram =
    MINX ( FILTER ( FilteredDeviceTable, 'Table'[Timestamp] = NextTimeStamp ), 'Table'[Program ID] )
RETURN
    NextProgram

1.png

View solution in original post

6 REPLIES 6
tamerj1
Super User
Super User

Hi @Opilio2 
Yes you are right. I missed that we are dealing with time rather than date. Please use

NextProgramSameDay column = 
VAR CurrentTimeStamp = 'Table'[Timestamp]
VAR CurrentDate = DATE ( YEAR ( CurrentTimeStamp ), MONTH ( CurrentTimeStamp ), DAY ( CurrentTimeStamp ) )
VAR FilteredDeviceTable =
    FILTER (
        CALCULATETABLE (
            'Table',
            ALLEXCEPT ( 'Table', 'Table'[Device ID] )
        ),
        DATE ( YEAR ( 'Table'[Timestamp] ), MONTH ( 'Table'[Timestamp] ), DAY ( 'Table'[Timestamp] ) ) = CurrentDate
            && 'Table'[Timestamp] > CurrentTimeStamp
    )
VAR NextTimeStamp =
    MINX (
        FilteredDeviceTable,
        'Table'[Timestamp]
    )
VAR NextProgram =
    MINX ( FILTER ( FilteredDeviceTable, 'Table'[Timestamp] = NextTimeStamp ), 'Table'[Program ID] )
RETURN
    NextProgram

1.png

tamerj1
Super User
Super User

@Opilio2 

Try

 

NextProgramSameDay column =
VAR CurrentDate = Table[Timestamp]
RETURN
    COUNTROWS (
        FILTER (
            CALCULATETABLE (
                Table,
                ALLEXCEPT ( Table, Table[Device ID], Table[Timestamp] )
            ),
            Table[Timestamp] > CurrentDate
        )
    )

 

Unfortunately, this does not work but returns an empty column. 

tamerj1
Super User
Super User

Hi @Opilio2 

if you are looking for a column you can use

NextProgramSameDay column =
COUNTROWS (
    FILTER (
        CALCULATETABLE (
            Table,
            ALLEXCEPT ( Table, Table[Device ID], Table[Timestamp] )
        ),
        Table[Timestamp] > EARLIER ( Table[Timestamp] )
    )
)

Hey @tamerj1 ,
many thanks for your fast reply - this doesn't seem to be the solution, though; your suggestion only delivers blanks.
Additionally, I might have been mistakable, though- I'm looking for the following program ID/ name, given a certain deive ID and the date.

Are you creating a calculated column or w measure?

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Top Solution Authors
Top Kudoed Authors