title: Is there a way to add a ending column to a pivoted dataframe?
body: <p>Let's say I have the next dataframe:</p>
  
  <pre><code>val df = spark.sparkContext.parallelize(Seq(
          ("A", "12", 50),
          ("A", "13", 100),
          ("A", "14", 30),
          ("B", "15", 40),
          ("C", "16", 60),
          ("C", "17", 70)
        )).toDF("Name", "Time", "Value")
  </code></pre>
  
  <p>and I pivot by 'Time':</p>
  
  <pre><code>val pivoted = df.groupBy($"Name").
      pivot("Time").
      agg(coalesce(sum($"Value"),lit(0)))
  
  pivoted.show()
  </code></pre>
  
  <p>which results in:</p>
  
  <pre><code>+----+----+----+----+----+----+----+
  |Name|  12|  13|  14|  15|  16|  17|
  +----+----+----+----+----+----+----+
  |   B|null|null|null|  40|null|null|
  |   C|null|null|null|null|  60|  70|
  |   A|  50| 100|  30|null|null|null|
  +----+----+----+----+----+----+----+
  </code></pre>
  
  <p>Until then, everything is ok. What I want, is to add a column next to 'column 17' calculating the sum of each row. So the expected output should be:</p>
  
  <pre><code>+----+----+----+----+----+----+----+----+
  |Name|  12|  13|  14|  15|  16|  17|sum |
  +----+----+----+----+----+----+----+----+
  |   B|null|null|null|  40|null|null|40  |
  |   C|null|null|null|null|  60|  70|130 |
  |   A|  50| 100|  30|null|null|null|180 |
  +----+----+----+----+----+----+----+----+
  </code></pre>
  
  <p>(Noobly, )I've tried adding a 'withColumn', but it fails:</p>
  
  <pre><code>val pivotedWithSummation = df.groupBy($"Name").
      pivot("Time").
      agg(coalesce(sum($"Value"),lit(0))).
      withColumn("summation", sum($"Value"))
  </code></pre>
  
  <p>I came with this <a href="https://stackoverflow.com/questions/54247831/multiple-aggregate-function-over-a-pivot-table">answer</a>, but I couldn't apply it :/ </p>
  
  <p>I'm using Scala v.2.11.8 and Spark 2.3.1</p>
  
  <p>Thanks in advance!</p>