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.

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.