Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago

Add new rows based on previous and next row

Hello Power BI community,

 

I need a help creating a new row based on previous and next row. I currently have a table like this:

AccountPay Date
A01/01/2020
A02/01/2020
A03/01/2020
A05/01/2020
B08/01/2019
B09/01/2019
B10/01/2019
B11/01/2019
B01/01/2020

 

I want to make a new row and add a month if date is not continuous based on the previous and next row. For example, 04/01/2020 should be added for account A and 12/01/2019 for acccount B. Some accounts have all the rows I need, so I don't need to add rows for them.

 

This is what it should look like:

AccountPay Date
A01/01/2020
A02/01/2020
A03/01/2020
A04/01/2020
A05/01/2020
B08/01/2019
B09/01/2019
B10/01/2019
B11/01/2019
B12/01/2019
B01/01/2020

 

How can I acieve this?

2 Replies

  • HI Anonymous 

     

    I think the best solution for you is to use a calendar date and relate that to your table: 
    https://www.vahiddm.com/post/creating-calendar-table-with-3-steps

     

     

    but if you need DAX to create a new table and add missing dates, use this code to add a new table with DAX:

     

    Table 2 =
    VAR _A =
        FILTER (
            CALCULATETABLE (
                CALENDAR ( MIN ( 'Table'[Pay Date] ), MAX ( 'Table'[Pay Date] ) ),
                ALLEXCEPT ( 'Table', 'Table'[Account] )
            ),
            DAY ( [Date] ) = 1
        )
    VAR _B =
        VALUES ( 'Table'[Account] )
    RETURN
        FILTER (
            CROSSJOIN ( _B, _A ),
            [Date]
                >= CALCULATE (
                    MIN ( 'Table'[Pay Date] ),
                    'Table'[Account] = EARLIER ( [Account] )
                )
                && [Date]
                    <= CALCULATE (
                        MAX ( 'Table'[Pay Date] ),
                        'Table'[Account] = EARLIER ( [Account] )
                    )
        )

     

     

    Output:

     

     

    If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
    Appreciate your Kudos!!
    LinkedIn: 
    www.linkedin.com/in/vahid-dm/

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, thanks for your response. Is there a bettwer way to do this when I have 1000+ accounts?