Forum Discussion

shday98's avatar
shday98
Regular Visitor
3 years ago
Solved

Adding a column identifying if above or below average of table

Happy New Year "Get Help with Power BI"!   I'm using Excel Power Query to extract elapsed time and the amount of air firefighters are consuming from their air bottle and need to add a column that w...
  • nickvanmaele's avatar
    3 years ago

    Hi shday98 ,

     

    Here is a solution that will work in Excel. 

     

    On a worksheet, ensure that you have a Table (CTRL+T) like this one:

    NameAir Consumed
    Person A500
    Person B1000
    Person C1500
    Person D700

     

    Then, in Excel, click anywhere in the Table and choose "From Table/Range" in the Data ribbon.

    This will start up Power Query and create a query based on that table. 

     

    Open up the Advanced Editor of that query, and paste in the code below:

    Note: in the first line "Source = ...", replace "tblAirConsumption" in the code below by the name that your own table has in Excel. 

     

    let
    Source = Excel.CurrentWorkbook(){[Name="tblAirConsumption"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Air Consumed", Int64.Type}}),
    AverageAir = List.Average(#"Changed Type"[Air Consumed]),
    DifferenceWithAverage = Table.AddColumn(#"Changed Type","Performance vs average", each [Air Consumed] - AverageAir),
    AboveBelow = Table.AddColumn(DifferenceWithAverage, "Above or below average", each if [Air Consumed] > AverageAir then "Above avg" else "Below avg"),
    #"Changed Type1" = Table.TransformColumnTypes(AboveBelow,{{"Performance vs average", type number}, {"Above or below average", type text}})
    in
    #"Changed Type1"

     

    That ought to give you two additional columns that show if a person is above or below the average, and by how much. You can then look at it step by step to see how it all works. 

     

    If this answers your question, please mark this answer as the solution.