Reshaping your data easily – Case study [Pivot tables FTW]

Share

Facebook
Twitter
LinkedIn

Late. Jayaram, my uncle is also a teacher. When I was a kid, I used to spend a lot of time with him, learning all sorts of things. He taught me chess, maths and so many life lessons. I remember one such lesson very vividly.  One day, he asked me to do something. I did it in a very long way. After seeing me struggle for several minutes, he chipped in and showed me how to do it easily. He then said, “when someone asks you where your nose is, you don’t twist arm around your head. You just point to your nose directly.”

show-me-your-nose

The idea is that when you have a direct, simple way to do something, you should use it.

Nose and pivot tables… how are they connected?

We are coming to the point. Recently, learneagerly, one of our forum users asked a question about how to transform (reshape) a set of data in Excel.

Marc L, one of our Excel ninjas, posted an awesome VBA script to do that.

Later in the day, I chipped in and shared a formula approach to transform the data.

I suggest checking out both approaches for learning more about VBA & INDEX formula respectively.

After posting my answer, I got thinking… May be there is a more direct way to reshape the data.

Why, yes, there is. You can use Pivot Tables.

Let’s take a look at the data & problem first

Here is a snapshot of raw data and expected output.

data-and-expected-output-reshaping-with-pivots

As you can see, we have two columns of data and we need to extract n (here it is 6) items from first column, then from second column and lay them out in output. We repeat this until we run out of the data.

Reshape this thing with a Pivot

raw-data-reshape-with-pivots-v1The first step is to add two extra columns to your raw data. Let’s call them Running & Repeat.

  • Running: with numbers 1 thru 6 and repeat the pattern (just auto fill or copy paste)
  • Repeat: with 6 cells of 1, 6 cells of 2… and repeat this pattern (auto-fill)

But what if I want a different n

Even better. use formulas. Let’s say your data starts from H6:I6

  • Running:  =MOD(ROWS($I$6:I6)-1,n)+1
  • Repeat:  =IF(J6=1,SUM(K5,1),K5)

Related: Using ROWS() to generate running numbers in Excel.

Now that we have these extra columns, select all the data (2 columns of data + 2 extra columns we just added) and insert a pivot table.

Set up the report by,

  • Adding Repeat & Running to row labels area (in that order)
  • Add Col A & Col B to values area.
  • Move the ? values to row labels area (by dragging it)
  • Position ? values between Repeat & Running row labels.
  • Your pivot report’s last column will have the transformed data.
  • Viola, nose pointed!

pivot-table-ftw

 

Download Example Workbook

Here is the example workbook. Examine the pivot table & formulas in Running & Repeat columns to learn more.

Get your Excel muscles in to shape

Are you struggling to find your nose or worse still, twisting your arm on the way? If so, check out our Excel school program. We have awesome online lessons, beautiful explanations, powerful techniques and easy to understand downloads. It won’t be long before you are smelling roses.

Check out our Excel School online class & join today.

How do you reshape your data?

Pivot Tables and Power Query are my go to tools for almost all kinds of reshaping problems. Often, I indulge in INDEX formulas or a bit of VBA. For example, just a few days ago, I had to split first 100,000 digits of Pi ? in chunks of 3 digits, 3 digits and 14 digits in a pattern. As the data is too long, loading it Excel cell was impractical. Loading it in to multiple lines with each having digits was impractical (as I may need to split them in another pattern). So I used a simple VBA script to zap the data and get what I need.

In case you are curious: I made a chart to celebrate the Pi day (14th of March) with our community on Twitter.

But when I am not splitting irrational stuff, I usually rely on Pivot tables or PQ.

What about you? How do you reshape your data? Please share your approaches and tips in the comments section.

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

13 Responses to “Convert fractional Excel time to hours & minutes [Quick tip]”

  1. Debraj Roy says:

    Hi Purna..

    Again a great tip.. Its a great way to convert Fractional Time..
    By the way.. Excel has two great and rarely used formula..

    =DOLLARFR(7.8,60) and =DOLLARDE(7.48,60)

    basically US Account person uses those to convert some currency denomination.. and we can use it to convert Year(i.e 3.11 Year = 3 year 11 month) and Week(6.5 week = 6 week 5 days), in the same manner...

  2. Jason says:

    This doesn't work for me. When applying the custom format of [h]:mm to 7.8 I get 187:12

    Any ideas why?

    • Hui... says:

      @Jason
      7.8 in Excel talk means 7.8 days
      =7.8*24
      =187.2 Hrs
      =187 Hrs 12 Mins

      If you follow Chandoo's instructions you will see that he divides the 7.8 by 24 to get it to a fraction of a day

      Simple, assuming the fractional time is in cell A1,

      Use below steps to convert it to hours & minutes:

      1. In the target cell, write =A1/24
      2. Select the target cell and press CTRL+1 to format it (you can also right click and select format cells)
      3. Select Custom from “Number” tab and enter the code [h]:mm
      4. Done!

  3. WhoKnows says:

    Hi, sorry to point this out but Column C Header is misspelt 'Hours Palyed'

  4. abhishek malik says:

    good one

  5. Julia says:

    So how do I go the other way and get hours and minutes to fractional time?

    • Chandoo says:

      If you have 7.5 in cell A1,

      - Use int(A1) to get the hours.
      - Use mod(A1,1)*60 to get minutes.

      If you have 7:30 (formatted as time) in A1

      - Use hours(a1) to get hours
      - Use minutes(a1) to get minutes.

      • Paula says:

        I had the same issue. You can solve it by changing the format as described above:

        Right click cell > Format Cells > (In Number tab) > Custom > Then enter the code [h]:mm
        ([hh]:mm and [hhh]:mm are nice too if you want to show leading zeros)

        • Jack Scarce says:

          Thanks guys, these are the tips I'm looking for.
          ...dividing the number of minutes elapsed by the percent change is my task - "int" is the key this time

  6. Srikanth says:

    It doesnt work for greater than 24 hours
    It returns 1:30 for 25.5 hours. It should have returned 25:30

    Ideally I would right function as
    =QUOTIENT(A1,1)&":"&MOD(A1,1)*60

    • Paula says:

      Sorry, replied to wrong comment....

      ----
      I had the same issue. You can solve it by changing the format as described above:

      Right click cell > Format Cells > (In Number tab) > Custom > Then enter the code [h]:mm
      ([hh]:mm and [hhh]:mm are nice too if you want to show leading zeros)

  7. Daniel says:

    Clever use of MOD here to extract the decimal part of a number. Divide a number containing a decimal by 1 and return the remainder. Humm. Very clever.

  8. Tomer says:

    Thanks very much, extremely useful !

Leave a Reply