Skip to content

Instantly share code, notes, and snippets.

View cbaragao's full-sized avatar

Chris Aragao cbaragao

View GitHub Profile
// Function to calculate the distance between two geographic coordinates
GetDistance(lat1:Number, lon1:Number, lat2:Number, lon2:Number):Number = With(
{
// Convert latitude and longitude differences from degrees to radians
dLat: (lat2 - lat1) * Pi() / 180.0,
dLon: (lon2 - lon1) * Pi() / 180.0,
// Convert original latitudes to radians
(l as list, selections as list)=>
let
Source =
List.Accumulate(
selections,
{},
(state,current)=>
if
Text.Contains(current,":")
then
(l as list, range as list)=>
let
Start_Position = List.PositionOf(l, range{0}),
End_Position = List.PositionOf(l, range{1}),
Result= List.Range(l,Start_Position,(End_Position-Start_Position)+1)
in
Result
//SPEC
{
"data": {
"name": "dataset"
},
"title": "Sales Variance by Product",
"transform": [
{
"calculate": "17000000",
"as": "target"
//SPEC
{
"data": {
"name": "dataset"
},
"title": "Sales Variance by Product",
"transform": [
{
//"calculate": "datum['Total Sales'] *.80","as": "target"
"calculate": "17000000",
//SPEC
{
"data": {
"name": "dataset"
},
"title": "Sales Variance by Product",
"transform": [
{
//"calculate": "datum['Total Sales'] *.80","as": "target"
"calculate": "17000000",
{
"data": {
"name": "dataset"
},
"title":"Total Sales by Segment - Vertical Bar Chart",
"encoding": {
"x": {
"field": "Segment",
"type": "nominal",
"sort":"-y",
// SPECS
{
"data": {
"name": "dataset"
},
"title":"Sales Variance by Product",
"transform": [
{
//"calculate": "datum['Total Sales'] *.80","as": "target"
(t as table, seed as number) =>
let
// Step 1: Add an index column to the input table to preserve row order and enable alignment
Source = Table.AddIndexColumn(t, "__Index", 0, 1, Int64.Type),
// Step 2: Generate a reproducible list of random numbers using the provided seed
// This ensures consistent output across refreshes or deployments
Random_List = List.Random(Table.RowCount(t), seed),
// Step 3: Convert the random list into a table and add an index column for joining
(df as table, independent as text, dependent as text)=>
let
Source =
R.Execute(
"df <- dataset " &
"#(lf)logit <- glm(" & dependent & " ~ " & independent & ", data = df, family = ""binomial"")" &
"#(lf)df$Predicted <- predict(logit, newdata = df, type = ""response"")" &
"#(lf)df$Prediction <- ifelse(df$Predicted > 0.5, 1, 0)" &
"#(lf)df", [dataset=df]),
Return = Source{[Name="df"]}[Value]