excelexcel-formulaarray-formulasexcel-tables

How to create an excel formula using LET and other dynamic array functions which gives an calculated output of each column?


I have a table like this which stores marks of students in different subjects table which records marks of students

Name Class ID Math English Hindi
Tony 5 1 39 30 30
Andrew 5 2 40 20 20
Mark 5 3 40 10 30

and a tabel like this which sets the maximum marks of the subject table which stores max marks

Subject Max Marks
Math 40
English 30
Hindi 30

I want to create a excel formula that will return this by calculating the percentage of marks obtained in each subject and grade it accordingly and arranges the data to this calculated table

and could generate a excel function using AI which is =LET( Data, Table1, Headers, {"Name","Class","ID","Math","Math Grade","English","English Grade","Hindi","Hindi Grade"}, GradeThresholds, Table2, GetMaxMarks, LAMBDA(Subject, IFERROR(INDEX(Table2[Max Marks], MATCH(Subject, Table2[Subject], 0)), "Subject Not Found") ), Grades, LAMBDA(Score,MaxMarks, IFS( Score / MaxMarks >= 0.9, "A", Score / MaxMarks >= 0.75, "B", Score / MaxMarks >= 0.6, "C", Score / MaxMarks >= 0.5, "D", TRUE, "F" ) ), MathMaxMarks, GetMaxMarks("Math"), EnglishMaxMarks, GetMaxMarks("English"), HindiMaxMarks, GetMaxMarks("Hindi"), MathGrades, Grades(INDEX(Data,,MATCH("Math",Table1[#Headers],0)), MathMaxMarks), EnglishGrades, Grades(INDEX(Data,,MATCH("English",Table1[#Headers],0)), EnglishMaxMarks), HindiGrades, Grades(INDEX(Data,,MATCH("Hindi",Table1[#Headers],0)), HindiMaxMarks), ExpandedTable, HSTACK( INDEX(Data,,MATCH("Name",Table1[#Headers],0)), INDEX(Data,,MATCH("Class",Table1[#Headers],0)), INDEX(Data,,MATCH("ID",Table1[#Headers],0)), INDEX(Data,,MATCH("Math",Table1[#Headers],0)), MathGrades, INDEX(Data,,MATCH("English",Table1[#Headers],0)), EnglishGrades, INDEX(Data,,MATCH("Hindi",Table1[#Headers],0)), HindiGrades ), VSTACK(Headers, ExpandedTable) )

which works perfectly, but i want the formula to be dynamic and adjust itself as to i will be adding new subjects to the tables and don't want to hardcode the subject names into the formula each time I add a new subject.


Solution

  • I created this solution for you:

    enter image description here

    [A14]=LET(
      marks,LAMBDA(sc,subj,
        LET(r,sc/XLOOKUP(subj,Table2[Subject],Table2[Max Marks]),
          IF(r<0.5,"F",CHAR(71-INT(20*r/3)))
        )
      ),
      REDUCE(
        Table1[[#All],[Name]:[ID]],
        SEQUENCE(1,COLUMNS(Table1)-3,4),
        LAMBDA(a,v,
          HSTACK(a,
            CHOOSECOLS(Table1[#All],v),
            VSTACK(INDEX(Table1[#All],1,v)&" Grade",
              marks(CHOOSECOLS(Table1,v),INDEX(Table1[#All],1,v))
            )
          )
        )
      )
    )
    

    Where Table1 is the first table, Table2 is the second table. You can easily add new rows and columns in Table1 and new rows in Table2 without formula modification.