Skip to content

Instantly share code, notes, and snippets.

View deldersveld's full-sized avatar

David Eldersveld deldersveld

View GitHub Profile
@deldersveld
deldersveld / ATUS-ActivityLexicon.pq
Last active February 2, 2024 14:29
American Time Use Survey - Activity Lexicon from PDF
let
FilePath = "[replace with folder path]\tabula-lexiconnoex0315.csv",
Source = Csv.Document(File.Contents(FilePath),[Delimiter=",", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.Csv]),
#"Removed Top Rows" = Table.Skip(Source,1),
#"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
#"Filtered Rows" = Table.SelectRows(#"Promoted Headers", each ([#"Major #(cr)category"] <> "ATUS 2003-2015 Activity coding lexicon" and [#"Major #(cr)category"] <> "Major #(cr)category")),
#"Replaced Value" = Table.ReplaceValue(#"Filtered Rows","",null,Replacer.ReplaceValue,{"Major #(cr)category", "First and #(cr)second-tier #(cr)categories"}),
#"Filled Down" = Table.FillDown(#"Replaced Value",{"Major #(cr)category", "First and #(cr)second-tier #(cr)categories"}),
#"Filtered Rows1" = Table.SelectRows(#"Filled Down", each ([#"6-digit #(cr)activity #(cr)code"] <> "")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows1",{"", "_1"})
Sparkline Line =
// Static line color - use %23 instead of # for Firefox compatibility
VAR LineColor = "%2301B8AA"
// "Date" field used in this example along the X axis
VAR XMinDate = MIN('Table'[Date])
VAR XMaxDate = MAX('Table'[Date])
// Obtain overall min and overall max measure values when evaluated for each date
library(leaflet)
library(htmlwidgets)
library(webshot)
customPalette <- colorFactor(c("red", "red", "navy", "#00274c"), domain = dataset$OwnerType)
m <- leaflet(data = dataset) %>%
setView(-85.5, 42.3, zoom = 6) %>%
addProviderTiles("CartoDB.Positron") %>%
addCircleMarkers(~Longitude, ~Latitude, radius = ~ifelse(OwnerType == "Private", 1, 3), color = ~customPalette(OwnerType), stroke = FALSE, fillOpacity = 0.8)
Sparkline Column Categorical Axis =
// Static column color - use %23 instead of # for Firefox compatibility
VAR BarColor = "%2301B8AA"
VAR BarOutlineColor = "%23DDDDDD"
VAR BarOutlineWidth = 2
// Obtain number of columns - width generated based on column count (~20 column maximum for bar chart)
VAR BarCount = DISTINCTCOUNT('Table'[Customer Segment])
VAR BarWidth = INT(DIVIDE(100,BarCount))
// Obtain overall min and overall max measure values when evaluated for each column
VAR YMinValue = MINX(VALUES('Table'[Customer Segment]),CALCULATE([Measure Value]))
@deldersveld
deldersveld / Power BI Sample Bar Chart Custom Visual.md
Last active October 26, 2021 11:45
Sample Power BI bar chart custom visual adapted from https://bl.ocks.org/mbostock/3885304

Sample Power BI bar chart custom visual adapted from https://bl.ocks.org/mbostock/3885304

  • Follow the instructions at https://github.com/Microsoft/PowerBI-visuals-docs to install the Power BI Custom Visual CLI Tool
  • Follow the steps on the same site to create a new visual and install the typings for D3
  • Copy and paste the code from this gist's "visual.ts" into "src/visual.ts"
  • Copy and paste the code from this gist's "visual.less" into "style/visual.less"
  • Start the visual in CLI and view it using the Developer Visual in Power BI service

Adapting a visual like this from a static D3 example is not a simple matter of copying and pasting. If you look through the original D3 example and compare it with the Power BI version, you will see some changes where the original was split between constructor() and update(). The original D3 also only used enter(), but for a dynamic visual in Power BI, code for transition() and exit() were also added. Some other tweaks were needed to get it working decently

{
"version": "1.0",
"name": "[Tool Name]",
"description": "[Tool Description]",
"path": "C:\\[PATH TO PYTHON EXECUTABLE]\\python.exe",
"arguments": "C:/[PATH TO PYTHON SCRIPT].py \"%server%\" \"%database%\"",
"iconData": "data:image/png;base64,[YOUR BASE64 IMAGE CONTENT]"
}
import sys
print('Power BI Desktop Connection')
print(str(sys.argv[1]))
print(str(sys.argv[2]))
print('')
conn = "Provider=MSOLAP;Data Source=" + str(sys.argv[1]) + ";Initial Catalog='';"
print(conn)
@deldersveld
deldersveld / RGBDecimalColorToHex.dax
Created July 13, 2018 11:59
DAX - RGB Decimal Color to Hex
RGB Decimal Color to Hex =
// Manually enter components of RGB decimal color value, e.g. "RGB(1,184,170)"
VAR RedDecimalColorValue = 1
VAR GreenDecimalColorValue = 184
VAR BlueDecimalColorValue = 170
//Decimal to Hex
VAR RedPosition0Dec = MOD(RedDecimalColorValue,16)
VAR RedPosition0Div = INT(RedDecimalColorValue / 16)
VAR RedPosition1Dec = MOD(RedPosition0Div,16)
VAR RedPosition1Div = INT(RedPosition0Div / 16)
@deldersveld
deldersveld / HexColorToRGB.dax
Last active July 13, 2018 03:30
DAX - Hex Color to RGB
Hex Color to RGB =
VAR HexColorValue = "#01B8AA"
// Do not change anything below this line
VAR PositionRed1 = RIGHT(LEFT(HexColorValue,2),1)
VAR PositionRed0 = RIGHT(LEFT(HexColorValue,3),1)
VAR PositionGreen1 = RIGHT(LEFT(HexColorValue,4),1)
VAR PositionGreen0 = RIGHT(LEFT(HexColorValue,5),1)
VAR PositionBlue1 = RIGHT(LEFT(HexColorValue,6),1)
VAR PositionBlue0 = RIGHT(LEFT(HexColorValue,7),1)
VAR Red1 = SWITCH(PositionRed1,"A",10,"B",11,"C",12,"D",13,"E",14,"F",15,PositionRed1) * 16
$getFirstLine = $true
get-childItem "[path]\*.tsv" | foreach {
$filePath = $_
$lines = $lines = Get-Content $filePath
$linesToWrite = switch($getFirstLine) {
$true {$lines}
$false {$lines | Select -Skip 1}