Skip to content

Instantly share code, notes, and snippets.

View cbaragao's full-sized avatar

Chris Aragao cbaragao

View GitHub Profile
(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]
(df as table, independent as text, dependent as text, family as text)=>
let
Source =
R.Execute(
"df <- dataset" &
"#(lf)logit <- glm(" & dependent & " ~ " & independent & ", data = df, family = """ & family &""") " &
"#(lf)summary_model <- summary(logit) " &
"#(lf)coef_df <- as.data.frame(summary_model$coefficients) " &
"#(lf)coef_df$Variable <- rownames(coef_df) " &
"#(lf)coef_df <- coef_df[, c(""Variable"", ""Estimate"", ""Std. Error"", ""z value"", ""Pr(>|z|)"")] " &