|
| 1 | +// DevicesOU_MultiRow |
| 2 | +let |
| 3 | + EnterData = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wckkty0xONbRSqKis0ssvSo+JgYgUK8Xq4JGNiQkoyk8hRk1MjH9aGpCLpNYIU60CWFoBwjPG6xosshiuMcFrAhbZmJiQ1OISoCtiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text = _t]), |
| 4 | + Source = Table.TransformColumnTypes(EnterData,{{"Text", type text}}), |
| 5 | + |
| 6 | + #"Added Custom" = Table.AddColumn(Source, |
| 7 | + "SplitToRecords", |
| 8 | + (row) => [ |
| 9 | + Segments = Text.Split( row[Text], ": "), |
| 10 | + DeviceName = Segments{0}, |
| 11 | + OU = Segments{1} |
| 12 | + ], Record.Type |
| 13 | + ), |
| 14 | + // returns a single item, the longest string |
| 15 | + List.SelectLongest = (source as list) as any => |
| 16 | + List.First( List.SortByLongest( source ) ), |
| 17 | + |
| 18 | + // returns original list, sorted as longest first |
| 19 | + List.SortByLongest = (source as list) as list => |
| 20 | + List.Sort( source, (item) => -Text.Length( item ) ), |
| 21 | + |
| 22 | + |
| 23 | + #"Expanded SplitToRecords" = Table.ExpandRecordColumn(#"Added Custom", "SplitToRecords", {"DeviceName", "OU"}, {"Device", "OU"}), |
| 24 | + #"Changed Type" = Table.TransformColumnTypes(#"Expanded SplitToRecords",{{"Device", type text}, {"OU", type text}}), |
| 25 | + #"Cleaned Text" = Table.TransformColumns(#"Changed Type",{ {"Device", Text.Trim, type text}, {"OU", Text.Trim, type text}} ), |
| 26 | + #"Removed Other Columns" = Table.SelectColumns(#"Cleaned Text",{"Device", "OU"}), |
| 27 | + #"Grouped Rows" = Table.Group( |
| 28 | + #"Removed Other Columns", {"Device"}, |
| 29 | + { |
| 30 | + { |
| 31 | + "OU_Longest", |
| 32 | + (row) => List.SelectLongest( row[OU] ), |
| 33 | + Text.Type |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + ) |
| 38 | +in |
| 39 | + #"Grouped Rows" |
| 40 | + |
| 41 | +// DevicesOU_SingleLine |
| 42 | +let |
| 43 | + |
| 44 | + EnterData = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wckkty0xONbRSqKis0ssvSo+JgYgUW6MLxMQEFOWn4BCOifFPSwNylWJ1YGYaYZqpgCRtTLSVSJpMiNIUklpcArQqFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text = _t]), |
| 45 | + Source = Table.TransformColumnTypes(EnterData,{{"Text", type text}}), |
| 46 | + |
| 47 | + // this is overkill. I kept it verbose so you can inspect each stage |
| 48 | + // it splits a single line, multiple times, as a single "step" |
| 49 | + SplitToRecords = Table.AddColumn(Source, |
| 50 | + "SplitToRecords", |
| 51 | + (row) => [ |
| 52 | + // Segments = Text.Split( row[Text], ": "), |
| 53 | + line = row[Text], |
| 54 | + DeviceName = Text.BeforeDelimiter( line, ": ", 0 ), |
| 55 | + RestOfLine = Text.AfterDelimiter( line, ": ", 0 ), |
| 56 | + OU_list = Text.Split( RestOfLine, ";"), |
| 57 | + sortByLongest = List.Sort( OU_list, each -Text.Length(_) ), |
| 58 | + OU_Longest = List.First( sortByLongest, null ) |
| 59 | + ], Record.Type |
| 60 | + ), |
| 61 | + |
| 62 | + // for testing, this lets you see one record at a time |
| 63 | + InspectOneRow = SplitToRecords{0}[SplitToRecords], |
| 64 | + |
| 65 | + // else grab the only two columns that we care about |
| 66 | + #"Expanded SplitToRecords" = Table.ExpandRecordColumn( |
| 67 | + SplitToRecords, "SplitToRecords", |
| 68 | + {"DeviceName", "OU_Longest"}, {"DeviceName", "OU_Longest"} ), |
| 69 | + |
| 70 | + #"Changed Type" = Table.TransformColumnTypes( |
| 71 | + #"Expanded SplitToRecords", |
| 72 | + { {"DeviceName", type text}, {"OU_Longest", type text}} ) |
| 73 | +in |
| 74 | + #"Changed Type" |
0 commit comments