Forum Discussion

GKJARC's avatar
GKJARC
Resolver I
4 years ago
Solved

Getting a date depending on previous rows

Hi all,

Below are 3 sample datasets, ordered by ValidFrom descending.

I'm trying to get Power BI to show in the far right column, the date since which a group of the same ID's is currently marked 1.
It is current when ValidTo = Blank.


However, in case there are two or more rows right below the current row that are also marked 1, Power BI should give the earliest ValidFrom date of those rows. 


In case the most recent row is marked 0, the far right column should show no date:


How might this be achieved in a measure or calculated column?

Any suggestions would be very welcome.

  • GKJARC's avatar
    GKJARC
    4 years ago

    Thank you very muchv-henryk-mstf for your suggestions. They were a good help although they did not completely meet all the requirements needed for this particular project.
    I now created a duplicate table and added in this duplicate table, an index column grouped by ID:
    https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query
    https://www.youtube.com/watch?v=jmzTHZK5z7g

    To create this index I followed these steps from the above links:

    1. Sort the column ValidFrom as Ascending (this makes Power BI allocate the highest Index to the latest ValidFrom. This will be important later for referring to previous row)
    2. Group the data by ID.
    3. Add custom column using ‘ Table.AddIndexColumn’ . Make sure Power BI has allocated the highest Index number to the latest ValidFrom date.
    4. Right click > Remove other columns.
    5. Expand table.
    6. Detect data type.

    That created this table as a duplicate from the original.



    Next, I created in this duplicate table, a calculated column to refer to the previous row, and continued from there:

     

2 Replies

  • v-henryk-mstf's avatar
    v-henryk-mstf
    Community Support

    Hi GKJARC ,

     

    Try the formula like below:

    Column = 
    MAXX (
        FILTER ( Table1, Table1[Marked] = 1 && Table1[Vaildto] = BLANK () ),
        Table1[Validfrom]
    )
    Column = 
    MAXX (
        FILTER (
            ALL ( Table2 ),
            Table2[Marked] = 1
                && Table2[Vaildto] <> BLANK ()
                && Table2[Validfrom] >= EARLIER ( Table2[Validfrom] )
        ),
        Table2[Validfrom]
    )

     

    If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


    Best Regards,
    Henry


    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • GKJARC's avatar
      GKJARC
      Resolver I

      Thank you very muchv-henryk-mstf for your suggestions. They were a good help although they did not completely meet all the requirements needed for this particular project.
      I now created a duplicate table and added in this duplicate table, an index column grouped by ID:
      https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query
      https://www.youtube.com/watch?v=jmzTHZK5z7g

      To create this index I followed these steps from the above links:

      1. Sort the column ValidFrom as Ascending (this makes Power BI allocate the highest Index to the latest ValidFrom. This will be important later for referring to previous row)
      2. Group the data by ID.
      3. Add custom column using ‘ Table.AddIndexColumn’ . Make sure Power BI has allocated the highest Index number to the latest ValidFrom date.
      4. Right click > Remove other columns.
      5. Expand table.
      6. Detect data type.

      That created this table as a duplicate from the original.



      Next, I created in this duplicate table, a calculated column to refer to the previous row, and continued from there: