Search code examples
powerquerypowerappsdataverse

How to get record before and after in forall loop powerapps


I am trying to run a function that returns a table with a corresponding date index and date text. This is so that I can display the information in a tutorial screen. However, I am confused on how to access to the previous record. Here is my pseudocode:

ForAll( Tweets (sorted by crf1d_date_index),
        If(the record IS NOT the LAST record,
             If('crf1d_date_index' != 'crf1d_date_index' of the NEXT record,
                     { 
                           Step: crf1d_date_index, 
                           Text: crf1d_tweet_time 
                     }
               )
           )

        If(the record IS the LAST record,
             If('crf1d_date_index' != 'crf1d_date_index' of the PREVIOUS record),
                     { 
                           Step: crf1d_date_index, 
                           Text: crf1d_tweet_time 
                     }
               )

)

Solution

  • You can use the Sequence function to create a list of indices, and then use that list within ForAll to access your list of tweets, something along the lines of the expression below:

    With(
      { myTweets, Tweets(sorted by crf1d_date_index) },
      ForAll(
        Sequence(CountRows(myTweets)),
        With(
          {
            tweet: Index(myTweets, Value),
            previousTweet: If(Value > 1, Index(myTweets, Value - 1)),
            nextTweet: If(Value < CountRows(myTweets), Index(myTweets, Value + 1))
          },
          If(
            Value < CountRows(myTweets),
            If(
              tweet.'crf1d_date_index' != nextTweet.'crf1d_date_index',
              { Step: crf1d_date_index, Text: crf1d_tweet_time }
            ),
            If(
              tweet.'crf1d_date_index' != previousTweet.'crf1d_date_index',
              { Step: crf1d_date_index, Text: crf1d_tweet_time }
            )
          )
        )
      )
    )