Best way to handle Table headings and tables

I am trying to work out a way where a user can have multiple subheadings to build a table. For example, a user can work with this type of workflow.

Section header 
--Table Heading
   ---Table

OR

Section header 
--Table Heading
  --Table Subheading (optional) 
     ---Table

OR

  Section header 
    --Table Heading
      ---Table Subheading (*optional*, if user has more sub-sub heading, they might need more one more subheading)
         ----Table Sub-subheading
            -----Table

What is the best way to determine if the user will require that subheading and then allow them to enter the information in a table?