web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to iterate over co...
Power Apps
Unanswered

How to iterate over columns and assign unique value to each one

(0) ShareShare
ReportReport
Posted on by 2

Is there a way to iterate over the months for this code so i dont have to repeat the line 12 times
ClearCollect(
Eq_cons,
AddColumns(ShowColumns(EQ_Parameters, "Equipment"),"Jan",0,"Feb",0,"Mar",0,
"Apr",0,"May",0,"Jun",0,"Jul",0,"Aug",0,"Sep",0,"Oct",0,"Nov",0,"Dec",0)
);
ClearCollect(
Eq_cons,
ForAll(Eq_cons,{
Equipment:ThisRecord.Equipment,
Jan: LookUp(EQ_Parameters, Equipment = "Primary pumps").Rated_kW * LookUp(Month_Est, Parm = "Days in Month").Jan
}))

note that each month is a column name and it will be used to calculate the value under the column
idealy i want something like this (written in python)

 

for month in months:
 Eq_cons[month]=Function(month)

 



Categories:
I have the same question (0)
  • WarrenBelz Profile Picture
    156,064 Most Valuable Professional on at

    Hi @Moataz_Imad ,

    I am not entirely sure what you are doing here as you are collecting a Collection with itself, but I can tell you with certainty that you cannot refer to column names dynamically in the context you require (and that is a "both ends" of your references). So a lot of hard-coding coming up . . .

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • Moataz_Imad Profile Picture
    2 on at

    thank you for the fast reply!
    honeslty I'm still very new to the powerapps this code is the result of hours of trial and error, and chatGPT

    what I am trying to do is:
    in the first clearcollect i create the collection and fill it with 0s
    the second clearcollect fill each column with its calculated values
    please suggest if you have any more efficient way to do it

    the code is working fine and gives correct results but it is VERY hard coded (about 400 long lines of code) so I was just looking for ways to make it more efficient and clean

    Moataz_Imad_0-1686038074260.png

     

  • WarrenBelz Profile Picture
    156,064 Most Valuable Professional on at

    Hi @Moataz_Imad ,

    Generally I use a second collection, simply leaving the first intact for subsequent iterations, however that is not the issue here. I have now looked at it again and there is a shorter way as your Lookups do not relate to a collection field, so you can "pre-calculate" these, with one being a simple number and the second a Table of numbers. You can also include the first data set without doing a collection

    With(
     {
     wEq,
     AddColumns(
     ShowColumns(
     EQ_Parameters, 
     "Equipment"
     ),
     "Jan",
     0,
     "Feb",
     0,
     "Mar",
     0,
     "Apr",
     0,
     "May",
     0,
     "Jun",
     0,
     "Jul",
     0,
     "Aug",
     0,
     "Sep",
     0,
     "Oct",
     0,
     "Nov",
     0,
     "Dec",
     0
     )
     },
     With(
     {
     wKW:
     LookUp(
     EQ_Parameters, 
     Equipment = "Primary pumps"
     ).Rated_kW,
     wMonth:
     LookUp(
     Month_Est, 
     Parm = "Days in Month"
     )
     },
     ClearCollect(
     Eq_cons,
     ForAll(
     wEq As aCol,
     {
     Equipment: aCol.Equipment,
     Jan: wKW * wMonth.Jan,
     Feb: wKW * wMonth.Feb,
     Mar: wKW * wMonth.Mar,
     Apr: wKW * wMonth.Apr,
     May: wKW * wMonth.May,
     Jun: wKW * wMonth.Jun,
     Jul: wKW * wMonth.Jul,
     Aug: wKW * wMonth.Aug,
     Sep: wKW * wMonth.Sep,
     Oct: wKW * wMonth.Oct,
     Nov: wKW * wMonth.Nov,
     Dec: wKW * wMonth.Dec
     }
     )
     )
     )
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 337 Most Valuable Professional

#2
11manish Profile Picture

11manish 173

#3
Valantis Profile Picture

Valantis 86

Last 30 days Overall leaderboard