SSRS 2005 find name of column with max value

The column on the far right is what I’m trying to add to my report. Is this possible to do without modifying the query to use something like Unpivot?

Step X      Step W      Step A     Step B     Step Y     Last Step
1/21/2013   1/24/2013   1/3/2013   1/5/2013   1/7/2013   Step W

This is a step in the right direction, but it appears to only work in SSRS 2008:

  • Update a table using CTE and NEWID()
  • Rewriting correlated subquery as JOIN?
  • Arithmetic overflow error converting expression to data type datetime
  • Need to convert Text field to Varchar temporarily so that I can pass to a stored procedure
  • SQL comma-separated row with Group By clause
  • Impersonate remote user in to access MS SQL Server
  • One Solution collect form web for “SSRS 2005 find name of column with max value”

    You can use the UNPIVOT function and a CROSS APPLY to get this:

    ;with cte as
      select col, value
      from yourtable
        for col in ([Step X], [Step W], [Step A], [Step B], [Step Y])
      ) unpiv
    select [Step X], 
      [Step W], 
      [Step A], 
      [Step B], 
      [Step Y],
      d.col LastStep
    from yourtable
    cross apply
      select c1.col
      from cte c1
      inner join
        select max(value) MaxDate
        from cte
      ) c2
        on c1.value = c2.maxdate
    ) d

    See SQL Fiddle with Demo

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.