Pivoting rows to columns
When preparing data that has been generated by transactional systems, you may encounter data structures that appear nonsensical from a reporting and analytics perspective. Take a sales order as an example. A sale may be for one or multiple items and the total sales amount may be affected by things such as a loyalty card, discount, referral code, and of course sales tax. Depending on which system you are working with, such information may be reported separately, that is, in columns. However, it's quite likely to see multiple rows in your dataset for the same transaction. In this recipe, we'll look at pivoting data from rows to columns, which will resolve any issues arising from such a data structure. Broadly, these steps are similar to pivoting columns to rows, with some important differences, as we'll see.
Getting ready
To follow along with this recipe, download the Sample Files 6.3 folder from this book's GitHub repository.