Merging and formating Excel Worksheets with power query - excel

I'm trying to format an excel file using Power Query in order to be able to pivot it, but I haven't been able to do it right.
I'm merging many worksheets from different workbooks into one. Every sheet has the data of the specific workbook in the first 3 rows (column 1= Title; and Column 2= Value), and then I have the table well formatted starting in row 5 (with headers and all, but different amount of rows each)
How can I transform the data in the first 3 rows of every sheet into columns, so I can get a Pivotable table?
Here an example of what I get when merging 2 files.

Based on the picture I assume you would like to transform such kind of file
to something like that
I used the following M-Code for that
let
Source = Table.FromColumns({Lines.FromBinary(File.Contents("D:\tmp\Files\file1.txt"), null, null, 1252)}),
#"Kept First Rows" = Table.FirstN(Source,3),
#"Split Column by Delimiter" = Table.SplitColumn(#"Kept First Rows", "Column1", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Col1", "Col2"}),
step = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Col1", type text}, {"Col2", type text}}),
#"Transposed Table" = Table.Transpose(step),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
Hdr = Table.TransformColumnTypes(#"Promoted Headers",{{"Nummer", type text}, {"Id", type text}}),
Custom1 = Source,
#"Removed Top Rows" = Table.Skip(Custom1,4),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Removed Top Rows", "Column1", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}, {"Column1.4", type text}}),
#"Promoted Headers1" = Table.PromoteHeaders(#"Changed Type1", [PromoteAllScalars=true]),
#"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers1",{{"F1", Int64.Type}, {"F2", Int64.Type}, {"F3", Int64.Type}, {"F4", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type2", "Nummer", each Hdr[Nummer]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "ID", each Hdr[Id]),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Export", each Hdr[Export]),
#"Expanded Export" = Table.ExpandListColumn(#"Added Custom2", "Export"),
#"Expanded ID" = Table.ExpandListColumn(#"Expanded Export", "ID"),
#"Expanded Nummer" = Table.ExpandListColumn(#"Expanded ID", "Nummer"),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Nummer",{{"Export", type date}})
in
#"Changed Type"

Related

Excel Unpivot Multiple Columns to a Single Column

What I want to achieve:
As the title says, is there any way to convert my table structure? I've tried using Power Query but it didn't work. Any kind of help is greatly appreciated. Thanks!
So, I was trying to pivot type and type value, but it seems impossible if I maintain the current table structure since it would cause duplicates when I wanted to aggregate on type.
Should I remake the table structure or there is any way to get around this problem?
Thanks in advance!
In Power Query, the following is adaptable to any number of type/value column pairs.
Unpivot all except the ID column
Add a custom column to define if the unpivoted value is a Type or a Type Value
Add an Index column and then do an integer/divide by 2 so things will sort in the desired order
Pivot with no aggregation, using a custom function as the "built-in" function will error with multiple items.
Custom function to Pivot with No aggregation
Rename as noted in comments
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
//Rename: fnPivotAll
(Source as table,
ColToPivot as text,
ColForValues as text)=>
let
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"
Regular Query
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"ID", type text}, {"Type 1", type text}, {"Type 1 Value", Int64.Type}, {"Type 2", type text}, {"Type 2 Value", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
#"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom",
each if Text.EndsWith([Attribute],"Value") then "Value" else "Type"),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute"}),
#"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),
#"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "Integer-Division", each Number.IntegerDivide([Index], 2), Int64.Type),
#"Removed Columns1" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),
Pivot = fnPivotAll(#"Removed Columns1","Custom","Value"),
#"Removed Columns2" = Table.RemoveColumns(Pivot,{"Integer-Division"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns2",{{"Type", type text}, {"Value", Int64.Type}})
in
#"Changed Type1"
More generically to stack vertically in powerquery while keeping certain columns
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
base_columns=1, groupsof=2, //stack them
Combo = List.Transform(List.Split(List.Skip(Table.ColumnNames(Source),base_columns),groupsof), each List.FirstN(Table.ColumnNames(Source),base_columns) & _),
#"Added Custom" =List.Accumulate(Combo, #table({"Column1"}, {}),(state,current)=> state & Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, current)),1)),
#"Rename"=Table.RenameColumns(#"Added Custom",List.Zip({Table.ColumnNames(#"Added Custom"),List.FirstN(Table.ColumnNames(Source),base_columns+groupsof)}))
in #"Rename"
What seems to be fastest method of those I've tested
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
leading=1, groupsof=2,
#"Added Custom" = Table.AddColumn(Source, "Custom", each List.Split( List.RemoveFirstN(Record.ToList( _),leading), groupsof) ),
#"Added Custom0" = Table.AddColumn(#"Added Custom", "Custom0", each Text.Combine(List.FirstN(Record.ToList(_),leading),"|")),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom0",{"Custom0", "Custom"}),
#"Expanded Custom" = Table.ExpandListColumn( #"Removed Other Columns", "Custom"),
#"Extracted Values" = Table.TransformColumns(#"Expanded Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), "|"), type text}),
#"Merged Columns" = Table.CombineColumns(#"Extracted Values",{"Custom0", "Custom"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Custom"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Merged Columns", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), List.FirstN(Table.ColumnNames(Source),leading+groupsof))
in #"Split Column by Delimiter"
There is probably a better way, but if you first concat the 4 columns with specific unique delimiter to split on later in a custom column, you have a work-around in PQ:
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Type1", type text}, {"Type1 Val", Int64.Type}, {"Type2", type text}, {"Type2 Val", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom1", each [Type1]&"|"&Number.ToText([Type1 Val])&"$"&[Type2]&"|"&Number.ToText([Type2 Val])),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Type1", "Type1 Val", "Type2", "Type2 Val"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Removed Columns", {{"Custom1", Splitter.SplitTextByDelimiter("$", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Custom1"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Custom1", type text}}),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "Custom1", Splitter.SplitTextByEachDelimiter({"|"}, QuoteStyle.Csv, false), {"Custom1.1", "Custom1.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Custom1.1", type text}, {"Custom1.2", Int64.Type}})
in
#"Changed Type2"
Just in case you tagged 'Excel-Formula' and you have access to ms365:
Formula in H1:
=REDUCE({"ID","Type","Val"},ROW(A2:A5),LAMBDA(X,Y,VSTACK(X,INDEX(A:E,Y,{1,2,3}),INDEX(A:E,Y,{1,4,5}))))
Or formula:
=SORT(VSTACK(A2:C5,HSTACK(A2:A5,D2:E5)))

Column rows to column headers in Power Query

I have a data set where I would like to have a table with Unique IDs in one Column A and from Column B the rows from a "Input" table above with a different rows as a column headers. In Column A are IDs (unique) and Column B has different rows that have to be in columns but matching values on the rest of the columns.. see on screenshot.
Third Column C is a just observational column that gives info what kind of data type should be there (it can be avoided in this case).
I though I was going to solve it "easily" with Pivot/Unpivot+Transponse method in Power Query but no way....I can get it in one row like in "Output" table..
The dummy data is in link below.
https://docs.google.com/spreadsheets/d/1qKeVj9nJF1usBk-OUZPJfpRqSQnOTCvr/edit?usp=sharing&ouid=101738555398870704584&rtpof=true&sd=true
Merge the value columns into a single column before the pivot, eg
let
Source = Excel.Workbook(File.Contents("C:\Users\david\Downloads\Test1.xlsx"), null, false),
Sheet1_sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
FilterNullAndWhitespace = each List.Select(_, each _ <> null and (not (_ is text) or Text.Trim(_) <> "")),
#"Added Custom" = Table.AddColumn(Sheet1_sheet, "IsEmptyRow", each try List.IsEmpty(FilterNullAndWhitespace(Record.FieldValues(_))) otherwise false),
#"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", -1),
#"Added Custom1" = Table.AddColumn(#"Added Index", "Section", each if [IsEmptyRow] then -1 else if try #"Added Index"[IsEmptyRow]{[Index]} otherwise true then [Index] else null),
#"Removed Blank Rows" = Table.SelectRows(#"Added Custom1", each not [IsEmptyRow]),
#"Filled Down" = Table.FillDown(#"Removed Blank Rows", {"Section"}),
#"Grouped Rows" = Table.Group(#"Filled Down", {"Section"}, {{"Rows", each _}}, GroupKind.Local),
#"Selected Group" = #"Grouped Rows"[Rows]{1},
#"Removed Columns" = Table.RemoveColumns(#"Selected Group", {"IsEmptyRow", "Index", "Section"}),
#"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ObjectID", Int64.Type}, {"Feld", type text}, {"Datentyp", type text}, {"boolValue", type text}, {"dateValue", type date}, {"intValue", Int64.Type}, {"stringValue", type text}, {"longStringValue", type text}, {"referencedObjectId", Int64.Type}}),
#"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({[boolValue], Text.From([dateValue], "en-US"), Text.From([intValue], "en-US"), [stringValue], [longStringValue], Text.From([referencedObjectId], "en-US")}, ""), type text),
#"Removed Columns1" = Table.RemoveColumns(#"Inserted Merged Column",{"Datentyp", "boolValue", "dateValue", "intValue", "stringValue", "longStringValue", "referencedObjectId"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Feld]), "Feld", "Merged")
in
#"Pivoted Column"

I am trying to create two separate columns from one column. I have cost type column that has actual and forecast as types

as shown in the image above, I am trying to separate actuals and forecast as columns. Is there a way to transform a table as shown in the image?
Its filtering and combining.
Use drop down atop the column to filter cost_types for "actuals". Remove cost_types column. Rename the amount column as actuals
Use drop down atop the column to filter cost_types for "forecast". Remove cost_types column. Rename the amount column as forecast
combine the two data sets
sample code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each ([cost type] = "actual")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"cost type"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"amount", "actuals"}}),
#"Filtered Rows2" = Table.SelectRows(Source, each ([cost type] = "forecast")),
#"Removed Columns2" = Table.RemoveColumns(#"Filtered Rows2",{"cost type"}),
#"Renamed Columns2" = Table.RenameColumns(#"Removed Columns2",{{"amount", "forecast"}}),
combined = Table.Combine({#"Renamed Columns" , #"Renamed Columns2"})
in combined
alternate data structure, which makes more sense to me:
code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"project id", type text}, {"title", type text}, {"manager", type text}, {"dept", type text}, {"Subproject id", type text}, {"effective date", type text}, {"cost type", type text}, {"amount", Int64.Type}}),
#"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[#"cost type"]), "cost type", "amount", List.Sum)
in #"Pivoted Column"

excel convert data into multi rows from single row

I have below sample insurance data contains family details in single row for each ID.
ID Enrollment date Area Full Name Gender DOB Sum Insured Spouse Name Gender DOB Kid1_Name Gender DOB Kid2_Name Gender DOB
29348 24-01-2021 17 NAINAR M Male 17-Mar-1982 500000 SUBBULAKSHMI FEMALE 21-Jun-1988 GOKULSRIRAM MALE 31-Oct-2007 SRIDHAR MALE 19-Feb-2009
23434 19-04-2020 17 Kishore Male 12-Jun-1986 200000 A Savitha Female 10-Jun-1991 Sathvik Male 4-Mar-2014 A Saketh male 13-Feb-2015
46565 01-05-2020 5 Ragu Male 6-Aug-1996 300000
I'm trying to convert data like below format, so that family details are shown in rows
Tried using PivotTable option and power query option in excel but no luck.
Is it possible in excel ?
Thanks
Here's one kludgy way to do it in powerquery
(a) Merge groups of columns together (b) unpivot (c) split those columns again
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Self", each "Self"),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Spouse", each "Spouse"),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Child1", each "Child1"),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Child2", each "Child2"),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Added Custom3", {{"DOB", type text}, {"Sum Insured", type text}}, "en-US"),{"Full Name", "Gender", "DOB", "Self","Sum Insured"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m1"),
#"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns", {{"DOB3", type text}}, "en-US"),{"Spouse Name", "Gender2", "DOB3", "Spouse"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m2"),
#"Merged Columns2" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns1", {{"DOB5", type text}}, "en-US"),{"Kid1_Name", "Gender4", "DOB5", "Child1"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m3"),
#"Merged Columns3" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns2", {{"DOB7", type text}}, "en-US"),{"Kid2_Name", "Gender6", "DOB7", "Child2"},Combiner.CombineTextByDelimiter("::", QuoteStyle.None),"m4"),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Merged Columns3", { "ID", "Enrollment date", "Area"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByDelimiter("::", QuoteStyle.Csv), {"Name", "Gender", "Date of Birth", "Relation", "Insured amount"}),
#"Filtered Rows" = Table.SelectRows(#"Split Column by Delimiter", each ([Name] <> "")),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Date of Birth", type datetime}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Date of Birth", type date}, {"Enrollment date", type date}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Attribute"})
in #"Removed Columns"
Note if you load multiple columns with same column headers into powerquery, then the titles will change to have numbers after them. You probably will have to update code to fix the column names for Date Birth and Gender
Here is another power query method.
Original Data
Read the code comments and explore the Applied steps to get a better idea of the algorithm.
Select the ID column and Unpivot other columns
Group by ID
Create a custom aggregation that creates a List of records for each family
Expand the records into a table
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//Unpivot all except ID column
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID"}, "Attribute", "Value"),
//Group by ID then custom aggregation
//Column Names for final report
colNames = {"Date of Enrollment", "Area", "Relation", "Name", "Gender", "Date of Birth", "Sum Insured"},
#"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"ID"}, {
{"Records", (t)=>
List.Generate(
()=>[ed=t[Value]{0}, a=t[Value]{1}, r="Self", n=t[Value]{2}, g=t[Value]{3}, dob=t[Value]{4}, si=t[Value]{5}, idx=5],
each [idx] < Table.RowCount(t),
each [ed=null, a=null, r=Text.SplitAny(t[Attribute]{[idx]+1}," _"){0},
n=t[Value]{[idx]+1}, g=t[Value]{[idx]+2}, dob=t[Value]{[idx]+3}, si=null, idx=[idx]+3],
each Record.FromList(
{[ed],[a],[r],[n],[g],[dob],[si]},
colNames)
)}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"ID"}),
#"Expanded Records" = Table.ExpandListColumn(#"Removed Columns", "Records"),
#"Expanded Records1" = Table.ExpandRecordColumn(#"Expanded Records", "Records",
colNames,colNames),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Records1",{
{"Date of Enrollment", type date}, {"Area", Int64.Type}, {"Relation", type text}, {"Name", type text},
{"Gender", type text}, {"Date of Birth", type date}, {"Sum Insured", Currency.Type}})
in
#"Changed Type1"
Results

Power Query Merge Queries Data Format Error - Cant Convert to Number

Intent -
I have 2 tables "Diagnosis" and "PHQ9 Score". Both tables have two common columns, Chart Number and Date of Service. In each table I merged these 2 columns and used the default name "Merged" in both. I then selected merge queries and used the "Merged" column in each table as the reference column to complete the merge query. I am intending to bring in data from the PHQ9 table into the Diagnosis table. power query then creates a column with the word "Table" in each cell. I then use the drop down in the new column to select the column of data from the PHQ9 table I wish to have added to the Diagnosis table.
Here is the result:
I have looked for data type issues but no luck.
let
Source = Folder.Files("\\dc\finance team\Finance Team\PHQ9\Patrick Testing\Data\Diagnosis"),
#"Filtered Hidden Files1" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true),
#"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
#"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
#"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File"}),
#"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Table Column1",{{"Source.Name", type text}, {"Name", type text}, {"Data", type any}, {"Item", type text}, {"Kind", type text}, {"Hidden", type logical}}),
#"Expanded Data" = Table.ExpandTableColumn(#"Changed Type", "Data", {"Column1", "Column2", "Column3", "Column4", "Column5"}, {"Data.Column1", "Data.Column2", "Data.Column3", "Data.Column4", "Data.Column5"}),
#"Removed Top Rows" = Table.Skip(#"Expanded Data",1),
#"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
#"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Depression 2.xlsx", "Page1_1", "Column3", "Page1_1_1", "Sheet", "false"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Removed Columns", {{"ICD-10 Codes", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "ICD-10 Codes"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ICD-10 Codes", type text}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type1", each Text.Contains([#"ICD-10 Codes"], "F32") or Text.Contains([#"ICD-10 Codes"], "F33") or Text.Contains([#"ICD-10 Codes"], "R45")),
#"Replaced Value" = Table.ReplaceValue(#"Filtered Rows"," ","",Replacer.ReplaceText,{"ICD-10 Codes"}),
#"Extracted Date" = Table.TransformColumns(#"Replaced Value",{}),
#"Changed Type2" = Table.TransformColumnTypes(#"Extracted Date",{{"Service Date", type date}, {"Patient Chart Number", Int64.Type}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type2", "Patient Chart Number", "Patient Chart Number - Copy"),
#"Duplicated Column1" = Table.DuplicateColumn(#"Duplicated Column", "Service Date", "Service Date - Copy"),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Duplicated Column1", {{"Patient Chart Number - Copy", type text}, {"Service Date - Copy", type text}}, "en-US"),{"Patient Chart Number - Copy", "Service Date - Copy"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
#"Merged Queries" = Table.NestedJoin(#"Merged Columns", {"Merged"}, #"PHQ9 Score", {"Merged"}, "PHQ9 Score", JoinKind.LeftOuter),
#"Expanded PHQ9 Score" = Table.ExpandTableColumn(#"Merged Queries", "PHQ9 Score", {"Note Field Value"}, {"PHQ9 Score.Note Field Value"})
in
#"Expanded PHQ9 Score"

Resources