1
votes

I have a dataset in Power BI like this:

ID      FirstDate    LastDate
214443  07/06/2016  07/06/2017
214443  09/11/2016  09/11/2017
214443  28/09/2018  11/06/2019
214443  31/05/2019  11/06/2019

I would like to create two calculated columns that contain the latest date for every index. Currently, I am only able to get the latest date of all the dataset or the same date for every row.

The output should be this:

ID      FirstDate   LastDate    FirstDate2  LastDate2
214443  07/06/2016  07/06/2017      
214443  09/11/2016  09/11/2017  07/06/2016  07/06/2017
214443  28/09/2018  11/06/2019  09/11/2016  09/11/2017
214443  31/05/2019  11/06/2019  28/09/2018  11/06/2019

Thanks!!

2

2 Answers

2
votes

I started with a data like this,

enter image description here

And then added the Index Column using the query editor,

enter image description here

And then created a column to check the number of times a single ID is present :-

Number of ID's Present = COUNTROWS(FILTER('MyTable', 
     (EARLIER('MyTable'[ID]) = 'MyTable'[ID])))

And then wanted to know the starting Index of each ID's to make it easy :-

Starting Index = CALCULATE(MIN(MyTable[Index]),FILTER('MyTable', 
     (EARLIER('MyTable'[ID]) = 'MyTable'[ID])))

And then now you can do what you are looking for with some easy ways,

First Date 2 = 
    Var NumberofIDsPresent = MyTable[Number of ID's Present]
    Var StartingIndex = MyTable[Starting Index]
    Var CurrentIndex = MyTable[Index]

    Var Only_OneTime_ID_Present = IF(OR(NumberofIDsPresent = 1,CurrentIndex = StartingIndex),1,0)
    Var Multiple_Times_ID_Present = CALCULATE(MIN(MyTable[First Date]),FILTER(MyTable, MyTable[Index] = CurrentIndex - 1))
    var result = IF(Only_OneTime_ID_Present = 1, BLANK(),Multiple_Times_ID_Present)
    return result


Last Date 2 = 
    Var NumberofIDsPresent = MyTable[Number of ID's Present]
    Var StartingIndex = MyTable[Starting Index]
    Var CurrentIndex = MyTable[Index]

    Var Only_OneTime_ID_Present = IF(OR(NumberofIDsPresent = 1,CurrentIndex = StartingIndex),1,0)
    Var Multiple_Times_ID_Present = CALCULATE(MIN(MyTable[Last Date]),FILTER(MyTable, MyTable[Index] = CurrentIndex - 1))
    var result = IF(Only_OneTime_ID_Present = 1, BLANK(),Multiple_Times_ID_Present)
    return result

Now, this makes my data look something like this,

enter image description here

Please accept the answer if it helps and Kindly let me know, if it doesn't solve your problem.

0
votes

Take the max over the FirstDate column only considering rows with matching ID and dates before the current row's date.

Previous Date =
CALCULATE (
    MAX ( Table1[FirstDate] ),
    FILTER (
        ALLEXCEPT ( Table1, Table1[ID] ),
        Table1[FirstDate] < EARLIER ( Table1[FirstDate] )
    )
)