-
Notifications
You must be signed in to change notification settings - Fork 4
/
Template-Import-ninlib.pq
105 lines (97 loc) · 4.83 KB
/
Template-Import-ninlib.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// PQ_Library_Path
"H:\data\2024\tabular\Ninmonkey.PowerQueryLib\build\2024-04.ninlib.pq" meta [IsParameterQuery=true, List={"H:\data\2024\tabular\Ninmonkey.PowerQueryLib\build\2024-04.ninlib.pq", "H:\data\2024\tabular\Ninmonkey.PowerQueryLib\build\2022-01.ninlib.pq", "c:\pq\your-lib.pq", "H:\data\2023\BI\git\ninMonkQuery-examples\Dynamic_Logic\pq.lib\2024.pq-lib.pq"}, DefaultValue="H:\data\2024\tabular\Ninmonkey.PowerQueryLib\build\2024-04.ninlib.pq", Type="Text", IsParameterQueryRequired=true]
// lib
/* a fancier version of the import function for Import.PowerQueryLib.pq, or other scripts
let
// evaluates a .pq text file that's editable outside of power bi, without pasting updates. just refresh.
EvalQueryFunc = (filePath as text, optional options as nullable record) as any => [
encoding = options[Encoding]? ?? TextEncoding.Utf8,
environment = options[Environment]? ?? #shared,
bytes = File.Contents(filePath),
lines = Text.FromBinary( bytes, encoding ),
return = Expression.Evaluate( lines, environment )
][return]
in
EvalQueryFunc
<file:///H:\data\2023\BI\git\ninMonkQuery-examples\Dynamic_Logic\pq.lib\2024.pq-lib.pq>
*/
// with extra
let
/* this is the entry point */
EvalQuery = ( filePath as text, optional options as nullable record ) as any => [
encoding = options[Encoding]? ?? TextEncoding.Utf8,
environment = options[Environment]? ?? #shared,
bytes = File.Contents(filePath),
lines = Text.FromBinary( bytes, encoding ),
return = Expression.Evaluate( lines, environment )
][return],
Convert.ScriptExtent.FromError = (err as any) => [
Split.ScriptExtent = Splitter.SplitTextByEachDelimiter({"[", ",", "-", ",", "]"}, QuoteStyle.None),
lineData = Split.ScriptExtent( err[Message] ),
ret = [
StartLineNumber = Number.FromText( lineData{1}? ),
StartColumnNumber = Number.FromText( lineData{2}? ),
EndLineNumber = Number.FromText( lineData{3}? ),
EndColumnNumber = Number.FromText( lineData{4}? ),
RemainingMessage = lineData{5}?, // shouldn't be more than 1 index?
Reason = err[Reason],
Message = err[Message],
ErrorRecord = err,
ErrorDetailsJson = [
target = ErrorRecord[Detail]?,
json = Json.FromValue( target, TextEncoding.Utf8 ),
string = try Text.FromBinary( json, TextEncoding.Utf8 ) catch (e) => null
][string],
RawText = err[Message]
]
][ret],
File.ShowError = ( path as text, extent as record, optional options as nullable record) => [
LinesOfContext = options[LinesOfContext]? ?? 2,
Extent = extent,
Bytes = File.Contents( path ),
SourceMeta = Value.Metadata( Bytes ),
Contents = Lines.FromBinary( Bytes, QuoteStyle.None, false, TextEncoding.Utf8 ),
LinesCount = extent[EndLineNumber] - extent[StartLineNumber],
SelectedLines = List.Range( Contents, extent[StartLineNumber] - LinesOfContext, LinesCount + LinesOfContext ),
Prefix = {
"Reason: #[Reason]",
"Path: #[LineNumber], #[FullPath]",
"Message: #[Message]",
"...",
"RemainingMessage: #[RemainingMessage]",
"DetailsJson: #[ErrorDetailsJson]",
"...",
"Context: "
},
MergedTemplate = Text.Combine( Prefix & SelectedLines, "#(cr,lf)" ),
ret = Text.Format( MergedTemplate, [
Reason = extent[Reason],
Message = extent[Message],
LineNumber = extent[StartLineNumber]?,
RemainingMessage = extent[RemainingMessage]?,
ErrorDetailsJson = extent[ErrorDetailsJson]?,
FullPath = path
] )
][ret],
// ],
FinalEval =
try EvalQuery( PQ_Library_Path )
catch (e) =>
File.ShowError( PQ_Library_Path, Convert.ScriptExtent.FromError( e ), [
LinesOfContext = 5
] )
in
FinalEval
// lib_meta
let
Source = lib,
top_items = Table.Buffer( Table.FromValue( lib ) ),
Custom1 = top_items,
procRow = ( source as any) as nullable record => [
target = Value.Metadata( source ),
hasKeys = Record.FieldCount( target ) > 0,
ret = if hasKeys then source else null
][target],
#"Added Custom" = Table.AddColumn(Custom1, "Custom", (row) => procRow( row[Value] ), type record)
in
#"Added Custom"