Code Monkey home page Code Monkey logo

power-query's People

Contributors

alendaris avatar

Stargazers

 avatar

Watchers

 avatar

power-query's Issues

AssertTable is filtering too early

AssertTable currently filters before applying the test, which means tests on data that will be removed will always fail.

// Runs an Assertion Check on an entire table and returns only the rows where there is an issue
AssertTable = (inputTable as table, functionToApply as function, optional columnsToInclude as list) =>
let
indexedTable = Table.AddIndexColumn(inputTable, "Index", 2, 1, Int64.Type),
selectedColumns = if (columnsToInclude = null) then Table.ReorderColumns(indexedTable, {"Index"} & Table.ColumnNames(inputTable)) else Table.SelectColumns(indexedTable, {"Index"} & columnsToInclude),
transformedTable = Table.AddColumn(selectedColumns, "_Assert", functionToApply),
filteredTable = Table.SelectRowsWithErrors(transformedTable)
in
filteredTable,

  • Swap selectedColumns and transformedTable
  • Update the remaining lines

Add documentation metadata to Luminosity.pq

(color as text, optional gamma as number) =>
let
gamma_use = if gamma = null then 2.2 else gamma,
luminosity =
0.2126 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 1, 2))/255,gamma_use) + // Red
0.7152 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 3, 2))/255,gamma_use) + // Green
0.0722 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 5, 2))/255,gamma_use), // Blue
in
luminosity

Add documentation metadata to ContrastingColor.pq

(background_color as text, optional dark_text as text, optional light_text as text, optional gamma as number) =>
let
dark_text_use = if dark_text = null then "#000000" else dark_text,
light_text_use = if light_text = null then "#FFFFFF" else light_text,
gamma_use = if gamma = null then 2.2 else gamma,
luminosity = (color as text, optional gamma as number) =>
let
gamma_use = if gamma = null then 2.2 else gamma,
luminosity =
0.2126 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 1, 2))/255,gamma_use) + // Red
0.7152 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 3, 2))/255,gamma_use) + // Green
0.0722 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 5, 2))/255,gamma_use) // Blue
in
luminosity,
font_color = if luminosity(background_color, gamma_use)>0.5 then dark_text_use else light_text_use
in
font_color

Add documentation metadata to Unzip.pq

// Unzip
// Pulled from: https://community.powerbi.com/t5/Power-Query/How-to-connect-Azure-DevOps-REST-API-in-to-power-bi/m-p/895318/highlight/true#M30599
// Microsoft Power BI Community Forums Member artemus :: https://community.powerbi.com/t5/user/viewprofilepage/user-id/62590
(ZIPFile) =>
let
ushort = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
uint = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
EDOCfn = BinaryFormat.Record([
ZipContent = BinaryFormat.Binary(Binary.Length(ZIPFile) - 22),
Magic = uint,
DiskNum = ushort,
CDirectoryDiskId = ushort,
CDirectoryRecordCountOnDisk = ushort,
CDirectoryRecordCount = ushort,
SizeOfCentralDirectory = uint,
CentralDirectoryOffset = uint,
CommendLength = ushort
]),
EDOC = EDOCfn(ZIPFile),
BeforeCentralDirectory = BinaryFormat.Binary(EDOC[CentralDirectoryOffset]),
CentralDirectory = BinaryFormat.Length(BinaryFormat.Record(
[
ZipContent = BeforeCentralDirectory,
Items = BinaryFormat.List(BinaryFormat.Record(
[
Magic = uint,
CurrentVersion = ushort,
MinVersion = ushort,
Flags = ushort,
CompressionMethod = ushort,
FileModificationTime = ushort,
FileModificationDate = ushort,
CRC32 = uint,
BinarySize = uint,
FileSize = uint,
FileInfo = BinaryFormat.Choice(
BinaryFormat.Record(
[
Len = ushort,
FieldsLen = ushort,
FileCommentLength = ushort,
Disk = ushort,
InternalFileAttr = ushort,
ExternalAttr = uint,
PosOfFileHeader = uint
]),
(fileInfo) => BinaryFormat.Record(
[
FileName = BinaryFormat.Text(fileInfo[Len], TextEncoding.Ascii),
Fields = BinaryFormat.Binary(fileInfo[FieldsLen]),
FileComment = BinaryFormat.Text(fileInfo[FileCommentLength], TextEncoding.Ascii),
Disk = BinaryFormat.Transform(BinaryFormat.Null, each fileInfo[Disk]),
InternalFileAttr = BinaryFormat.Transform(BinaryFormat.Null, each fileInfo[Disk]),
ExternalAttr = BinaryFormat.Transform(BinaryFormat.Null, each fileInfo[InternalFileAttr]),
PosOfFileHeader = BinaryFormat.Transform(BinaryFormat.Null, each fileInfo[PosOfFileHeader])
])
)
]),
EDOC[CDirectoryRecordCount]
)
]),
EDOC[CentralDirectoryOffset] + EDOC[SizeOfCentralDirectory]),
Contents = List.Transform(
CentralDirectory(ZIPFile)[Items],
(cdEntry) =>
let
ZipEntry = BinaryFormat.Record(
[
PreviousData = BinaryFormat.Binary(cdEntry[FileInfo][PosOfFileHeader]),
Magic = uint,
ZipVersion = ushort,
ZipFlags = ushort,
CompressionMethod = ushort,
FileModificationTime = ushort,
FileModificationDate = ushort,
CRC32 = uint,
BinarySize = uint,
FileSize = uint,
FileName = BinaryFormat.Choice(
BinaryFormat.Record(
[
Len = ushort,
FieldsLen = ushort
]),
(fileInfo) => BinaryFormat.Record(
[
FileName = BinaryFormat.Text(fileInfo[Len], TextEncoding.Ascii),
Fields = BinaryFormat.Binary(fileInfo[FieldsLen])
])
),
FileContent = BinaryFormat.Transform(
BinaryFormat.Binary(cdEntry[BinarySize]),
each Binary.Decompress(_, Compression.Deflate)
)
])(ZIPFile)
in
[FileName=ZipEntry[FileName][FileName], Content=ZipEntry[FileContent]]
)
in
Contents

Drop Switch function

  • Redo if statement to catch all types
  • Drop the switch function

https://github.com/alendaris/power-query/blob/18-drop-switch-function/functions/ValueToMCode.pq#L47-L66

Fail = (value as any) as text => "Type Unrecognized",
Switch = (Expression as any, Values as list, Results as list, optional Else as any) =>
try Results{List.PositionOf(Values, Expression)}
otherwise
if Else = null then Fail
else Else,
ToMCode = (value as any,optional indent as number) as any =>
let
ParseFunc = Switch(Value.Type(Source),
{type null,type text,type number},
{NullToMCode,TextToMCode,NumberToMCode}),
Return =
if Value.Is(value,Table.Type) then TableToMCode(value,indent)
else if Value.Is(value,Record.Type) then RecordToMCode(value,indent)
else if Value.Is(value,List.Type) then ListToMCode(value,indent)
else ParseFunc(Source,indent)
in
Return,

FunctionToMCode output is quoted

Output is quoted.. Only the Deflated function code should be quoted.

FunctionToMCode = (value as function) as text =>
if Record.HasFields(Value.Metadata(Value.Type(value)),"Source Code") then
let
Source = Value.Metadata(Value.Type(value))[Source Code],
Deflated = Text.From(Binary.Compress(Text.ToBinary(Source),Compression.Deflate)),
Reinflate = "Text.FromBinary(Binary.Decompress(Binary.FromText(" & Deflated & "),Compression.Deflate))"
in
TextToMCode(Reinflate)
else
TextToMCode("/* Function without Source Code Metadata */null"),

ColorContrastRatio is not displaying examples

ExampleParameter1 = "#FFCC72",
ExampleParameter2 = "#000000",
ExampleResult = func(ExampleParameter1,ExampleParameter2),
fntype = type function
(
lighter_color as (type text meta [
Documentation.FieldCaption = "Color 1",
Documentation.FieldDescription = "One of two colors to compare in the contrast ratio calculation.",
Documentation.SampleValues = {ExampleParameter1}
]),
darker_color as (type text meta [
Documentation.FieldCaption = "Color 2",
Documentation.FieldDescription = "One of two colors to compare in the contrast ratio calculation.",
Documentation.SampleValues = {ExampleParameter2}
])
) as text meta
[
Documentation.Name = "ColorContrastRatio",
Documentation.LongDescription = "This function calculates the Color Contrast Ratio between the lighter color and the darker color.",
Documentation.Examples =
{
[
Description = "Determine the color contrast ratio between #FFCC72 and #000000.",
Code = "ColorContrastRatio(""#FFCC72"", ""#000000"")",
Result = ExampleResult
]
}
]

Line 37 may be at issue with the incorrect type for the function return (function should return numeric):

Add documentation metadata to Load Function Library - Local Filesystem.pq

(FolderPath as text) =>
let
Source = Folder.Files(FolderPath),
#"Removed Other Columns" = Table.SelectColumns(Source, {"Content", "Name"}),
#"Remove .pq" = Table.ReplaceValue(#"Removed Other Columns", ".pq", "", Replacer.ReplaceText, {"Name"}),
#"Pull Source Code" = Table.AddColumn(#"Remove .pq", "Source Code", each Text.Combine(Lines.FromBinary([Content]), "#(lf)")),
#"Generate Function" = Table.AddColumn(#"Pull Source Code", "Function from File", each Expression.Evaluate([Source Code], #shared)),
#"Original Function Type" = Table.AddColumn(#"Generate Function", "Original Function Type", each Value.Type([Function from File])),
#"Add Source Code" = Table.AddColumn(#"Original Function Type", "Function Type Metadata with Source Code Added", each Record.AddField(Value.Metadata([Original Function Type]), "Source Code", [Source Code])),
#"Regenerate Function" = Table.AddColumn(#"Add Source Code", "Value", each Value.ReplaceType([Function from File], Value.ReplaceMetadata([Original Function Type], [Function Type Metadata with Source Code Added]))),
#"Removed Other Columns1" = Table.SelectColumns(#"Regenerate Function", {"Name", "Value"}),
newrecord = Record.FromTable(#"Removed Other Columns1"),
Library = if Record.HasFields(newrecord, {"Library Source Code"}) then
[
Library = newrecord,
Source Code = newrecord[Library Source Code](newrecord)
]
else
[
Library = newrecord
]
in
Library

Add documentation metadata to Library Source Code.pq

(libraryName as any) =>
let
SourceCodeNamePrefix = "",
library =
if Value.Type(libraryName) = Text.Type then
Expression.Evaluate(Expression.Identifier(libraryName), #shared)
else
libraryName,
functionSourceCode = library[Library Function Source Code],
funcs = Table.RemoveColumns(Record.ToTable(library), "Value"),
codeTable = Table.AddColumn(funcs, "Value", each functionSourceCode(libraryName, [Name])),
#"Replaced Value" = Table.ReplaceValue(codeTable, each [Name], each SourceCodeNamePrefix & [Name], Replacer.ReplaceText, {"Name"}),
code = Record.FromTable(#"Replaced Value")
in
code

ToMCode sub function should be main-level code

ToMCode = (value as any,optional indent as number) as any =>
let
ParseFunc = Switch(Value.Type(Source),
{type null,type text,type number},
{NullToMCode,TextToMCode,NumberToMCode}),
Return =
if Value.Is(value,Table.Type) then TableToMCode(value,indent)
else if Value.Is(value,Record.Type) then RecordToMCode(value,indent)
else if Value.Is(value,List.Type) then ListToMCode(value,indent)
else ParseFunc(Source,indent)
in
Return,
Return = ToMCode(Source)

Add documentation metadata to ColorContrastRatio.pq

(lighter_color as text, darker_color as text) =>
let
luminosity = (color as text, optional gamma as number) =>
let
gamma_use = if gamma = null then 2.2 else gamma,
luminosity =
0.2126 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 1, 2))/255,gamma_use) + // Red
0.7152 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 3, 2))/255,gamma_use) + // Green
0.0722 * Number.Power(Expression.Evaluate("0x" & Text.Middle(color, 5, 2))/255,gamma_use) // Blue
in
luminosity,
color_contrast =
if luminosity(lighter_color)>=luminosity(darker_color) then
(luminosity(lighter_color)+0.05)/(luminosity(darker_color)+0.05)
else
(luminosity(darker_color)+0.05)/(luminosity(lighter_color)+0.05)
in
color_contrast

Add metadata documentation to TableCleanFieldNames.pq

See the template in the functions folder.

(dirtyTable as table) as table =>
let
RenameColumn = (ColumnName as text) as text =>
let
SplitColumnName =
Text.Combine(
List.Transform(
Text.Split(
Text.Replace(
Text.Combine(
Splitter.SplitTextByCharacterTransition({"a".."z"},{"A".."Z"})(ColumnName),
" "
),
"_",
" "
),
" "
),
each
if Text.Length(_)<3 then
Text.Upper(_)
else
Text.Proper(_)
),
" "
),
FixTrailingNo =
if Text.End(SplitColumnName,3) = " NO" then
Text.Replace(SplitColumnName," NO"," Number")
else
SplitColumnName
in
FixTrailingNo
in
Table.TransformColumnNames(dirtyTable,RenameColumn)

Luminosity.pq does not have examples displayed

//ExampleResult = func("#FFCC72", 2.2),
ExampleResult = func("#0000FF", 2.2),
fntype = type function
(
color as (type text meta [
Documentation.FieldCaption = "Color",
Documentation.FieldDescription = "Color in RGB hexadecimal format (#RRGGBB)",
Documentation.SampleValues = {"#FFCC72"}
]),
optional gamma as (type number meta [
Documentation.FieldCaption = "Gamma",
Documentation.FieldDescription = "Gamma value for the luminosity calculation (default: 2.2)",
Documentation.SampleValues = {2.2}
])
) as table meta
[
Documentation.Name = "Luminosity",
Documentation.LongDescription = "This function calculates a luminosity value for a given hexadecimal color.",
Documentation.Examples =
{
[
Description = "Determine the luminosity of pure blue (#0000FF).",
Code = "Luminosity(""#0000FF"")",
Result = 0.0722
]
}
]

Might be at issue at L28 where the function return is defined as table, but the result is a number.

) as table meta

Compressed TableToMCode causes errors in PBI Server environments

TableToMCode =
let
compressedCode = "zVTRatswFH33V1xUCDYYbXkdTaF1Eyg0HaShLyEU1b5OzGQpk+QlIeRf8i35sklynMbOBnucDbZ879HRuefK4mggr0QKAwAIIJyyD47ANBg3iGBwF3A0wfFwPPjU8TA4jWIffC60+Z4nklel0J9JOpUTudY1XVRDEyl+oTJT2SGiIyVLxxN6WHO3mOPjoZV8XfHCGFTUDx62L9IsC7EIoy5QVJz/S2y4MYq9MV6hpkOlpDpnz+oVvhplF7lQfp9ltcC29Hal3aVIUmkjS9KNI0uXlhg3hiay/CgEhs4DOlVM6FyqMpzVi/XncYMucni3rXMVgVmiAOKGBJBrBGIv6IFnnOCKsxRD/+EMD9+j2AH8g5DIAv3bUpMYSNStf2R5L+onA7jxWySEnZ12Rvc6FdQ+1bpfWInnLWEpiFuIXCzV88F9DDchzyPYWeq/En/2Y1b76Uy5mNuh3cMeIiBBITrVxEEm06pEYZgppLBmzoLHywh1st3vQaDZ2uNEZtbeuAN8RJ2qYnWisfCmcRoYwG1qJ915itsvfgxgpM1orwNkDmNwcdtIZmBd2JamCpk5BbzbUAjXZ/hZodoCZoWRil4reZZi8f+oSeyshbSIQePhNeZVVipFj7hOvqHSTRl9+pX2ryH3lVlK5RFP5Q+EEfKsZEJ8g/V6TadLfHhKU1lZuDA0lSX8Qedww8oVR21ZdrO2fSdZvu+NxgnqipvT53w/D9wBah2xT/AnSfPTTbcrDN0hG7fjYzQsY4aFdfQMi2Jo7ckoCn4D",
func = Expression.Evaluate(Text.FromBinary(Binary.Decompress(Binary.FromText(compressedCode), Compression.Deflate)), #shared)
in
func,

TableToMCode =
let
compressedCode = "zVTRatswFH33V1xUCDYYbXkdTaF1Eyg0HaShLyEU1b5OzGQpk+QlIeRf8i35sklynMbOBnucDbZ879HRuefK4mggr0QKAwAIIJyyD47ANBg3iGBwF3A0wfFwPPjU8TA4jWIffC60+Z4nklel0J9JOpUTudY1XVRDEyl+oTJT2SGiIyVLxxN6WHO3mOPjoZV8XfHCGFTUDx62L9IsC7EIoy5QVJz/S2y4MYq9MV6hpkOlpDpnz+oVvhplF7lQfp9ltcC29Hal3aVIUmkjS9KNI0uXlhg3hiay/CgEhs4DOlVM6FyqMpzVi/XncYMucni3rXMVgVmiAOKGBJBrBGIv6IFnnOCKsxRD/+EMD9+j2AH8g5DIAv3bUpMYSNStf2R5L+onA7jxWySEnZ12Rvc6FdQ+1bpfWInnLWEpiFuIXCzV88F9DDchzyPYWeq/En/2Y1b76Uy5mNuh3cMeIiBBITrVxEEm06pEYZgppLBmzoLHywh1st3vQaDZ2uNEZtbeuAN8RJ2qYnWisfCmcRoYwG1qJ915itsvfgxgpM1orwNkDmNwcdtIZmBd2JamCpk5BbzbUAjXZ/hZodoCZoWRil4reZZi8f+oSeyshbSIQePhNeZVVipFj7hOvqHSTRl9+pX2ryH3lVlK5RFP5Q+EEfKsZEJ8g/V6TadLfHhKU1lZuDA0lSX8Qedww8oVR21ZdrO2fSdZvu+NxgnqipvT53w/D9wBah2xT/AnSfPTTbcrDN0hG7fjYzQsY4aFdfQMi2Jo7ckoCn4D",
func = Expression.Evaluate(Text.FromBinary(Binary.Decompress(Binary.FromText(compressedCode), Compression.Deflate)), #shared)
in
func,

FunctionToMCode -- Retain Metadata for functions

Reinflate = "Text.FromBinary(Binary.Decompress(Binary.FromText(" & TextToMCode(Deflated) & "),Compression.Deflate))"

Some function designers include metadata with their functions (thinking of ImkeF -- and I need to do that more so that my own functions have proper documentation). ValueToMCode only accesses a Metadata called Source Code (which I explicitly set in my external function loader code), so any additional metadata on the parameters or the function itself is lost when the source code is extracted.

Move indent parameter in ValueToMCode into the options record

Want to move the indentation parameter in ValueToMCode into the options record to remove it from the documentation screen for the function. This should not break any other code since it really is an internal option for recursive calls to the function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.