Showing results for 
Search instead for 
Did you mean: 
Level 8

Dynamic Variables

Is it possible to have a dynamic variable that is dependent on the nth record it is in?


To explain:


I have a Gallery that shows records, each record has a different "Added Date and time".


I want to create a countdown against each record which displays the minutes and seconds left until 15 minutes after is was added.


i.e record 1 was added at 12:00 , current time is 12:05 , countdown would show 00:10:00 (Ten minutes left)


To generate the values needed to create the countdowns I have a timer with the duration of 1000 ms (1 sec) that repeats and does the below ontimerend (it works out the difference between when the record was added and now in ms)


UpdateContext( { Countdowns:{ Countdown1 : DateDiff(DateTimeValue(Text(First('DATASOURCE]'.Date_x0020_Added).Date_x0020_Added)),Now(),Milliseconds),Countdown2 : DateDiff(DateTimeValue(Text(Last(FirstN('DATASOURCE]'.Date_x0020_Added,2)).Date_x0020_Added)),Now(),Milliseconds) }} )

This creates the variable "countdowns" with both countdown1 and countdown2 retrieving their values from selected (1st & 2nd)  records, achieved by using the "First" and a "Last(FirstN" functions to select the correct record. This will be expanded for the 3rd 4th etc when I work out the below issue.


So this works well , however i'm now not sure how to call the variables in the respective record.


What I want to say in the label text property in each record (that will be the countdown) is "If i'm the first record in the gallery my text property should be Countdown1" and "If i'm the Second record in the gallery my text property should be Countdown2". 


So I need to call the correct variable dependant on which record "I" am.


I really hope this makes sense.


Alternatively if you can think of a different way of achieving this requirement please let me know.


To be honest this is were a foreach function would come in handy.


As always any help would be greatly appreciated!



Community Support Team
Community Support Team

Re: Dynamic Variables

Hi @SamPhillips,


Please consider take use of AddColumns() function to create a collection with a CountDown column added to the original data source, better with the Primary Key Renamed to another value, for example, rename the ID field to CID.

So the structure should be:

ClearCollect(Temp, AddColumns(RenameColumns(DataSource, "ID", "CID"), "CountDown",CountDownFormula ))

Once collection is generated, we could take use of the ID and the CID to lookup the CountDown value for each record.


For example:

CountDownLabel within the Gallery: 

Lookup(Temp, CID=ThisItem.ID, CounfDown)

Function reference:

AddColumns in PowerApps

Lookup in PowerApps


Please post back if you need any further help on this.




Community Support Team _ Michael Shao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources


Power Apps Super User Class of 2020

Check it out!


New Badges

Check it out!


Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!


Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020


Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors
Users online (5,718)