Search code examples
if-statementgoogle-sheetsarray-formulasgoogle-sheets-querysparklines

Reversing a column when calculating slope for a line chart in Google Sheets


I have the following sheets:

https://docs.google.com/spreadsheets/d/1aC9lsmxVw0pYN_Wjk7gooB0c7CsvmkRsEeCUBEKUIlM/edit?usp=sharing

It should be pretty obvious looking at it. There is a spark-line which becomes green if the the trend is positive. From the data, it makes intuitive sense that the line should be trending up. However, due to the way I wrote the formula, the line is instead trending down and red. How can I reverse the columns being used in the formula?

Note: The data on the right hand side should remain in the same order.

Thanks for any help.


Solution

  • try:

    =IFERROR(ARRAYFORMULA(SPARKLINE(
     QUERY({B2:B, ROW(B2:B)}, "select Col1 order by Col2 desc"), 
     {"charttype", "line"; "color", IF(SLOPE(
     QUERY({B2:B, ROW(B2:B)}, "select Col1 order by Col2 desc"), 
     ROW(A2:A)-1)>=0, "lime", "red"); "linewidth", 2})))
    

    enter image description here