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 / Building a dynamically...
Power Apps
Unanswered

Building a dynamically sized heatmap with blocks that change colour via clicking

(0) ShareShare
ReportReport
Posted on by 5

I have built a heatmap using the following code in a gallery Items section:

 

 

SortByColumns(
 Ungroup(
 AddColumns(
 RenameColumns(Sequence(width),"Value", "Impact"),
 "ProbabilityTable", RenameColumns(Sequence(height),"Value", "Probability")
 ),
 "ProbabilityTable"
 ),
 "Impact",Descending,"Probability",Ascending
)

 

 

(source: https://forwardforever.com/creating-a-heat-map-in-power-apps/)

 

The 'width' and 'height' variables are taken from a Dataverse table. The user may change these values via a form, and create a heatmap of varying sizes e.g. 6x6, 9x9, 3x5, etc. Below is an example 6x6 grid that I have been experimenting on with colours.

 

heatmap.png

 

Currently, the user can change the colour between 6 options by clicking on each square individually. The way I do this is with a massive switch statement in the 'OnSelect' option of the label within the Gallery. The variable r1c1 means row 1, column 1. There is a variable for each box in the heatmap. The 'categoryCount' variable is a number ranging between 3-6, depending on how many colour categories the user wants in their heatmap. This essentially means that once the user hits the final colour, it will loop back round to the beginning.

 

 

Switch(
 ThisItem.Impact,
 6,
 Switch(
 ThisItem.Probability,
 1, UpdateContext({r1c1: r1c1+1});
 If(r1c1=categoryCount,UpdateContext({r1c1: 0}),
 2, UpdateContext({r1c2: r1c2+1});
 If(r1c2=categoryCount,UpdateContext({r1c2: 0}),
 3, UpdateContext({r1c3: r1c3+1});
 If(r1c3=categoryCount,UpdateContext({r1c3: 0}),
 4, UpdateContext({r1c4: r1c4+1});
 If(r1c4=categoryCount,UpdateContext({r1c4: 0}),
 5, UpdateContext({r1c5: r1c5+1});
 If(r1c5=categoryCount,UpdateContext({r1c5: 0}),
 6, UpdateContext({r1c6: r1c6+1});
 If(r1c6=categoryCount,UpdateContext({r1c6: 0})
 )
),
[continues on through 5, 4, 3, 2, 1 each time looping through 6 values for Probability.]

 

 

 

 The way I assign RGBA values is currently through a massive IF statement in the TemplateFill section of the gallery, I didn't want to re-type the RGBA values as I don't have them to hand - so that is not an error, don't worry.

 

 

If(And(ThisItem.Impact=6,ThisItem.Probability=1,r1c1=0),RGBA(green value),
And(ThisItem.Impact=6,ThisItem.Probability=1,r1c1=1),RGBA(amber value),
And(ThisItem.Impact=6,ThisItem.Probability=1,r1c1=2),RGBA(light red value),
And(ThisItem.Impact=6,ThisItem.Probability=1,r1c1=3),RGBA(light green value),
And(ThisItem.Impact=6,ThisItem.Probability=1,r1c1=4),RGBA(dark amber value),
And(ThisItem.Impact=6,ThisItem.Probability=1,r1c1=5),RGBA(dark red value))

[this goes on for r1c1 through to r1c6, with the Impact staying the same and the Probability going from 1 to 6 - this covers the entire first row. This is then repeated for each row in the 6x6 heatmap]

 

 

 

So, what are my current problems?

I need to find a way to make this dynamic for any size of heatmap. I'm familiar with how I'd do this in a traditional programming language, but the ForAll loops in PowerApps confuse me - and I can't set variables within them. I managed to edit something from a tutorial that was nearly what I wanted, but doesn't really behave in any way that I understand.

 

 

ClearCollect(tempCollection, RenameColumns(colHeatMap, "Width", "newWidth", "Height", "newHeight"));
ForAll(tempCollection,
 Patch(
 colHeatMap,
 LookUp(colHeatMap,Width=newWidth),
 {Pos: Value(ThisRecord.Pos) + 1}
 )
)

 

 

(source: https://www.codesharepoint.com/PowerPlatform/powerapps-forall-function-with-examples)

 

For the above example, I used the following (slightly edited) collection from the heatmap tutorial:

 

 

ForAll(RenameColumns(Sequence(width), "Value", "ProbabilityValue"),
 ForAll(RenameColumns(Sequence(height), "Value", "ImpactValue"),
 Collect(colHeatMap,{Probability:ProbabilityValue,Impact:ImpactValue,Pos:0})
 )
)

 

 

 (source: https://forwardforever.com/creating-a-heat-map-in-power-apps/)

 

Hopefully my problem and explanation make sense - please let me know if you have any questions, or if I don't make sense, and I will do my best to clarify.

Categories:
I have the same question (0)
  • Scott_Parker Profile Picture
    1,090 on at

    The thing to realize about ForAll is that despite the impression given by having a "for" in the name, ForAll isn't a loop at all. No iteration is taking place. It is analogous to the map function in other languages. ForAll does something with each record in a table, with no regard for order of which one happens first.

    You provided a good amount of detail about the issue. But, I'm not quite understanding the problem because your last code block looks like it is already dynamically sized. Set the gallery WrapCount to change dynamically, as well.

  • and21100 Profile Picture
    5 on at

    Hey Scott,

     

    Thanks for your reply. Shame that ForAll doesn't really act like a for loop! So the issue is, it is currently dynamically sized up to a 6x6 grid. But if I were to choose a 10x10 grid, then anything outside the 6x6 grid will not react to being clicked and will not have any colour. There is the option to set a max grid size (in this case the max is 10x10), and to just expand the switch/IF statements to go from 1 to 10 - however this leads to pretty big performance issues (I think over 600 conditions in an IF statement?). So I'm looking to find a way to reduce the massive IF statement in TemplateFill, and preferably to Switch statement too, to something that is more efficient and performance friendly.

     

    Currently I've found a work-around, where instead of having one big gallery I have 10 different galleries. Each gallery is a row in the heatmap, so the TemplateFill just has an IF statement handling 60 conditions at a time instead of 600 every time a square is clicked. So far this has been performing well, it's just clunky to setup and might be more difficult to get information from the powerapp into a database.

  • Scott_Parker Profile Picture
    1,090 on at

    Okay, I think I get the issue now. Yes, the template fill is the problem. So is the large switch statement in the OnSelect.

     

    Instead of having r1c1, r1c2, etc. variables, this information should go into a collection. I went with the fields Impact, Probability, and Color.

     

    I added a variable "HeatColorCount" which represents the number of colors. What you need to do is change your OnSelect of the heat map square to be:

     

    With(
     {
     xItem: LookUp(
     colHeatMap,
     Impact = ThisItem.Impact And Probability = ThisItem.Probability
     )
     },
     Patch(
     colHeatMap,
     xItem,
     {
     Color: Mod(
     xItem.Color,
     HeatColorCount
     ) + 1
     }
     )
    )


     

    And change the heatmapsquare.color formula to be something like:

     

    RGBA 0, 18, 107, (.8) * LookUp(
     colHeatMap,
     Probability = ThisItem.Probability And Impact = ThisItem.Impact
     ).Color / HeatColorCount
    )


    This is just a gradient. In your case, do a switch statement on the LookUp().Color to get what you want. If you want to make your color sets customizable, have another collection for your Colors and do a LookUp() on those.

     

     

  • and21100 Profile Picture
    5 on at

    Thanks so much Scott, this makes sense to me - I'll have a go at implementing it this week and get back to you 🙂

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 July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 416 Most Valuable Professional

#2
11manish Profile Picture

11manish 205 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard