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

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.
GKJARC_2-1648490348509.png

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. 
GKJARC_1-1648490320702.png

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

GKJARC_3-1648490389815.png


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

Any suggestions would be very welcome.

1 ACCEPTED SOLUTION

Thank you very much@v-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.

GKJARC_1-1648984728027.png

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

GKJARC_2-1648984819800.png

 

View solution in original post

2 REPLIES 2
v-henryk-mstf
Community Support
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]
)

vhenrykmstf_0-1648704534835.pngvhenrykmstf_1-1648704572229.png

 

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.

Thank you very much@v-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.

GKJARC_1-1648984728027.png

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

GKJARC_2-1648984819800.png

 

Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors
Top Kudoed Authors