Forum Discussion

BDenman24's avatar
BDenman24
Cloud Apps
09-05-2024
Solved

Conditional Text in BloX Widget

Hello,

I'm still pretty new at BloX widgets, so hopefully this is a simple correction.  I am trying to make a BloX widget have conditional text if a value is below another value, display text, else display another text.  I am able to get the background color to change based upon that criteria, but not the text.  Below is my code:

{
"style": "",
"script": "",
"title": "",
"showCarousel": true,
"carouselAnimation": {
"delay": 0
},
"conditions": [
{
"minRange": "-Infinity",
"maxRange": "{panel:DIR Company Avg}",
"backgroundColor": "#fd6e69"
},
{
"minRange": "{panel:DIR Company Avg}",
"maxRange": "Infinity",
"backgroundColor": "#3adcca"
}
],
"body": [
{
"spacing": "none",
"type": "Container",
"items": [
{
"spacing": "small",
"type": "TextBlock",
"class": "condition_data",
"text": "Your DIR score is {panel: DIR Score}",
"horizontalAlignment": "center",
"size": "large",
"weight": "bold",
"color": "white"
}
]
}
],
"conditionsTarget": [
{
"data-class": "condition_data",
"conditions": [
{
"minRange": "-Infinity",
"maxRange": "{panel:DIR Company Avg}",
"type": "TextBlock",
"text": "below avg"
},
{
"minRange": "{panel:DIR Company Avg}",
"maxRange": "Infinity",
"type": "TextBlock",
"text": "above avg"
}
]
}
]
}
Any help would be greatly appreciated!  Thanks!
  • Hi BDenman24 ,

    One solution is as follows:

    1. Create a measure titled 'Is Above' that returns 1 if the value is "Above Avg" and 0 if it is "Below Avg." Your formula may look something like this: 

     

    case when sum([DIR Score]) > AVG([DIR Company Avg]) then 1 else 0 end

     

     

    • Update the BloX script with the following:

     

    {
        "style": "",
        "script": "",
        "title": "",
        "showCarousel": true,
        "carouselAnimation": {
            "delay": 0
        },
        "conditions": [
            {
                "minRange": "-Infinity",
                "maxRange": "{panel:DIR Company Avg}",
                "backgroundColor": "#fd6e69"
            },
            {
                "minRange": "{panel:DIR Company Avg}",
                "maxRange": "Infinity",
                "backgroundColor": "#3adcca"
            }
        ],
        "body": [
            {
                "spacing": "none",
                "type": "Container",
                "items": [
                    {
                        "spacing": "small",
                        "type": "TextBlock",
                        "class": "condition_data",
                        "text": "Your DIR score is {panel: DIR Score}",
                        "horizontalAlignment": "center",
                        "size": "small",
                        "weight": "bold",
                        "color": "white"
                    },
                    {
                        "spacing": "small",
                        "type": "TextBlock",
                        "text": "{panel: Is Above}",
                        "horizontalAlignment": "center",
                        "size": "small",
                        "weight": "bold",
                        "color": "white"
                    }
                ]
            }
        ]
    }​

     

    • Add the widget script to the BloX widget:

     

    widget.on('processresult', function(w, args){
    		
    	args.result.forEach(item => {
    		
    		Object.keys(item).forEach(el => {
    			if(item[el].Panel === 'Is Above') {
    				if(item[el].Value === 0)
    					item[el].Text = 'Below Avg'
    				else
    					item[el].Text = 'Above Avg'
    			}
    		
    		})
    	})
    
    })​

     

    Here's the result: 

    Please let me know if this works for you.

     

    Thanks,

    Hari

    https://www.binextlevel.com/ 

    ā€ƒ

4 Replies

  • harikm007's avatar
    harikm007
    Data Warehouse

    Hi BDenman24 ,

    One solution is as follows:

    1. Create a measure titled 'Is Above' that returns 1 if the value is "Above Avg" and 0 if it is "Below Avg." Your formula may look something like this: 

     

    case when sum([DIR Score]) > AVG([DIR Company Avg]) then 1 else 0 end

     

     

    • Update the BloX script with the following:

     

    {
        "style": "",
        "script": "",
        "title": "",
        "showCarousel": true,
        "carouselAnimation": {
            "delay": 0
        },
        "conditions": [
            {
                "minRange": "-Infinity",
                "maxRange": "{panel:DIR Company Avg}",
                "backgroundColor": "#fd6e69"
            },
            {
                "minRange": "{panel:DIR Company Avg}",
                "maxRange": "Infinity",
                "backgroundColor": "#3adcca"
            }
        ],
        "body": [
            {
                "spacing": "none",
                "type": "Container",
                "items": [
                    {
                        "spacing": "small",
                        "type": "TextBlock",
                        "class": "condition_data",
                        "text": "Your DIR score is {panel: DIR Score}",
                        "horizontalAlignment": "center",
                        "size": "small",
                        "weight": "bold",
                        "color": "white"
                    },
                    {
                        "spacing": "small",
                        "type": "TextBlock",
                        "text": "{panel: Is Above}",
                        "horizontalAlignment": "center",
                        "size": "small",
                        "weight": "bold",
                        "color": "white"
                    }
                ]
            }
        ]
    }​

     

    • Add the widget script to the BloX widget:

     

    widget.on('processresult', function(w, args){
    		
    	args.result.forEach(item => {
    		
    		Object.keys(item).forEach(el => {
    			if(item[el].Panel === 'Is Above') {
    				if(item[el].Value === 0)
    					item[el].Text = 'Below Avg'
    				else
    					item[el].Text = 'Above Avg'
    			}
    		
    		})
    	})
    
    })​

     

    Here's the result: 

    Please let me know if this works for you.

     

    Thanks,

    Hari

    https://www.binextlevel.com/ 

    ā€ƒ

    • BDenman24's avatar
      BDenman24
      Cloud Apps

      Thanks for the response!  I tried a lot of the solutions you pointed out and nothing really solved my issue.  However, Hari's solution below did!  Thanks again!