Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
maico_presente
Regular Visitor

Date Value from previous record

Hello,

 

I've been working on a problem for hours and can't get any further! HELP ;-))

 

I have a list of users who belong to a group from a certain date.
If a user is moved to another group i have another entry with the new "from" date.

I need a new column (date_to) that says until when the user was in the group. If there is no newer entry the date of today should be entered.

 

user_numbergroup_iddate_from
123xy10.11.2015
123ab05.07.2017
123de26.09.2019
456xy05.12.2016
456de03.03.2019

 

expected result:

user_numbergroup_iddate_fromdate_to
123xy10.11.201504.07.2017
123ab05.07.201725.09.2019
123de26.09.201912.12.2019
456xy05.12.201602.03.2019
456de03.03.201912.12.2019
1 ACCEPTED SOLUTION
V-pazhen-msft
Community Support
Community Support

@maico_presente 

 

You would need to add an index column in power query that allows you to look at the value from the previous row.

date_to =
VAR currentindex = 'Table'[Index]
VAR lastrowdate =
    CALCULATE (
        MAX ( 'Table'[date_from] ),
        ALLEXCEPT ( 'Table', 'Table'[user_number] ),
        FILTER ( 'Table', 'Table'[Index] = currentindex + 1 )
    )
RETURN
    IF (
        [date_from]
            = CALCULATE ( MAX ( [date_from] ), ALLEXCEPT ( 'Table', 'Table'[user_number] ) ),
        TODAY (),
        lastrowdate - 1
    )


Sample pbix: https://microsoftapc-my.sharepoint.com/:u:/g/personal/v-pazhen_microsoft_com/EUYxhep3tNpGk3Tinz6bPQQ...

Paul
Best

View solution in original post

3 REPLIES 3
V-pazhen-msft
Community Support
Community Support

@maico_presente 

 

You would need to add an index column in power query that allows you to look at the value from the previous row.

date_to =
VAR currentindex = 'Table'[Index]
VAR lastrowdate =
    CALCULATE (
        MAX ( 'Table'[date_from] ),
        ALLEXCEPT ( 'Table', 'Table'[user_number] ),
        FILTER ( 'Table', 'Table'[Index] = currentindex + 1 )
    )
RETURN
    IF (
        [date_from]
            = CALCULATE ( MAX ( [date_from] ), ALLEXCEPT ( 'Table', 'Table'[user_number] ) ),
        TODAY (),
        lastrowdate - 1
    )


Sample pbix: https://microsoftapc-my.sharepoint.com/:u:/g/personal/v-pazhen_microsoft_com/EUYxhep3tNpGk3Tinz6bPQQ...

Paul
Best

@V-pazhen-msft 
Fantastic!!! Thank you sooooo much!!!

Anonymous
Not applicable

Imke Feldmann has created a very useful function for shifting columns up and down:

 

https://www.thebiccountant.com/2018/07/12/fast-and-easy-way-to-reference-previous-or-next-rows-in-po...

 

The article above explains well how you can achieve your goal, but I will give a short summary. First, sort your table. Shift your date column with the mentioned function, the resulting column might be called date_from.Next. Then you can create a new column (like  date_to = if date_from.Next = null then DateTime.LocalNow() else Date.AddDays(date_from.Next, -1)) that contains the data you want.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.