I have three tables:
Product
ProductID ProductName
1 Cycle
2 Scooter
3 Car
<
We can create matrix using pivot, this can easily done with data frames
product|Key|Value
A |P |10|
A |Q |40|
B |R |50|
B |S |50|
val newdf=df.groupBy("product").pivot("key").sum("value")
|product|P |Q |R |S |
|B |null|null| 50| 50|
|A | 10| 40|null|null|
We can replace null and we can do calculations as well