Skip to content

Instantly share code, notes, and snippets.

View Hugoberry's full-sized avatar

Igor Cotruta Hugoberry

View GitHub Profile
@Hugoberry
Hugoberry / reverse32bit.m
Created February 27, 2017 23:08
Function for reversing the bits in a 32bit number
reverse32 = (x) =>
let
b0 = Number.BitwiseAnd(x,0xff),
b1 = Number.BitwiseShiftRight(Number.BitwiseAnd(x,0xff00),8),
b2 = Number.BitwiseShiftRight(Number.BitwiseAnd(x,0xff0000),16),
b3 = Number.BitwiseShiftRight(Number.BitwiseAnd(x,0xff000000),24)
in
Number.BitwiseOr(
Number.BitwiseOr(
Number.BitwiseShiftLeft(reverse(b0),24),
@Hugoberry
Hugoberry / CRC32lookupTable.m
Last active February 27, 2017 23:12
Precomputin the CRC32 lookup table
calculateCRClookup = (divident)=>
let
// move divident byte into MSB of 32Bit CRC
curByte = Number.BitwiseShiftLeft(divident,24)
in
List.Accumulate({0..7},
curByte,
(_curByte,_)=>
let
// if MSB set, shift left and XOR with polynomial = 0x4C11DB7
@Hugoberry
Hugoberry / swap.m
Created March 1, 2017 15:11
Swapping the list elements in positions _from and _to. Power Query implementation
List.Swap = (_,_from as number ,_to as number) =>
let
from = List.Min({_from,_to}),
to = List.Max({_from,_to})
in if from=to then _ else
List.Range(_,0,from)
&{_{to}}
&List.Range(_,from+1,to-from-1)
&{_{from}}
&List.Range(_,to+1)
@Hugoberry
Hugoberry / Web.BufferedContents.m
Created March 2, 2017 11:36
When the trace files shows duplicate Web.Content which might be triggered by some operations down the Power Query pipeline, it is a good idea to Buffer the Web.Contents first
(url) =>
let
BufferedWithMetadata = (binary)=>(try Binary.Buffer() otherwise null) meta Value.Metadata(binary),
response = BufferWithMetadata(Web.Contents(url,[ManualStatusHandling={404}])),
out = if Value.Metadata(response)[Response.Status]=404
then null
else response
in
out
@Hugoberry
Hugoberry / Number.DEC2BIN.m
Last active March 15, 2017 17:30 — forked from IvanBond/Number.ToBinaryString.m
DEC2BIN or HEX2BIN in Power Query M
(num as number)=>
List.Last(
List.Generate(
() =>[reminder = num,
binString= Number.ToText(Number.BitwiseAnd(reminder,1))],
each [reminder]> 0,
each [reminder = Number.BitwiseShiftRight([reminder],1),
binString= Number.ToText(Number.BitwiseAnd(reminder,1))&[binString]],
each [binString]
)
@Hugoberry
Hugoberry / HEX2DEC.m
Created March 2, 2017 15:19
HEX2DEC one liner implementation in Power Query M
(hexString as text)
=> Expression.Evaluate(“0x”&hexString)
@Hugoberry
Hugoberry / Tabluar.xml
Created March 7, 2017 12:55
Hidden Tabular SSAS specific DMVs
<Batch Transaction="false" xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<Discover xmlns="urn:schemas-microsoft-com:xml-analysis">
<RequestType>TMSCHEMA_MODEL</RequestType>
<Restrictions>
<RestrictionList>
<DatabaseName>b7b2993d-570b-4c92-9e5d-d5e8f72c935d</DatabaseName>
</RestrictionList>
</Restrictions>
<Properties>
<PropertyList/>
let
Source = #table(null,{{1}}),
RScript = R.Execute("output<-dataset #(lf)msmdsrv_proc <- system2(""tasklist"",args = c('/nh','/fi','""imagename eq msmdsrv.exe""'), stdout=TRUE) #(lf)msmdsrv_pid <- strsplit(msmdsrv_proc[2],""\\s+"")[[1]][2]#(lf)msmdsrv_regex <- paste(c(""ESTABLISHED\\s*"", msmdsrv_pid),collapse="""")#(lf)tcp <- system2(""netstat"", args = c('-ano'), stdout=TRUE) #(lf)msmdsrv_connections<-grep(msmdsrv_regex,tcp,value=TRUE)#(lf)msmdsrv_inbound<-strsplit(strsplit(msmdsrv_connections[1],""\\s+"")[[1]][3],"":"")[[1]] #(lf)output$port<-tail(msmdsrv_inbound,n=1)",[dataset=Source]),
out = RScript{[Name="output"]}[Value]
in
out
@Hugoberry
Hugoberry / RKmeans.m
Last active September 1, 2017 11:35
R flavoured Kmeans algorithm in Power Query
let
RScript = R.Execute("set.seed(20)#(lf)irisCluster<-kmeans(iris[,3:4],3,nstart=20)#(lf)iris$cluster<-irisCluster$cluster"),
out = RScript{[Name="iris"]}[Value]
in
out
@Hugoberry
Hugoberry / RemovePassword.m
Created March 9, 2017 16:30
Fetching Remove Password comments from Github. Using Power Query and R
let
header = [#"Accept"="application/vnd.github.cloak-preview"],
response = Web.Contents("https://api.github.com/search/commits?q=remove password",[Headers=header]),
json = Json.Document(response),
select = Table.SelectColumns(Table.FromRecords(json[items]),{"url","commit","repository"}),
expand_commit = Table.TransformColumns(Table.ExpandRecordColumn(select, "commit", {"committer", "message"}),{"message",each Text.Clean(_) }),
expand_commiter = Table.ExpandRecordColumn(expand_commit, "committer", {"date", "name", "email"}),
expand_repo = Table.ExpandRecordColumn(expand_commiter, "repository", {"html_url"}),
commits = Table.AddColumn(expand_repo, "commit", each Json.Document(Web.Contents([url]))),
files = Table.ExpandListColumn(Table.ExpandRecordColumn(commits, "commit", {"files"}),"files"),