Conditional formatting - Background color - Simple SQL help
Asked by Meta Box:

Hi!

I have a pretty simple use-case, related to the below type of tables: have text centered for all values and background color green when the value is "Yes".

I spent a long type looking into FORMATWIKI but without success. Thanks!

|--------|--------|--------|------|
|        | Garden | Garage | Pool |
| House1 | Yes    | No     | No   |
| House2 | No     | Yes    | No   |
| House3 | Yes    | No     | Yes  |


Answers:
author: Katerina Kovriga {Stiltsoft}
body: Hi [@Meta Box](/t5/user/viewprofilepage/user-id/4956140) ,

I assume that you are talking about our [Table Filter and Charts for Confluence](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?tab=overview&hosting=cloud&utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) app and its [Table Transformer](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Table+Transformer+macro?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro?

If this is the case, please try the following SQL query:

SELECT T1.'__',  
CASE WHEN T1.'Garden' = "Yes"  
THEN  
FORMATWIKI("{cell:bgColor=#00CC00\|align=center}" + T1.'Garden' + "{cell}")  
ELSE  
FORMATWIKI("{cell:align=center}" + T1.'Garden' + "{cell}")  
END  
AS 'Garden'  
FROM T1

![Thu 9-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/200365iE0C87B580EF4AAA6/image-size/large?v=v2&px=999 "Thu 9-1.png")

Repeat the CASE WHEN abstract for every column that you need to format.

Here is our documentation regarding [conditional formatting](https://docs.stiltsoft.com/display/public/TFAC/Advanced+table+cells+formatting#Advancedtablecellsformatting-Conditionalinsertingofvaluesandcoloringcellsbackground?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) for you to check.

Also keep in mind the following order of attributes: bgColor, textColor, align, width.

comments: author: Meta Box
body: Thank you, it worked perfectly!!!

One more question, what is the code if I want to put a specific background color for empty cell?

Thanks again


author: Katerina Kovriga {Stiltsoft}
body: Hi [@Meta Box](/t5/user/viewprofilepage/user-id/4956140) ,

The FORMATWIKI function works if your cells are not empty. So, you need to put some characters (for example, "-") into them manually or using an additional internal SELECT for your SQL query.

For example, if my cells are "Yes", "No", " " (empty) and I want to color "Yes" as green and empty cells as red, I can use the following SQL query:

SELECT '__',  
CASE WHEN 'Garden' = "Yes"  
THEN  
FORMATWIKI("{cell:bgColor=#00CC00\|align=center}" + 'Garden' + "{cell}")  
WHEN 'Garden' = "-"  
THEN  
FORMATWIKI("{cell:bgColor=Red\|align=center}" + 'Garden' + "{cell}")  
ELSE  
FORMATWIKI("{cell:align=center}" + 'Garden' + "{cell}")  
END  
AS 'Garden'  
FROM (SELECT '__',  
CASE WHEN 'Garden' IS NULL  
THEN  
"-"  
ELSE  
'Garden'  
END  
AS 'Garden' FROM T\*)

![Fri 12-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/200472iF92522AF7B6D539D/image-size/large?v=v2&px=999 "Fri 12-1.png")

Here we firstly insert "-" into empty cells and only then color them.


author: Keith Lanier
body: Hi [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) - I am trying to use this for my case, but I get this error:

TypeError: Cannot read properties of undefined (reading 'Executive Summary')

Here is my code:

SELECT 'Executive Summary',  
CASE  
WHEN INSTR('Executive Summary', "R\&amp;D Status - 100")  
THEN FORMATWIKI("{cell:bgColor=#8FCA7D}" + T1.'Executive Summary' + "{cell}")  
WHEN 'Executive Summary' = "-"  
THEN FORMATWIKI("{cell:bgColor=#FA7E70}" + T1.'Executive Summary' + "{cell}")  
ELSE  
FORMATWIKI("{cell:bgColor=#FFE784}" + T1.'Executive Summary' + "{cell}")  
END  
AS 'Executive Summary'

FROM (SELECT 'Executive Summary',  
CASE WHEN 'Executive Summary' IS NULL  
THEN  
"-"  
ELSE  
'Executive Summary'  
END  
AS 'Exective Summary' FROM T\*)


author: Natalie Paramonova [Stiltsoft]
body: Hi [@Meta Box](/t5/user/viewprofilepage/user-id/4956140) ,  
We've checked your code and here are the tips:

* Remove the "T1." indexes for the external SELECT. As you also use an internal SELECT, you can't refer to your table as "T1." here.
* Pay attention to the typo in the last line of your code: not "AS 'Exective Summary' FROM T\*)" but "AS 'Exec**u**tive Summary' FROM T\*)".
* You may also remove referrers to the 'Executive Summary' columns after both SELECTS. It is not a mistake, but there is no any sense: you select the column, then transform it and show it again with the same name. Your data is just overwtitten.

So, here is how it looks for us:  
![Thu 1-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/228711i8B475D159089FC83/image-size/large?v=v2&px=999 "Thu 1-1.png")  
SELECT  
CASE WHEN INSTR('Executive Summary', "R\&amp;D Status - 100")  
THEN FORMATWIKI("{cell:bgColor=#8FCA7D}" + 'Executive Summary' + "{cell}")  
WHEN 'Executive Summary' = "-"  
THEN FORMATWIKI("{cell:bgColor=#FA7E70}" + 'Executive Summary' + "{cell}")  
ELSE  
FORMATWIKI("{cell:bgColor=#FFE784}" + 'Executive Summary' + "{cell}")  
END  
AS 'Executive Summary'  
FROM (SELECT  
CASE WHEN 'Executive Summary' IS NULL  
THEN "-"  
ELSE 'Executive Summary'  
END  
AS 'Executive Summary' FROM T\*)  
![Thu 1-2.png](https://community.atlassian.com/t5/image/serverpage/image-id/228712iE4FA43D647120BE1/image-size/large?v=v2&px=999 "Thu 1-2.png")  
Hope it helps your case.


author: Keith Lanier
body: [@Natalie Paramonova \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/1779341) - thank you for the response, I am able to get this to work and understand the mistakes.

However, I am still struggling with SQL nesting issue. If I want to add more columns from T\* to the table, how to add to the SELECT statement?

For example, I want to add the Jira Key. How do I add it as the first column and the 'Executive Summary' as the second column?

SELECT  
'Key' AS 'Jira Key'  
FROM T\*


author: Natalie Paramonova [Stiltsoft]
body: Hi, to obtain several specific columns, you need to list them using commas:

SELECT 'Column 1', 'Column 2'  
FROM ...

If you want to list all the columns from the original table, use asterisk:

SELECT \*  
FROM ...

So, for your original query it will be smth like that:

SELECT 'Column 1' AS 'New name 1', 'Column 2' AS 'New name 2',  
CASE WHEN INSTR('Executive Summary', "R\&amp;D Status - 100")  
THEN FORMATWIKI("{cell:bgColor=#8FCA7D}" + 'Executive Summary' + "{cell}")  
WHEN 'Executive Summary' = "-"  
THEN FORMATWIKI("{cell:bgColor=#FA7E70}" + 'Executive Summary' + "{cell}")  
ELSE  
FORMATWIKI("{cell:bgColor=#FFE784}" + 'Executive Summary' + "{cell}")  
END  
AS 'Executive Summary'  
FROM (SELECT 'Column 1', 'Column 2',  
CASE WHEN 'Executive Summary' IS NULL  
THEN "-"  
ELSE 'Executive Summary'  
END  
AS 'Executive Summary' FROM T\*)

Hope it helps your case.


author: Keith Lanier
body: Thank you very much! Now it is working as I expect!

I realize now that since I am specifying the 'Executive Summary' column in the FROM statement, that I must also list ALL of the columns defined in the SELECT statement.

Basically, the second SELECT statement is nested in the FROM statement for the first SELECT. This was what I was missing.

So the logical syntax for a table with 2 columns to apply conditional formatting to 1 of the columns which contains empty cell would be:
>
> ```
> SELECT
>    'Column 1' AS 'New name 1', 
>    CASE [code to format 'Column 2'] END AS 'Column 2'
> FROM
>    (SELECT
>       'Column 1',
>       CASE [code to change value of 'Column 2'] END AS 'Column 2' 
>    FROM T*)
> ```

Thank you again!


author: Keith Lanier
body: [@Natalie Paramonova \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/1779341) - I have another basic formatting question using FORMATWIKI:

Is this correct syntax to apply 2 formats to the same column cells?

FORMATWIKI("{cell:bgColor=#FA7E70\|width=250px}" + 'Summary'+ "{cell}") AS 'Summary'

This does not work for me.


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@Keith Lanier](/t5/user/viewprofilepage/user-id/3516547),  

The code is correct, and it works fine for me. For example, regarding your previous query it will be looking as:  

SELECT  
CASE WHEN INSTR('Executive Summary', "R\&amp;D Status - 100")  
THEN FORMATWIKI("{cell:bgColor=#8FCA7D\|width=500px}" + 'Executive Summary' + "{cell}")  
WHEN 'Executive Summary' = "-"  
THEN FORMATWIKI("{cell:bgColor=#FA7E70\|width=500px}" + 'Executive Summary' + "{cell}")  
ELSE  
FORMATWIKI("{cell:bgColor=#FFE784\|width=500px}" + 'Executive Summary' + "{cell}")  
END  
AS 'Executive Summary'  
FROM (SELECT  
CASE WHEN 'Executive Summary' IS NULL  
THEN "-"  
ELSE 'Executive Summary'  
END  
AS 'Executive Summary' FROM T\*)  

![Tue 9-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/230274i9C5E4AF8E1B27BEA/image-size/large?v=v2&px=999 "Tue 9-1.png")  

You may check the names of your columns for typos (in the table and in the query itself), sometimes it happens.


author: Keith Lanier
body: Thank you! It is working for me now.

I am not sure if I had a hidden character or something that was causing it not to work initially.


author: Keith Lanier
body: [@Katerina Rudkovskaya \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/3738229) - now I have another request:

How can combine column "Key" with column "Summary" and keep the Key URL hyperlink in the combined column?

Here are the 2 columns which I want to combine:

'Key' AS 'Key'  
, FORMATWIKI("{cell:width=15%}" + 'Summary' + "{cell}") AS 'Capabilities'

Here is what I tried:

FORMATWIKI("{cell:width=15%}" + 'Summary' + "\\n" + 'Key' + "{cell}") AS 'New'

But, I want to keep the URL for the 'Key':

![Screenshot 2022-12-13 135558.png](https://community.atlassian.com/t5/image/serverpage/image-id/230308iFD8C0F9A8FFDA749/image-size/large?v=v2&px=999 "Screenshot 2022-12-13 135558.png")


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@Keith Lanier](/t5/user/viewprofilepage/user-id/3516547),  

Try to replace your pluses by commas, it [preserves initial text formatting](https://docs.stiltsoft.com/display/TFAC/Advanced+table+cells+formatting#Advancedtablecellsformatting-Preservinginitialcellformatting):  

FORMATWIKI("{cell:width=15%}", 'Summary', "\\n", 'Key', "{cell}")


author: Keith Lanier
body: Thanks again - that is a great tip!

For future reference, where can I find a user guide that documents all the syntax?

I've searched and can only fine more "how to" examples, but no real syntax user guide for all the options/formats/functions.


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@Keith Lanier](/t5/user/viewprofilepage/user-id/3516547),  

We have a rather big section within our documentation related to the Table Transformer macro:

* [General description](https://docs.stiltsoft.com/display/TFAC/How+to+use+Table+Transformer+macro?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111)
* [Default presets](https://docs.stiltsoft.com/display/TFAC/Default+automatic+presets?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111)
* [Syntax and available functions](https://docs.stiltsoft.com/display/TFAC/General+syntax+and+SQL+functions+available?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) (the macro is based on the AlaSQL library)
* General examples regarding [the most common use cases](https://docs.stiltsoft.com/display/TFAC/Custom+Transformation+-+use+cases+with+advanced+SQL+queries?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) (these cases come from our customers, and this section is often updated)
* General examples of [the FORMATWIKI usage](https://docs.stiltsoft.com/display/TFAC/Advanced+table+cells+formatting?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) (for example, your question about preserving the initial cell formatting is the last one on the page)

Hope it helps.


Tags: filter

format

table

table-filter

table-filter-and-charts

table-transformer

wiki