google-sheetsgoogle-sheets-formulapivot-tablearray-formulas

Is there any way to generate a pivot table only using formulas?


I got stuck on a task to generate a pivot table in Google Sheets without actually using a 'Pivot table' tool.

Export file with raw data:

raw data

The goal is to generate a sheet based on this export file, where all the tasks will be grouped by the project, and to calculate time totals for every project.

My goal is this:

goal

The problem is that the number of projects and tasks is not constant, so the number of rows for every project may vary.

I know that there's a 100% working solution to generate a Pivot table using the native tool, but my goal is to do that by using only formulas or script. Is there any way?

Here's the actual sheet https://docs.google.com/spreadsheets/d/1tfeGbOOe7KVa0E4OlkZ9PMhM6xGoXaQVx4Yx7sU3qZU/edit?usp=sharing


Solution

  • =ARRAYFORMULA({SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY("♠"&SORT(UNIQUE(FILTER(A2:A, A2:A<>"")))&
     TRANSPOSE(QUERY(TRANSPOSE(IF(LEN(QUERY(TRANSPOSE(QUERY(QUERY(A2:C,
     "select B,sum(C) where B<>'' group by B pivot A", 0), "offset 1", 0)), "offset 1", 0)), 
     "♠ ♦"&TRANSPOSE(SORT(UNIQUE(FILTER(B2:B, B2:B<>""))))&"♦"&QUERY(TRANSPOSE(QUERY(QUERY(A2:C,
     "select B,sum(C) where B<>'' group by B pivot A", 0), "offset 1", 0)), "offset 1", 0), ))
     , , 999^99))&"♠ ♦Total:♦"&MMULT(QUERY(TRANSPOSE(QUERY(QUERY(A2:C,
     "select B,sum(C) where B<>'' group by B pivot A", 0), "offset 1", 0)), "offset 1", 0)*1,
     TRANSPOSE(SPLIT(REPT(COUNTUNIQUE(B2:B)&"♦", COUNTUNIQUE(B2:B)), "♦")^0)), , 999^99)), 
     "♠")), "♦"); "", "Total:", SUM(C2:C)})
    

    0


    compact:

    =INDEX(LET(a, A2:A, b, B2:B, r, A2:C, o, "offset 1", t, "Total:", u, COUNTUNIQUE(b), 
     q, QUERY(TRANSPOSE(QUERY(QUERY(r, "select B,sum(C) where B<>'' group by B pivot A", ), o, )), o, ), 
    {SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY("♠"&SORT(UNIQUE(FILTER(a, a<>"")))&
     TOCOL(QUERY(TRANSPOSE(IF(LEN(q), "♠ ♦"&
     TRANSPOSE(SORT(UNIQUE(FILTER(b, b<>""))))&"♦"&q, )),, 9^9))&"♠ ♦"&t&"♦"&MMULT(q*1,
     TRANSPOSE(SPLIT(REPT(u&"♦", u), "♦")^0)),, 9^9)), "♠")), "♦"); "", t, SUM(C2:C)}))
    

    enter image description here