# Power table background color change based on another columns value

**URL:** <https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144>\
**Category:** Ignition\
**Created:** [January 26, 2019, 10:58pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144 "2019-01-26T22:58:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [January 26, 2019, 10:58pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/1 "2019-01-26T22:58:59Z")

</div>

How do you change the background color of a column based on another columns value. I can change the same column but I don’t know how to get it to change another ones background. The script below just changes that certain column.

```auto
if colName == 'ReqReading' and value == 0:
			return {'background' : 'black'}

```

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [January 27, 2019, 10:46pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/2 "2019-01-27T22:46:08Z")

</div>

Has anyone done this before?

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [January 28, 2019, 1:22am UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/3 "2019-01-28T01:22:25Z")

</div>

```auto
	data = self.data
	
	returnValue = {}
	
	if colName == "Col1":
		if value > 10:
			returnValue["background"] = "red"
	elif colName == "Col2":
		if data.getValueAt(rowIndex,"Col1" )>10 and value > 10:
				returnValue["background"] = "blue"
	
	return returnValue

```

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [January 30, 2019, 4:18pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/4 "2019-01-30T16:18:26Z")

</div>

that worked great. I was able to modify it to work the way I need it.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [January 30, 2019, 4:22pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/5 "2019-01-30T16:22:28Z")

</div>

I want to add text to the certain row and column through the same script. Is there a way to do this? This is what I have for the solution for the color. but I am not for sure how to add text to the same column. The text I want to add is N/A in white letters.

```auto
data = self.data		
	returnValue = {}		
	
	if colName == "Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==1:
							returnValue["background"] = "green"
	
	if colName =="Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==0:
								returnValue["background"] = "black"
								
	return returnValue

```

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [January 30, 2019, 4:26pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/6 "2019-01-30T16:26:10Z")

</div>

I have tried this but it takes the black color away.

```auto
data = self.data		
	returnValue = {}		
	
	if colName == "Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==1:
							returnValue["background"] = "green"
	
	if colName =="Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==0:
								returnValue["background"] = "black"
								textValue["N/A"]="white"
	return returnValue

```

---

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [January 30, 2019, 4:35pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/7 "2019-01-30T16:35:53Z")

</div>

```auto
data = self.data		
	returnValue = {}		
	
	if colName == "Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==1:
			returnValue["background"] = "green"
	
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="N/A"
			returnValue["foreground"]="white"

	return returnValue

```

What happened with your code was it had an exception at the textValue[“N/A”]=“white” line - textValue wasn’t defined so it threw an error, which means it never got to return a value at all. This should work.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [January 30, 2019, 7:10pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/8 "2019-01-30T19:10:24Z")

</div>

My next issue is turning the comments column background red only if the status column texted is Maint Required or Getting Moisture but it turns the whole column red not just the certain row.

```auto
data = self.data		
	returnValue = {}		
		
	if colName == "Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==1:
			returnValue["background"] = "green"
		
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="N/A"
			returnValue["foreground"]="white"
	
	if colName == 'Status' and value == "Maint Required":
		returnValue ['background'] = 'red'
	
	if colName =="Comments":
				if data.getValueAt(rowIndex,"Status" )=="Maint Required" or "Getting Mositure":
					returnValue["background"] = "red"
					
	return returnValue

```

---

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [January 30, 2019, 8:35pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/9 "2019-01-30T20:35:21Z")

</div>

It’s this line:

```auto
if data.getValueAt(rowIndex,"Status" )=="Maint Required" or "Getting Mositure"

```

The right side of the ‘or’ is just a string, which evaluates to true. So it’s ALWAYS true for that column. Try something like this:

```auto
if data.getValueAt(rowIndex,"Status" ) in ["Maint Required", "Getting Mositure"]:

```

It gets the value of the Status column at that row index, and checks to see if it’s in an array with just those two strings in it. If its either one of those, this evaluates to True.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [January 31, 2019, 7:47pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/10 "2019-01-31T19:47:47Z")

</div>

this one worked before I added the one below

```auto
if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="0"
			returnValue["foreground"] = "white"

```

this one below doesn’t seem to work, any ideas?

```auto

	
	if data.getValueAt(rowIndex,"Reading" ) < 200:
				return {'background' : 'red'}

```

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [February 4, 2019, 1:55pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/11 "2019-02-04T13:55:13Z")

</div>

any Ideas on the question above, is it over righting it?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 4, 2019, 2:18pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/12 "2019-02-04T14:18:18Z")

</div>

The latter operation is a pure return of a fixed dictionary (just for a red background). A return instruction exits the function at that point, yielding just what it’s given. You don’t show enough of your script to be sure, but consider just assigning:

```auto
	if data.getValueAt(rowIndex,"Reading" ) < 200:
				returnvalue['background'] = 'red'

```

Assuming there’s an unconditional `return returnvalue` later in the script.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [February 4, 2019, 2:27pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/13 "2019-02-04T14:27:25Z")

</div>

That didn’t work either, here is what I have . I have all tried changing it to a elif but still the same result

````auto
# reference column name Question
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="0"
			returnValue["foreground"] = "white"
	
	if data.getValueAt(rowIndex,"Reading" ) < 200:
		returnValue["background"] = "red"
	```
````

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 4, 2019, 2:39pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/14 "2019-02-04T14:39:02Z")

</div>

Please post your entire script. I’m also now confused as to what each column’s behavior should be.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [February 4, 2019, 2:47pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/15 "2019-02-04T14:47:13Z")

</div>

If the ReqReading column values is equal to 0 then, the Reading column rows goes black and puts a 0 in that row only. ReqReading is a hidden column.The ones left white are for data entry. If the value is greater the the listed value in the question column then I want to change the color of that row.

 ![Capture](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/337a675858551cd1749ab749ac82bd9cee6f9ee5.png)

```auto
data = self.data		
	returnValue = {}		
	
	# reference column name Question and Descr
	if colName =="Question":
			
			if data.getValueAt(rowIndex,"Descr" )== "Air Compressor #1 Quincy 750":
				returnValue["background"] = "D5D5D5"	
	
			elif data.getValueAt(rowIndex,"Descr" )== "Air Cooling Tower Thermotech #1 FEVF-13412MA":
				returnValue["background"] = "D5D5D5"	
			
			elif data.getValueAt(rowIndex,"Descr" )== "Air Dryer ICP IDHE-800 #1":
				returnValue["background"] = "D5D5D5"
				
			elif data.getValueAt(rowIndex,"Descr" )== "Air Compressor #2 Quincy 750":
				returnValue["background"] = "D5D5D5"		
	
			elif data.getValueAt(rowIndex,"Descr" )== "Air Cooling Tower Thermotech #2 FEVF-13412MA":
				returnValue["background"] = "D5D5D5"		
			
			elif data.getValueAt(rowIndex,"Descr" )== "Air Dryer ICP IDHE-1000 for ELGI Air Compressor #1":
				returnValue["background"] = "D5D5D5"
				
			elif data.getValueAt(rowIndex,"Descr" )== "Air Compressor Ingersoll Rand #1 R90i-W145":
				returnValue["background"] = "D5D5D5"
			
			elif data.getValueAt(rowIndex,"Descr" )== "Air Cooling Tower Gunter #1 S-GFW 1/4 for Ingersoll Rand Air Comp 1 & 2":
				returnValue["background"] = "D5D5D5"
	# reference column name Question
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="0"
			returnValue["foreground"] = "white"
	
		elif data.getValueAt(rowIndex,"Reading" ) < 200:
			returnValue["background"] = "red"
	
	
	
	if colName == "Status" and value == "Maint Required":
		returnValue ["background"] = "FF4747"
	elif colName == "Status" and value == "Getting Moisture":
				returnValue ["background"] = "FF4747"
	elif colName=="Status" and value == "OK":
				returnValue ["background"] = "8AFF8A"
	
	elif colName =="Comments":
		if data.getValueAt(rowIndex,"Status" )=="Maint Required":
			returnValue["background"] = "FF4747"
		if data.getValueAt(rowIndex,"Status" )=="Getting Moisture":
			returnValue["background"] = "FF4747"
		if data.getValueAt(rowIndex,"Status" )=="OK":
			returnValue["background"] = "black"
			returnValue["text"]="N/A"
			returnValue["foreground"]="white"		
	return returnValue

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 4, 2019, 3:49pm UTC](https://forum.inductiveautomation.com/t/power-table-background-color-change-based-on-another-columns-value/22144/16 "2019-02-04T15:49:32Z")

</div>

First, none of your returnvalue[] assignments are cumulative. That is, at any branch where you are setting return values, only that branch sets values. So you can use actual return statements for faster execution and better readability. Also, consider adding another hidden column, boolean, to identify the questions that need to be grey. `GreyQ`, perhaps. Also, consider only using .getValueAt() once per value retrieved, into a temporary variable. Assuming ReqReading is the listed value from the question, I’d have a routine that looks like this:

```python
	ds = self.data		

	if colName == "Question":
		if ds.getValueAt(rowIndex, 'GreyQ'):
			returnValue["background"] = "D5D5D5"	

	elif colName == "Reading":
		ReqReading = ds.getValueAt(rowIndex, "ReqReading")
		if ReqReading:
			if value < ReqReading:
				return {'background': 'red'}
		else:
			return {'background': 'black', 'text': '0', 'foreground': 'white'}

	elif colName == "Status":
		if value in ("Maint Required", "Getting Moisture"):
			return {'background': 'FF4747'}
		if value == "OK":
			return {'background': '8AFF8A'}
	
	elif colName == "Comments":
		Status = ds.getValueAt(rowIndex, "Status")
		if Status in ("Maint Required", "Getting Moisture"):
			return {'background': 'FF4747'}
		if Status == "OK":
			return {'background': 'black', 'text': 'N/A', 'foreground': 'white'}

```

I would also consider encoding more of this information in hidden columns.
