Forum Discussion

cookm01's avatar
cookm01
Regular Visitor
1 year ago
Solved

Creating two straight average lines on a bar chart

Hello,

 

I am currently trying to create two straight average lines for a bar chart, to represent the averages for two years on the chart.

 

The context (using dummy data): I have a bar chart which shows clients' costs as a % of income in 2023 and 2024. The bar chart is filtered to only show the data from 2024, but I want to have two lines that demonstrate the average from 2023 and 2024 for all customers going across this. The average is to be created as an aggregate (just adding them up and dividing by total number of clients). I currently get the average line on the bar chart but it is not straight (it is the average for each customer rather than the total year).

 

Please find attached dummy data and example of table without lines below.

 

TIA

 

 

Year Customer% of income 
2024Customer A10%
2024Customer B 40%
2024Customer C70%
2024Customer D20%
2024Customer E60%
2024Customer F50%
2024Customer G30%
2024Customer H50%
2024Customer I90%
2024Customer J80%
2023Customer A20%
2023Customer B 40%
2023Customer C60%
2023Customer D30%
2023Customer E80%
2023Customer F90%
2023Customer G50%
2023Customer H10%
2023Customer I40%
2023Customer J60%

 

 

 

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi cookm01 ,

    I create a table as you mentioned.

    Then I create a measure and here is the DAX code.

    Measure = 
    VAR _currentYear =
        MAX ( 'Table'[Year ] )
    RETURN
        CALCULATE (
            AVERAGE ( 'Table'[% of income ] ),
            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Year ] = _currentYear )
        )

    Finally you will see what you want.

     

     

    Best Regards

    Yilong Zhou

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • cookm01 

     

    Try these 

     

    2023 Avg = CALCULATE(AVERAGE('DataTable'[% of income ]), ALLEXCEPT('DataTable','DataTable'[% of income ]),'DataTable'[Year ] = 2023)

     

    2024 Avg = CALCULATE(AVERAGE('DataTable'[% of income ]), ALLEXCEPT('DataTable','DataTable'[% of income ]),'DataTable'[Year ] = 2024)

     

    regards

     

    Phil

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi cookm01 ,

    I create a table as you mentioned.

    Then I create a measure and here is the DAX code.

    Measure = 
    VAR _currentYear =
        MAX ( 'Table'[Year ] )
    RETURN
        CALCULATE (
            AVERAGE ( 'Table'[% of income ] ),
            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Year ] = _currentYear )
        )

    Finally you will see what you want.

     

     

    Best Regards

    Yilong Zhou

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • PhilipTreacy's avatar
      PhilipTreacy
      Super User

      Anonymous 

       

      Not sure this is exactly what the OP asked for so not sure why it's been marked as the solution.

       

      The bar chart is filtered to only show the data from 2024, but I want to have two lines that demonstrate the average from 2023 and 2024 for all customers going across this.

       

      Regards

       

      Phil