Search code examples
google-sheetsgoogle-query-language

Create Percentage Table from 2 other tables - Google Query


Is there a way to calculate a 3rd crosstab Table containing percentages from 2 crosstab tables created from Google Queries? Initially i was stacking the 2 queries together using braces, but found that if there were row or column mismatches, then both the table queries would result in #VALUE error. So separated the 2 tables with 100 grouped rows between them. I want to calculate FTR percentage table below these tables.

Here is an FTRTable with Edit access.

You can refer to my earlier related post here.


Solution

  • formula for FTR table:

    =ARRAYFORMULA(REGEXREPLACE(TO_TEXT(QUERY(QUERY({QUERY(
     {InputData!D3:D&"♦"&InputData!C3:C, InputData!B3:F, InputData!D3:D&
     IF(InputData!D3:D="",,"♥"&TEXT(InputData!D3:D, "dd/mm/yyyy"))}, 
     "select Col1,Col4,Col3,Col7,count(Col2) 
      where Col4 is not null 
        and year(Col4)="&C1&" 
        and Col5 != 'lost'     
      group by Col1,Col4,Col3,Col7"),
    IFNA(VLOOKUP(INDEX(QUERY({InputData!D3:D&"♦"&InputData!C3:C, InputData!B3:F}, 
     "select Col1,Col4,Col3,count(Col2) 
      where Col4 is not null 
        and year(Col4)="&C1&" 
        and Col5 != 'lost'     
      group by Col1,Col4,Col3"),,1), 
    QUERY({InputData!D3:D&"♦"&InputData!C3:C, InputData!B3:F}, 
     "select Col1,count(Col2) 
      where Col4 is not null 
        and year(Col4)="&C1&" 
        and Col5 != 'lost' 
        and Col6  = 'yes' 
      group by Col1"), 2, 0))}, 
     "select Col4,Col3,Col6/Col5"), 
     "select Col2,max(Col3) 
      group by Col2
      pivot Col1 
      label Col2'FTR%' 
      format max(Col3)'#.00%'")), "(\d+♥)", ))
    

    0


    and all 3 tables in one formula fully dynamic:

    =ARRAYFORMULA({TRANSPOSE(QUERY(TRANSPOSE(QUERY(IFNA(
     HLOOKUP(INDEX(TRANSPOSE(QUERY(InputData!A3:F, 
     "select D,count(B) 
      where D is not null 
        and year(D)="&C1&" 
        and E!='lost' 
      group by D 
      pivot C 
      label D 'NO OF ISSUES'")), 1), TRANSPOSE(QUERY({InputData!A3:F}, 
     "select Col4,count(Col2) 
      where Col4 is not null
        and year(Col4)="&C1&" 
        and Col5!='lost' 
        and Col6 = 'yes' 
      group by Col4 
      pivot Col3 
      label Col4 'NO OF ISSUES'")), ROW(A1:A10000), 0)), 
     "where Col1 <> '#REF!'")), 
     "format Col1'm/d/yyyy'")); 
     SPLIT(REPT(" ♠", COLUMNS(TRANSPOSE(QUERY(InputData!A3:F, 
     "select D,count(B) 
      where D is not null
        and year(D)="&C1&" 
        and E!='lost' 
      group by D 
      pivot C 
      label D 'TOTAL OPPORTUNITIES'")))), "♠"); 
     TRANSPOSE(QUERY(InputData!A3:F, 
     "select D,count(B) 
      where D is not null
        and year(D)="&C1&" 
        and E!='lost' 
      group by D 
      pivot C 
      label D 'TOTAL OPPORTUNITIES'")); 
     SPLIT(REPT(" ♠", COLUMNS(TRANSPOSE(QUERY(InputData!A3:F, 
     "select D,count(B) 
      where D is not null
        and year(D)="&C1&" 
        and E!='lost' 
      group by D 
      pivot C 
      label D 'TOTAL OPPORTUNITIES'")))), "♠");
     REGEXREPLACE(TO_TEXT(QUERY(QUERY({QUERY(
     {InputData!D3:D&"♦"&InputData!C3:C, InputData!B3:F, InputData!D3:D&
     IF(InputData!D3:D="",,"♥"&TEXT(InputData!D3:D, "m/d/yyyy"))}, 
     "select Col1,Col4,Col3,Col7,count(Col2) 
      where Col4 is not null 
        and year(Col4)="&C1&" 
        and Col5 != 'lost'     
      group by Col1,Col4,Col3,Col7"),
    IFNA(VLOOKUP(INDEX(QUERY({InputData!D3:D&"♦"&InputData!C3:C, InputData!B3:F}, 
     "select Col1,Col4,Col3,count(Col2) 
      where Col4 is not null 
        and year(Col4)="&C1&" 
        and Col5 != 'lost'     
      group by Col1,Col4,Col3"),,1), 
    QUERY({InputData!D3:D&"♦"&InputData!C3:C, InputData!B3:F}, 
     "select Col1,count(Col2) 
      where Col4 is not null 
        and year(Col4)="&C1&" 
        and Col5 != 'lost' 
        and Col6  = 'yes' 
      group by Col1"), 2, 0))}, 
     "select Col4,Col3,Col6/Col5"), 
     "select Col2,max(Col3) 
      group by Col2
      pivot Col1 
      label Col2'FTR%' 
      format max(Col3)'#.00%'")), "(\d+♥)", )})
    

    enter image description here

    spreadsheet demo