# dataset highlight rows based on cell values

With the new Dataset/Background option feature introduced in 12.1, I would like to highlight the rows when either of the two columns has one of the target values in its cell. For example, I have the following data:

data = Transpose@Dataset@AssociationThread[{"a", "b", "c"}, RandomInteger[{1, 10}, {3, 10}]]


Iwould like to highlight those rows in red when either col a or b equals 1 or 10.

I can highlight rows based on one-col value, but don't know how to add the 2nd col to the expression.

Dataset[data, Background -> (Switch[#3[#2[[1]], "a"], 1 | 10, LightRed, _, White] &)]


How can I do that? Thanks!

SeedRandom[77];
RandomInteger[{1, 10}, {3, 10}]];

Dataset[data,
Background -> Normal[MapIndexed[#2[[1]] ->
If[Or @@ (MatchQ[1 | 10] /@ {#a, #b}), Pink, White] &, data, {1}]]]


• Nice use of MapIndexed. I think you meant to write {#a, #b} instead instead of {#a, #c} – theorist May 13 at 9:10
• Thank you @theorist. I had misread the question. Updated with the correction. – kglr May 13 at 9:36
• Thanks for the answer. I really like the way of using MapIndexed and Or. – Bemtevi77 May 13 at 14:29
data = Transpose@
RandomInteger[{1, 10}, {3, 10}]]

rowcolors =
If[MemberQ[{1, 10}, #[[1]]] || MemberQ[{1, 10}, #[[2]]], LightRed, White] & /@ Normal[data];

Dataset[data, Background -> {rowcolors}]


Alternatively,

rowcolors =
Table[If[MemberQ[{1, 10}, data[n, "a"]] ||
MemberQ[{1, 10}, data[n, "b"]], LightRed, White], {n, Length[data]}]

• Thanks for the answer! I wonder would it be possible to implement via the Pattern Matching? Thanks again. – Bemtevi77 May 13 at 7:36

Given:

SeedRandom[3]
data =
RandomInteger[{1,10},{10, 3}] //


and a helper function:

color = Replace @ {KeyValuePattern["a"|"b" -> 1|10] -> LightRed, _ -> White};


then we could write:

Dataset[data, Background -> Normal@data[{All}, color]]


or

Dataset[data, Background -> (color[Normal@#3[#2[[1]]]]&)]


• This is also very nice solution. I have never learned KeyValuePattern before your answer. Thanks! – Bemtevi77 May 14 at 2:23