Forum Discussion
Creating logic for a Correct or Incorrect Flag in table
- Anonymous2 years ago
Hi jakaihammuda ,
Since you can be sure that all questions are objective, we can use a very simple way to complete this question. I recreated the test data set:I assume that the questions are single choice, unordered multiple choice, judgment, sorting on two items and sorting on three items.
Then as before, New Source -> Blank Query:And put all of the M function into the Advanced Editor:
let Source = Table.NestedJoin(FACT, {"QuestionID"}, DIM, {"QuestionID"}, "DIM", JoinKind.FullOuter), #"Expanded DIM" = Table.ExpandTableColumn(Source, "DIM", {"CorrectAnswers", "IsOrder"}, {"CorrectAnswers", "IsOrder"}), #"Added Custom" = Table.AddColumn(#"Expanded DIM", "Custom", each if [IsOrder] = 0 then if [Answer] = [CorrectAnswers] then "Right" else if Text.Contains([CorrectAnswers], [Answer]) and not Text.Contains([Answer], [CorrectAnswers]) then "Lack of other answer" else "Wrong" else if [Answer] = [CorrectAnswers] then "Right" else if Text.Length([Answer]) = Text.Length([CorrectAnswers]) then "Wrong order" else "Lack of other answer"), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"IsOrder", "CorrectAnswers"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"UserID", "Answer", "Custom", "QuestionID"}) in #"Reordered Columns"The final output is as below:
Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
just as a fyi. I replaced the ";" with "&" like so
Hi jakaihammuda ,
It's almost the end of the week, I'll try to keep testing it as you requested and I'll reply in time if I have another better solution!
Best Regards,
Dino Tao
- jakaihammuda2 years agoHelper III
Hi Anonymous
Much appreciated thank you very much! I look forward to you response 🙂
Take care in the meantime!Jakai
- Anonymous2 years agoNot applicable
Hi jakaihammuda ,
Back for work!
First of all on the issue of separators, whether you choose to use "," or ";" or "&" are not relevant, you just need to modify the code in this place:Note, however, that as far as possible, only one separator is used in this column, and it may be troublesome to use multiple separators, such as both commas and semicolons.
Also based on this screenshot of yours, I see that you have spaces between the & and the character, so I'm afraid you need to count the two spaces before and after the & as separators as well.Then regarding the order issue, my suggestion is that you can add another new column Isorder in the DIM table and mark it as 1 if there is an order issue with the answers to the current question, otherwise mark it as null. as shown below:
In the FACT table I added some sample data:
Then create a new Blank Query and put all of the M function into the Advanced Editor:
let Source = Table.NestedJoin(FACT, {"QuestionID"}, DIM, {"QuestionID"}, "DIM", JoinKind.FullOuter), #"Expanded DIM" = Table.ExpandTableColumn(Source, "DIM", {"keyword", "Isorder"}, {"keyword", "Isorder"}), CustomCheck = Table.AddColumn(#"Expanded DIM", "Answer Check", each let AnswerLower = Text.Lower([Answer]), lowerKeyword = Text.Lower([keyword]), KeywordsList = Text.Split(lowerKeyword, ","), CheckList = List.Transform(KeywordsList, each Text.Contains(AnswerLower, Text.Trim(Text.Lower(_)))), AllKeywords = List.AllTrue(CheckList), AnyKeywords = List.AnyTrue(CheckList) in if [Isorder] = 1 then let CheckAnswer = (QuestionID as number, keyword as text, Answer as text) => let keywordList = Text.Split(lowerKeyword, ","), checkOrder = List.Accumulate( keywordList, [FoundAll = true, LastIndex = 0, WrongOrder = false], (state, currentKeyword) => let currentIndex = Text.PositionOf(AnswerLower, currentKeyword, Occurrence.First), isCurrentFound = currentIndex <> -1, isNewIndexBigger = currentIndex > state[LastIndex], isWrongOrder = state[WrongOrder] or (isCurrentFound and not isNewIndexBigger), hasFoundAllSoFar = state[FoundAll] and isCurrentFound in [ FoundAll = hasFoundAllSoFar, LastIndex = if isCurrentFound then currentIndex else state[LastIndex], WrongOrder = isWrongOrder ] ), result = if not checkOrder[FoundAll] then "Lack of other answers" else if checkOrder[WrongOrder] then "wrong order" else "right" in result in CheckAnswer([QuestionID], [keyword], [Answer]) else if AllKeywords then "Right" else if AnyKeywords then "Lack of other answers" else "Wrong"), FinalTable = Table.RemoveColumns(CustomCheck,{"keyword"}) in FinalTableAnd the final output is as below:
Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.- jakaihammuda2 years agoHelper III
Hi, Anonymous
Thank you so much for helping on this!
I think im almost there now but just the one issue regarding the "keywords", this isnt optimal for me to use based on the fact that some of the keywords may appear in some other question answers that are wrong. Its best if it soley do it on the correct answer as opposed to keywords.
This is my structure.
FACT:
Contains the user, results ID, time, Answers and Question ID
DIM table (Correct Answer):DIM table (Question)
I still want that logic or Correct, partially correct, Incorrect that youve created but i just cant base it off keywords 😞 the partially correct logic can still apply right? using the "&" adn checking on words before/after that to see if its a match with any in the actual correct answer?Is much amending needed to be done to just base it off the actual corrcet asnwer only?