Forum Discussion
Question and Answer Field Values needed in One Row.
- 1 year ago
Hi JCortez,
Thank you for reaching out to the Microsoft fabric community forum.
use this M-Code in power query and solve the problemlet Source = Table.FromRows({ {"6138", "Admission Date", "5/23/2024", "_PAS 44", "5/23/2024", "TRUE"}, {"6138", "Program Number", "Pride (53069)", "_PAS 44", "5/23/2024", "TRUE"}, {"6138", "Admission Date", "12/30/2024", "_PAS 44", "12/23/2024", "TRUE"}, {"6138", "Program Number", "PCO (55)", "_PAS 44", "12/23/2024", "TRUE"} }, {"MRN", "Question", "Answer", "Assessment", "Date", "Signed"}), #"Changed Types" = Table.TransformColumnTypes(Source, { {"MRN", Int64.Type}, {"Question", type text}, {"Answer", type text}, {"Assessment", type text}, {"Date", type text}, {"Signed", type logical} }), #"Grouped Rows" = Table.Group(#"Changed Types", {"MRN", "Assessment", "Date", "Signed"}, { {"AllData", each _, type table} }), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "QA_Record", each let Data = [AllData], RowCount = Table.RowCount(Data), QA_Record = List.Accumulate( {0..RowCount-1}, [], (state, index) => let CurrentRow = Data{index}, QuestionCol = "Question" & Text.From(index + 1), AnswerCol = "Answer" & Text.From(index + 1), NewFields = Record.FromList( {CurrentRow[Question], CurrentRow[Answer]}, {QuestionCol, AnswerCol} ) in Record.Combine({state, NewFields}) ) in QA_Record ), AllQAColumns = List.Distinct( List.Combine( List.Transform( #"Added Custom"[QA_Record], each if _ <> null then Record.FieldNames(_) else {} ) ) ), #"Expanded QA_Record" = Table.ExpandRecordColumn(#"Added Custom", "QA_Record", AllQAColumns, AllQAColumns), #"Removed Columns" = Table.RemoveColumns(#"Expanded QA_Record", {"AllData"}) in #"Removed Columns"
I tested it with sample data, and it worked fine. Please find the attached screenshot and Pbix for your reference.
Hope this helps if you have any queries we are happy to assist you further.
Best Regards,
Harshitha.
Will this work if the fields are coming from several tables? This is where they are coming from:
'Client'[med_rec_no]
'ASSESS_QUES'[question]
'Client_Assess_Response'[response]
'Client_Assessment'[clas_name]
'Client_Assessment'[clas_date]
'Client_Assessment'[clas_signed]
Thank you for your assistance.
Hi JCortez,
Thank you for reaching out to the Microsoft fabric community forum.
use this M-Code in power query and solve the problem
let
Source = Table.FromRows({
{"6138", "Admission Date", "5/23/2024", "_PAS 44", "5/23/2024", "TRUE"},
{"6138", "Program Number", "Pride (53069)", "_PAS 44", "5/23/2024", "TRUE"},
{"6138", "Admission Date", "12/30/2024", "_PAS 44", "12/23/2024", "TRUE"},
{"6138", "Program Number", "PCO (55)", "_PAS 44", "12/23/2024", "TRUE"}
}, {"MRN", "Question", "Answer", "Assessment", "Date", "Signed"}),
#"Changed Types" = Table.TransformColumnTypes(Source, {
{"MRN", Int64.Type},
{"Question", type text},
{"Answer", type text},
{"Assessment", type text},
{"Date", type text},
{"Signed", type logical}
}),
#"Grouped Rows" = Table.Group(#"Changed Types", {"MRN", "Assessment", "Date", "Signed"}, {
{"AllData", each _, type table}
}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "QA_Record", each
let
Data = [AllData],
RowCount = Table.RowCount(Data),
QA_Record = List.Accumulate(
{0..RowCount-1},
[],
(state, index) =>
let
CurrentRow = Data{index},
QuestionCol = "Question" & Text.From(index + 1),
AnswerCol = "Answer" & Text.From(index + 1),
NewFields = Record.FromList(
{CurrentRow[Question], CurrentRow[Answer]},
{QuestionCol, AnswerCol}
)
in
Record.Combine({state, NewFields})
)
in
QA_Record
),
AllQAColumns = List.Distinct(
List.Combine(
List.Transform(
#"Added Custom"[QA_Record],
each if _ <> null then Record.FieldNames(_) else {}
)
)
),
#"Expanded QA_Record" = Table.ExpandRecordColumn(#"Added Custom", "QA_Record", AllQAColumns, AllQAColumns),
#"Removed Columns" = Table.RemoveColumns(#"Expanded QA_Record", {"AllData"})
in
#"Removed Columns"
I tested it with sample data, and it worked fine. Please find the attached screenshot and Pbix for your reference.
Hope this helps if you have any queries we are happy to assist you further.
Best Regards,
Harshitha.
- v-hjannapu1 year agoCommunity Support
Hi JCortez,
I wanted to follow up and see if you have had a chance to review the information that was shared. If you have any additional questions or need further clarification, please don’t hesitate to reach out. I am here to assist with any concerns you might have.
Regards,
Harshitha.