Forum Discussion

eliasayyy's avatar
eliasayyy
Memorable Member
8 months ago
Solved

Automatically Scale My Line Chart Based on user selection

Hello Everyone here is link to my  sample dataset and pbix.
Test 

here is the pbix screenshot



Issue: Depending on measures selected, scaling is very off and doesnt show well. 


Requirment: Scale the other measures to match with the biggest measure in this example, Total Impression, to have all the lines be readable( i can turn off axis values and fix the tooltip)

Preference: Function, DAX, html visual, deneb anything to solve that issue dynamically.

Thank you in advanced

  • Hi eliasayyy,

    I hope you are doing well ☺️❤️

     

    So unfortunately I can't download the file but based on my understand you want to dynamically scale measures in a line chart so all lines are readable

    You can try this Deneb (Vega-Lite) that automatically normalizes your data:

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {"name": "dataset"},
      "transform": [
        {
          "aggregate": [
            {"op": "sum", "field": "Impressions", "as": "Total Impressions"},
            {"op": "sum", "field": "Clicks", "as": "Total Clicks"},
            {"op": "sum", "field": "Spend", "as": "Total Spend"},
            {"op": "average", "field": "CTR", "as": "CTR"},
            {"op": "average", "field": "CPC", "as": "CPC"}
          ],
          "groupby": ["Date"]
        },
        {
          "calculate": "datum['Total Impressions']",
          "as": "Impressions_norm"
        },
        {
          "fold": [
            "Total Impressions",
            "Total Clicks",
            "Total Spend",
            "CTR",
            "CPC"
          ],
          "as": ["Measure", "Value"]
        },
        {
          "joinaggregate": [
            {
              "op": "max",
              "field": "Value",
              "as": "max_value"
            }
          ],
          "groupby": ["Measure"]
        },
        {
          "calculate": "datum.Value / datum.max_value",
          "as": "normalized_value"
        },
        {
          "calculate": "datum.Value",
          "as": "original_value"
        }
      ],
      "encoding": {
        "x": {
          "field": "Date",
          "type": "temporal",
          "title": "Date",
          "axis": {"grid": false}
        },
        "y": {
          "field": "normalized_value",
          "type": "quantitative",
          "title": "Normalized Scale (0 to 1)",
          "axis": {
            "grid": true,
            "format": ".0%",
            "title": null
          }
        },
        "color": {
          "field": "Measure",
          "type": "nominal",
          "title": "Measures",
          "scale": {"scheme": "category10"}
        },
        "tooltip": [
          {"field": "Date", "type": "temporal", "title": "Date"},
          {"field": "Measure", "type": "nominal", "title": "Measure"},
          {
            "field": "original_value",
            "type": "quantitative",
            "title": "Actual Value",
            "format": ",.0f"
          },
          {
            "field": "normalized_value",
            "type": "quantitative",
            "title": "Normalized",
            "format": ".1%"
          }
        ]
      },
      "layer": [
        {
          "mark": {
            "type": "line",
            "strokeWidth": 2,
            "interpolate": "monotone"
          }
        },
        {
          "selection": {
            "hover": {
              "type": "single",
              "on": "mouseover",
              "nearest": true,
              "empty": "none"
            }
          },
          "mark": {
            "type": "point",
            "size": 100,
            "opacity": 0
          }
        }
      ],
      "config": {
        "view": {"stroke": null},
        "axis": {
          "domainWidth": 1,
          "labelFontSize": 11,
          "titleFontSize": 13
        },
        "legend": {
          "titleFontSize": 12,
          "labelFontSize": 11,
          "symbolSize": 100
        }
      }
    }

     

    If you prefer a DAX solution create these measures:

    // Base Measures (you should already have these)
    Total Impressions = SUM(random_campaign_data[Impressions])
    Total Clicks = SUM(random_campaign_data[Clicks])
    Total Spend = SUM(random_campaign_data[Spend])
    Avg CTR = AVERAGE(random_campaign_data[CTR])
    Avg CPC = AVERAGE(random_campaign_data[CPC])
    
    // Normalized Measures
    Normalized Impressions = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Total Impressions])
    RETURN DIVIDE([Total Impressions], MaxValue, 0)
    
    Normalized Clicks = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Total Clicks])
    RETURN DIVIDE([Total Clicks], MaxValue, 0)
    
    Normalized Spend = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Total Spend])
    RETURN DIVIDE([Total Spend], MaxValue, 0)
    
    Normalized CTR = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Avg CTR])
    RETURN DIVIDE([Avg CTR], MaxValue, 0)
    
    Normalized CPC = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Avg CPC])
    RETURN DIVIDE([Avg CPC], MaxValue, 0)

     

    If you want to ask any questions just mention me ☺️❤️ 

    if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.
  • Hi eliasayyy ,

    I understand und you are right that the chart looks nicer if the range of the different measures is smaller. But I wouldn't recommend to do such a scaling except your visualisations purpose is just to look good. 

    Of course it looks nicer if the values are closer but the viewer of your report will think there are closer but obviously they are not. That is very misleading. 
    Other options that came to my mind:

    • Using a second y-axis. But I wouldn't recommend that either. The user will see values that are close but they are not.
    • Using a logarithmic scale. That could work but will depend on the experience of the users. Most people are not familiar with logarithmic. And once again, you will see values close to each other that are no close to each other
    • Using small multiples could really be an option. Define certain bins / classes of values. and use them to distribute your measures to these multiples. Depending on the range of their values the measures would be assigned to one of the multiples. If you choose a 1-column layout all visuals will have the same x-axis. So you can compare them
    • Using normalized data could be an option as well if you are focussed on the trends. If you choose the max of all values as 100% you haven't won anything. You would have to choose the max of every line as 100% for that line. Once again users will have the impressions that values are close that are not close. In this case, you should make it very clear what is shown.

    Hope that helps and that at least one of the options meets your requirements.

10 Replies

  • Hi eliasayyy,

    I hope you are doing well ☺️❤️

     

    So unfortunately I can't download the file but based on my understand you want to dynamically scale measures in a line chart so all lines are readable

    You can try this Deneb (Vega-Lite) that automatically normalizes your data:

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {"name": "dataset"},
      "transform": [
        {
          "aggregate": [
            {"op": "sum", "field": "Impressions", "as": "Total Impressions"},
            {"op": "sum", "field": "Clicks", "as": "Total Clicks"},
            {"op": "sum", "field": "Spend", "as": "Total Spend"},
            {"op": "average", "field": "CTR", "as": "CTR"},
            {"op": "average", "field": "CPC", "as": "CPC"}
          ],
          "groupby": ["Date"]
        },
        {
          "calculate": "datum['Total Impressions']",
          "as": "Impressions_norm"
        },
        {
          "fold": [
            "Total Impressions",
            "Total Clicks",
            "Total Spend",
            "CTR",
            "CPC"
          ],
          "as": ["Measure", "Value"]
        },
        {
          "joinaggregate": [
            {
              "op": "max",
              "field": "Value",
              "as": "max_value"
            }
          ],
          "groupby": ["Measure"]
        },
        {
          "calculate": "datum.Value / datum.max_value",
          "as": "normalized_value"
        },
        {
          "calculate": "datum.Value",
          "as": "original_value"
        }
      ],
      "encoding": {
        "x": {
          "field": "Date",
          "type": "temporal",
          "title": "Date",
          "axis": {"grid": false}
        },
        "y": {
          "field": "normalized_value",
          "type": "quantitative",
          "title": "Normalized Scale (0 to 1)",
          "axis": {
            "grid": true,
            "format": ".0%",
            "title": null
          }
        },
        "color": {
          "field": "Measure",
          "type": "nominal",
          "title": "Measures",
          "scale": {"scheme": "category10"}
        },
        "tooltip": [
          {"field": "Date", "type": "temporal", "title": "Date"},
          {"field": "Measure", "type": "nominal", "title": "Measure"},
          {
            "field": "original_value",
            "type": "quantitative",
            "title": "Actual Value",
            "format": ",.0f"
          },
          {
            "field": "normalized_value",
            "type": "quantitative",
            "title": "Normalized",
            "format": ".1%"
          }
        ]
      },
      "layer": [
        {
          "mark": {
            "type": "line",
            "strokeWidth": 2,
            "interpolate": "monotone"
          }
        },
        {
          "selection": {
            "hover": {
              "type": "single",
              "on": "mouseover",
              "nearest": true,
              "empty": "none"
            }
          },
          "mark": {
            "type": "point",
            "size": 100,
            "opacity": 0
          }
        }
      ],
      "config": {
        "view": {"stroke": null},
        "axis": {
          "domainWidth": 1,
          "labelFontSize": 11,
          "titleFontSize": 13
        },
        "legend": {
          "titleFontSize": 12,
          "labelFontSize": 11,
          "symbolSize": 100
        }
      }
    }

     

    If you prefer a DAX solution create these measures:

    // Base Measures (you should already have these)
    Total Impressions = SUM(random_campaign_data[Impressions])
    Total Clicks = SUM(random_campaign_data[Clicks])
    Total Spend = SUM(random_campaign_data[Spend])
    Avg CTR = AVERAGE(random_campaign_data[CTR])
    Avg CPC = AVERAGE(random_campaign_data[CPC])
    
    // Normalized Measures
    Normalized Impressions = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Total Impressions])
    RETURN DIVIDE([Total Impressions], MaxValue, 0)
    
    Normalized Clicks = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Total Clicks])
    RETURN DIVIDE([Total Clicks], MaxValue, 0)
    
    Normalized Spend = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Total Spend])
    RETURN DIVIDE([Total Spend], MaxValue, 0)
    
    Normalized CTR = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Avg CTR])
    RETURN DIVIDE([Avg CTR], MaxValue, 0)
    
    Normalized CPC = 
    VAR MaxValue = MAXX(ALLSELECTED('Date'[Date]), [Avg CPC])
    RETURN DIVIDE([Avg CPC], MaxValue, 0)

     

    If you want to ask any questions just mention me ☺️❤️ 

    if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.
    • v-menakakota's avatar
      v-menakakota
      Community Support

      Hi eliasayyy ,

      Thanks for reaching out to the Microsoft fabric community forum. 

       

      I would also take a moment to thank Ahmed-Elfeel  , for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

      I hope the above details help you fix the issue. If you still have any questions or need more help, feel free to reach out. We’re always here to support you 

       

      Best Regards, 
      Community Support Team

      • v-menakakota's avatar
        v-menakakota
        Community Support

        Hi eliasayyy ,

        I hope the above details help you fix the issue. If you still have any questions or need more help, feel free to reach out. We’re always here to support you 

         

        Best Regards, 
        Community Support Team

  • I believe this will help you

     

    https://www.sqlbi.com/articles/using-dax-to-control-a-chart-range-in-power-bi/

     

    In the above article/video SQLBI shows you how to create a measure that computes the maximum value of a measure regardless of any selection. You can then use that measure to define the maximum value of the Y-axis.

     

    Not sure this is exactly what you are looking for, but I believe might be a starting point

     

    Best

     

    If this helped, please consider giving kudos and mark as a solution

    me in replies or I'll lose your thread

    Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

    Consider voting this Power BI idea

    Francesco Bergamaschi

    MBA, M.Eng, M.Econ, Professor of BI

    • eliasayyy's avatar
      eliasayyy
      Memorable Member

      Thank you for your reply, Unfortunetly this only fixes max axis and min axis not the measure themselves as tehre is multiple measures with different values, i need to actually have a daynamic funtion to scale the selected measures, so lets say impressions on nov 1 2025 was 3000 while clicks was 100, the scaling is very bad on chart so i need to scale the clicks based on the max value of this row which is 3000 and make it close to it like 2000 so it looks better visually on the chart

      • FBergamaschi's avatar
        FBergamaschi
        Super User

        I understand and believe you need to extend that logic, calculating for each measure max and min and choose the value you want for the graph. Otherwise it is unclear to me how could Power BI understand what you want to get

         

        Best

        If this helped, please consider giving kudos and mark as a solution

        me in replies or I'll lose your thread

        Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

        Consider voting this Power BI idea

        Francesco Bergamaschi

        MBA, M.Eng, M.Econ, Professor of BI

  • Hi eliasayyy ,

    what do you exactly mean by "Scale the other measures to match with the biggest measure"? Could you give an example? In your example "Total impressions" is the "biggest" measure. How should the other measures be scaled for let's say 23 Nov? What exactly do you wanna show and compare? 

    • eliasayyy's avatar
      eliasayyy
      Memorable Member

      Thank you for your reply. my aim is to have the chart look nicer, so ltes ay on 23 november, we had 181k impressions while clicks had 6904, so maybe scale t to be 100k for example so the line chart will look more readable and not close to 0. here i just did a x10 on clicks just to show you 



      however i dont want a static x10 i need it dynamically to scale because a x10 if you see on nov 26 is now higher than impressions. my goal is just to make the chart readable and see trend of other measures because for now only the impression trend looks decent and tell a story

       

      • Hans-Georg_Puls's avatar
        Hans-Georg_Puls
        Super User

        Hi eliasayyy ,

        I understand und you are right that the chart looks nicer if the range of the different measures is smaller. But I wouldn't recommend to do such a scaling except your visualisations purpose is just to look good. 

        Of course it looks nicer if the values are closer but the viewer of your report will think there are closer but obviously they are not. That is very misleading. 
        Other options that came to my mind:

        • Using a second y-axis. But I wouldn't recommend that either. The user will see values that are close but they are not.
        • Using a logarithmic scale. That could work but will depend on the experience of the users. Most people are not familiar with logarithmic. And once again, you will see values close to each other that are no close to each other
        • Using small multiples could really be an option. Define certain bins / classes of values. and use them to distribute your measures to these multiples. Depending on the range of their values the measures would be assigned to one of the multiples. If you choose a 1-column layout all visuals will have the same x-axis. So you can compare them
        • Using normalized data could be an option as well if you are focussed on the trends. If you choose the max of all values as 100% you haven't won anything. You would have to choose the max of every line as 100% for that line. Once again users will have the impressions that values are close that are not close. In this case, you should make it very clear what is shown.

        Hope that helps and that at least one of the options meets your requirements.