Forum Discussion
Quiz score via Flow?
LucianC , let play with such sample
Quiz table is what the flow updates. Answers is the supporting page with points. Final result is in green.
In my case each of above tables is in separate file - answers are separated for the security reason. Source file is separated to avoid any conflicts with flow since that file is practically never opened in opposite to file with results. Most probably it shall be no conflicts even they are in one file, but who know.
First query supporting table and only one transformation is to add the key for Q&A.
let
Source = Excel.CurrentWorkbook(){[Name="Points"]}[Content],
ProperType = Table.TransformColumnTypes(Source,{
{"Question", type text},
{"Answer", type text},
{"Points", Int64.Type}}),
// This is to generate unique key for the combination of Question & Answer
AddKey = Table.AddColumn(ProperType, "QAKey", each [Question] & "===" & [Answer])
in
AddKey
Query the quiz table, select all columns but questions, unpivot other columns and add the same key for each combination of Q&A. Merge on this key with Points query (into same or separate table) extracting only points. With that GroupBy on all columns except points, aggregate them by sum.
let
Source = Excel.CurrentWorkbook(){[Name="Quiz"]}[Content],
ProperType = Table.TransformColumnTypes(Source,{
{"Time", type datetime},
{"Name", type text},
{"email", type text}, {"Q1", type text},
{"Q2", type text}, {"Q3", type text}}),
UnpivotQuestions = Table.UnpivotOtherColumns(ProperType, {"Time", "Name", "email"}, "Attribute", "Value"),
AddKey = Table.AddColumn(UnpivotQuestions, "QAkey", each [Attribute] & "===" & [Value]),
MergeWithAnswers = Table.NestedJoin(AddKey,
{"QAkey"}, Points,
{"QAKey"}, "Points",
JoinKind.LeftOuter),
GetPoints = Table.ExpandTableColumn(MergeWithAnswers, "Points", {"Points"}, {"Points.1"}),
RemoveUnuzed = Table.SelectColumns(GetPoints,{"Time", "Name", "email", "Points.1"}),
//Use Advanced option to GroupBy
SumPoints = Table.Group(RemoveUnuzed,
{"Time", "Name", "email"},
{{"Points", each List.Sum([Points.1]), type number}})
in
SumPoints
That's all, the rest is cosmetic.
One more practical point. If you change wording of the question(s) in your form, or even name(s) of the column in Excel table, the flow doesn't care, it will add results correctly based on order of question/columns. Text with answers will be returned by flow as it is. However, both could affect the key as it is generated in this sample. Correction of wording always could happen, even extra space crashes the key.
As variant you may play with texts (trim, lowercase, etc), bt that's not very reliable. Better to work with indexes. For the questions they could be generated internally - sequential numbers in supporting table and pick-up number of columns in quiz query.
With answers is more complex, we can't pick-up any ID for them. Thus we use in Forms numbering list of answers, like
Is England part of EU? a) Yes b) No
with that you extract only first character of each answer, doesn't matter which text will be after that, and work with it.
But that's all improvements and more depends on your concrete data.
You may play with attached file to check how quires work.
SergeiBaklan - Thank you, this is so helpful and I was able to set it up! I am new to queries and am going one step further. I have multiple quizzes that will be dumping answers into a single Excel tab with the quiz name on it. Is there any way to direct the quiz answers added by flow into the correct answers table based on the quiz name or do I have to create a separate tab for each quiz flow? I attached a sample of what I'm trying to do.
- JgoetschelNov 14, 2022Copper Contributor
Unless I missed something looks like YEARS later and still no changes....